需要将媒体查询放在页面的一个部分中才能使其在另一个页面中工作吗? SCSS / SASS

问题描述 投票:1回答:1

我正在使用带有SCSS变量和mixins的标准Webpack构建。出于某种原因,除非在使用断点的上一节中放置了相同的媒体查询,否则无法在此站点的某个部分的媒体查询中覆盖样式。这是我的代码:

__ variables.scss

$sm-screen: 576px;
$md-screen: 768px;
$lg-screen: 992px;
$xl-screen: 1200px;

_ mixins.scss

@mixin sm-screen {
  @media screen and (min-width: #{$sm-screen}) {
      @content;
  }
}

@mixin md-screen {
  @media screen and (min-width: #{$md-screen}) {
      @content;
  }
}

@mixin lg-screen {
  @media screen and (min-width: #{$lg-screen}) {
      @content;
  }
}

@mixin xl-screen {
  @media screen and (min-width: #{$xl-screen}) {
      @content;
  }
}

@mixin screen-size($screen) {
  @media screen and (min-width: $screen) {
      @content;
  }
}

main.scss(正在导入文件的地方)

@import "/base/variables";
@import "/base/mixins";
@import "home";

_ home.scss(问题出在这里)

// ————————————————————————————————————————————————————————————
// HERO SECTION
// ————————————————————————————————————————————————————————————
  #hero-section {
    .hero-heading {
      font-size: 2rem;
    }

    @include sm-screen {
      .hero-heading {
        font-size: 5rem;
      }
    }

    @include lg-screen {
      .hero-heading {
        font-size: 6.5rem;
      }
    }

    @include xl-screen {
      .hero-heading {
        font-size: 6.5rem;
      }
    }
  }

...以及您所期望的,字体大小会正确覆盖:Chrome inspector showing the font overriding correctly in media queries

// ————————————————————————————————————————————————————————————
// INTRO SECTION (where things get buggy)
// ————————————————————————————————————————————————————————————
  #intro-section {
    .icon-group {
      list-style: none;
      padding: 0;
      margin-bottom: 50px;
    }

    @include md-screen {
      .icon-group {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        max-width: 600px;
        margin-left: auto;
        margin-right: auto;
        position: relative;
      }
    }

    @include xl-screen {
      .icon-group {
        display: flex;
        max-width: none;
        justify-content: center;
      }
    }
  }

当我在本节中使用相同的mixins时,中等断点将覆盖xl断点:medium breakpoint overriding the large breakpoint

这里是踢脚:

当我在HERO SECTION代码中添加一个中间断点时,该断点将按预期在INTRO SECTION中工作:

// ————————————————————————————————————————————————————————————
// HERO SECTION
// ————————————————————————————————————————————————————————————
  #hero-section {
    .hero-heading {
      font-size: 2rem;
    }

    @include sm-screen {
      .hero-heading {
        font-size: 5rem;
      }
    }

    @include md-screen {
      .hero-heading {
        font-size: 5rem;
      }
    }

    @include lg-screen {
      .hero-heading {
        font-size: 6.5rem;
      }
    }

    @include xl-screen {
      .hero-heading {
        font-size: 6.5rem;
      }
    }
  }


// ————————————————————————————————————————————————————————————
// INTRO SECTION
// ————————————————————————————————————————————————————————————
  #intro-section {
    .icon-group {
      list-style: none;
      padding: 0;
      margin-bottom: 50px;
    }

    @include md-screen {
      .icon-group {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        max-width: 600px;
        margin-left: auto;
        margin-right: auto;
        position: relative;
      }
    }

    @include xl-screen {
      .icon-group {
        display: flex;
        max-width: none;
        justify-content: center;
      }
    }
  }

it works now

为什么??

编辑:

这是我的全部_home.scss_variables.scss文件。当我注释掉所有HERO SECTION时,其他所有东西都可以正常工作。 HERO SECTION中必须有一个错误,但我只是没有抓住它。

__ variables.scss

// ————————————————————————————————————————————————————————————
// COLORS
// ————————————————————————————————————————————————————————————
$grey-01: #181F2C;
$grey-02: #70849f;
$grey-03: #E0E5EE;

$blue-01: #A0AFC3;

$green-01: #0C7C25;
$green-01-hover: darken($green-01, 8%);

// ————————————————————————————————————————————————————————————
// GENERAL
// ————————————————————————————————————————————————————————————
$dur: 0.25s;
$skew: 32;

// ————————————————————————————————————————————————————————————
// BREAKPOINTS
// ————————————————————————————————————————————————————————————
$sm-screen: 576px;
$md-screen: 768px;
$lg-screen: 992px;
$xl-screen: 1200px;

_ home.scss

body.home {

  // ————————————————————————————————————————————————————————————
  // HERO SECTION
  // ————————————————————————————————————————————————————————————
  #hero-section {
    min-height: 100vh;
    overflow: hidden;
    display: flex;
    justify-content: stretch;

    .left-side,
    .right-side {
      width: 50%;
      overflow: hidden;
      position: relative;
      background: $grey-01;
      flex-grow: 1;
      will-change: width;
      transition: width $dur ease;

      &.tap-active {
        width: 150%;

        .hero-content {
          visibility: visible;
          opacity: 1;
          margin: 0 #{$skew}vh;
        }

        .hero-img {
          opacity: 0.12;
          background-color: $grey-01;
        }
      }
    }

    .hero-content {
      opacity: 0;
      visibility: hidden;
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      color: white;
      z-index: 1;
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      transform: skew(#{$skew}deg);
      transition: all $dur ease;
      margin: 0 #{$skew}vh;
      width: calc(100vw - 10%);
    }

    .hero-heading {
      font-size: 22vw;
      text-align: center;
      line-height: 0.9;

      .h-underline {
        background: none;
      }
    }

    .hero-btn {
      margin-top: 30px;

      @media screen and (max-width: $lg-screen - 1px) {
        &.btn-disabled {
          pointer-events: none;
        }
      }
    }

    .hero-img {
      background: transparent center center/cover no-repeat;
      background-blend-mode: luminosity;
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      transform: skew(#{$skew}deg);
      transition: all $dur ease;
    }

    .left-side {
      transform: skew(-#{$skew}deg);
      margin-left: -#{$skew}vh;
      margin-right: 1px;

      .hero-content {
        right: -#{$skew}vh;
        margin: 0 #{$skew}vh 0 0;
      }

      .hero-btn {
        margin-bottom: 20vh;
      }

      .hero-img {
        right: -#{$skew}vh;
      }
    }

    .right-side {
      transform: skew(-#{$skew}deg);
      margin-right: -#{$skew}vh;
      margin-left: 1px;

      .hero-content {
        left: -#{$skew}vh;
      }

      .hero-heading {
        margin-top: 20vh;
      }

      .hero-img {
        left: -#{$skew}vh;
      }
    }

    @include sm-screen {
      .hero-heading {
        font-size: 5rem;
      }
    }

    @include lg-screen {

      .left-side,
      .right-side {
        .hero-content {
          margin: 0;
        }

        &:hover {
          width: 70%;

          .hero-content {
            visibility: visible;
            opacity: 1;
            margin: 0 #{$skew}vh;
            width: 50vw;
          }

          .hero-img {
            opacity: 0.12;
            background-color: $grey-01;
          }
        }
      }

      .left-side {
        .hero-content {
          margin-left: -#{$skew}vh;
        }

        .hero-btn {
          margin-bottom: 0;
        }
      }

      .right-side {
        .hero-heading {
          margin-top: 0;
        }
      }
    }

    @include xl-screen {
      .hero-heading {
        font-size: 6.5rem;
      }
    }
  }


  // ————————————————————————————————————————————————————————————
  // CLIENT LOGOS
  // ————————————————————————————————————————————————————————————
  #client-logos {
    padding: 15px 0;
    position: relative;

    &:before,
    &:after {
      content: '';
      display: block;
      position: absolute;
      height: 100%;
      width: 25%;
      max-width: 100px;
      top: 0;
      z-index: 1;
      pointer-events: none;
    }

    &:before {
      left: 0;
      background: linear-gradient(to right, white, white 15%, rgba(white, 0));
    }

    &:after {
      right: 0;
      background: linear-gradient(to left, white, white 15%, rgba(white, 0));
    }

    .glide__slides {
      align-items: center;
    }

    .glide__slide {
      display: flex;
      align-items: center;
      justify-content: center;
    }

    .client-logo {
      display: block;
      max-width: 100%;
      max-height: 15vw;

    }

    @include screen-size(800px) {
      padding: 25px 0;

      .client-logo {
        max-height: 70px;
        max-width: 125px;
      }
    }
  }


  // ————————————————————————————————————————————————————————————
  // INTRO SECTION
  // ————————————————————————————————————————————————————————————
  #intro-section {
    background: $grey-01 center center/cover no-repeat;
    padding: 60px 0;
    color: white;
    text-align: center;

    .intro-heading {
      margin-bottom: 30px;
    }

    .intro-text {
      margin-bottom: 40px;
    }

    .icon-group {
      list-style: none;
      padding: 0;
      margin-bottom: 50px;
    }

    .icon-wrapper {
      display: block;
      padding-top: 75px;
      background: center top/65px no-repeat;
      margin-bottom: 25px;
      position: relative;

      &:not(:last-of-type):after {
        content: '';
        display: block;
        background: $blue-01;
        opacity: 0.6;
        width: 45px;
        height: 1px;
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        bottom: 0;
      }

      &:last-of-type {
        margin-bottom: 0;

        .icon-heading {
          padding-bottom: 0;
        }
      }

      .icon-heading {
        padding-bottom: 25px;
      }
    }

    @include md-screen {
      .icon-group {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        max-width: 600px;
        margin-left: auto;
        margin-right: auto;
        position: relative;

        &:before,
        &:after {
          content: '';
          display: block;
          background: $blue-01;
          opacity: 0.6;
          position: absolute;
          left: 50%;
          top: 50%;
          transform: translate(-50%, -50%);
        }

        &:before {
          width: 1px;
          height: 80%;
        }

        &:after {
          width: 80%;
          height: 1px;
        }
      }

      .icon-wrapper {
        margin: 30px;

        &:not(:last-of-type):after {
          display: none;
        }

        .icon-heading {
          padding: 0;
        }
      }
    }

    @include xl-screen {
      .icon-group {
        display: flex;
        max-width: none;
        justify-content: center;

        &:before,
        &:after {
          display: none;
        }
      }

      .icon-wrapper {
        margin: 0;
        padding-right: 60px;
        padding-left: 60px;

        &:not(:last-of-type):after {
          display: block;
          height: 45px;
          width: 1px;
          left: 100%;
          bottom: 50%;
          transform: translate(0, 50%);
        }
      }
    }
  }


  // ————————————————————————————————————————————————————————————
  // FEATURED VIDEO
  // ————————————————————————————————————————————————————————————
  #featured-video {
    position: relative;

    .video-poster {
      position: absolute;
      top: 0;
      right: 0;
      left: 0;
      bottom: 0;
      z-index: 4;
      @include flex-center;
      text-align: center;
      // background: url(../assets/images/video-poster.jpg) center bottom/cover no-repeat $orange-01;
    }

    .fancy-title {
      display: none;
    }

    h2 {
      display: none;
    }

    .play-video-btn {
      // @extend .btn-text;

      cursor: pointer;
      position: relative;
      display: inline-flex;
      flex-direction: column;
      align-items: center;
      font-size: 1rem;
      margin-top: 26px;

      &:before {
        content: "";
        width: 0px;
        height: 0px;
        border-top: 14px solid transparent;
        border-bottom: 14px solid transparent;
        border-left: 26px solid white;
        margin-bottom: 32px;
        margin-right: -9px;
      }

      &:after {
        content: "";
        position: absolute;
        width: 75px;
        height: 75px;
        border-radius: 50%;
        border: 2px solid white;
        transition: all 0.2s ease;
        top: -27px;
        left: 50%;
        transform: translateX(-50%);
      }

      &:hover,
      &:focus {
        &:after {
          transform: translateX(-50%) scale(1.1);
        }
      }
    }

    @include sm-screen {
      h2 {
        display: block;
        color: white;
        font-size: 10vw;
        margin-bottom: 5%;
      }
    }

    @include md-screen {
      .fancy-title {
        display: inline-block;
      }
    }

    @include xl-screen {
      h2 {
        font-size: 8rem;
      }
    }
  }
}
css sass media
1个回答
0
投票

我能够使用一个包含所有@include的类名来完成此工作。这将按照声明的线性顺序呈现每个@media查询块。

#hero-section {
  .hero-heading {
    font-size: 2rem;

    @include sm-screen {
      font-size: 5rem;
    }

    @include lg-screen {
      font-size: 6.5rem;
    }

    @include xl-screen {
      font-size: 6.5rem;
    }
  }
}

#intro-section {
  .icon-group {
    list-style: none;
    padding: 0;
    margin-bottom: 50px;

    @include md-screen {
      display: grid;
      grid-template-columns: repeat(2, 1fr);
      max-width: 600px;
      margin-left: auto;
      margin-right: auto;
      position: relative;
    }

    @include xl-screen {
      display: flex;
      max-width: none;
      justify-content: center;
    }
  }
}

jsFiddle

© www.soinside.com 2019 - 2024. All rights reserved.