your programing

마법이 적은 멋진 미디어 쿼리

lovepro 2020. 10. 8. 08:25
반응형

마법이 적은 멋진 미디어 쿼리


less를 사용하는 일부 css 클래스 내에서 다른 해상도에 대해 css 스타일을 래핑하는 것이 좋습니다.

다음과 같은 작업을하고 싶습니다.

footer {
  width: 100%;
}

.tablet {
  footer {
    width: 768px;
  }
}

.desktop {
  footer {
    width: 940px;
  }
}

결국 다음과 같은 결과가 나타납니다.

footer {
  width: 100%;
}

@media screen and (min-width: 768px) {
  footer {
    width: 768px;
  }
}

@media screen and (min-width: 992px) {
  footer {
    width: 940px;
  }
}

.tablet는 다음과 같이 보일 수 있습니다.

@media screen and (min-width: 768px) {
  .tablet {

  }
}

누군가가 좋은 아이디어를 가지고 있기를 바랍니다!


내 프로젝트에서 내가 한 작업은 다음과 같습니다.

@desktop:   ~"only screen and (min-width: 960px) and (max-width: 1199px)";
@tablet:    ~"only screen and (min-width: 720px) and (max-width: 959px)";

@media @desktop {
  footer {
    width: 940px;
  }
}

@media @tablet {
  footer {
    width: 768px;
  }
}

이를 통해 미디어 쿼리를 한 번만 정의 할 수 있으며 적은 파일 전체에서 사용할 수 있습니다. 또한 좀 더 읽기 쉽습니다. :)


나는 Hai Nguyen의 대답 (승인 됨)에 완전히 동의하지만 다음과 같이하면 조금 더 정리할 수 있습니다.

@desktop:   ~"only screen and (min-width: 960px) and (max-width: 1199px)";
@tablet:    ~"only screen and (min-width: 720px) and (max-width: 959px)";

footer{
  width: 100%;
  @media @tablet {
    width: 768px;
  }
  @media @desktop {
    width: 940px;
  }
}

본질적으로 동일하지만 원본 선택기 내부에 미디어 쿼리를 중첩 할 수 있습니다. 특정 요소에 대한 모든 CSS를 함께 유지하고 스타일을 훨씬 더 모듈화합니다 (분리 중단 점 접근 방식과 비교).


Nguyen과 Yancey에 대해 +1-그리고 하나 더 추가됩니다.

당신이 원하는 경우 명시 적으로 정의 폭의를, 당신은 함께 그것을 달성 할 수있는 string interpolation변수 중단 점에 대해. 예를 들어 부트 스트랩의 경우-엄격한 규칙은 정의 중복을 방지하는 것입니다.

@screen-xs-min:     480px;
@screen-sm-min:     768px;
@screen-md-min:     992px;
@screen-lg-min:     1200px;

@screen-xs-max:     (@screen-sm-min - 1);
@screen-sm-max:     (@screen-md-min - 1);
@screen-md-max:     (@screen-lg-min - 1);

@phone:             ~"only screen and (max-width: @{screen-xs-min})";
@phone-strict:      ~"only screen and (min-width: @{screen-xs-min}) and (max-width: @{screen-xs-max})";
@tablet:            ~"only screen and (min-width: @{screen-sm-min})";
@tablet-strict:     ~"only screen and (min-width: @{screen-sm-min}) and (max-width: @{screen-sm-max})";
@desktop:           ~"only screen and (min-width: @{screen-md-min})";
@desktop-strict:    ~"only screen and (min-width: @{screen-md-min}) and (max-width: @{screen-md-max})";
@large:             ~"only screen and (min-width: @{screen-lg-min})";

footer{
    width: 100%;
    @media @tablet {
        width: 768px;
    }
    @media @desktop {
        width: 940px;
    }
}

다음과 같은 미디어 쿼리를 결합 할 수도 있습니다.

@media @desktop, @tablet, @ipad{ 

//common styles... 

}

이 믹스 인 및 변수를 사용하고 있습니다.

.max(@max; @rules){@media only screen and (max-width: (@max - 1)){@rules();}}
.min(@min; @rules){@media only screen and (min-width: @min){@rules();}}
.bw(@min; @max; @rules){@media only screen and (min-width: @min) and (max-width: (@max - 1)){@rules();}}

@pad: 480px;
@tab: 800px;
@desktop: 992px;
@hd: 1200px;

그래서 이거

footer{
    width: 100%;
    .bw(@tab,@desktop,{
        width: 768px;
    });
    .min(@desktop,{
        width: 940px;
    });
}

된다

footer {
  width: 100%;
}
@media only screen and (min-width: 800px) and (max-width: 991px) {
  footer {
    width: 768px;
  }
}
@media only screen and (min-width: 992px) {
  footer {
    width: 940px;
  }
}

다음과 같은 설정을 사용합니다.

@vp_desktop:    801px;
@vp_tablet:     800px;
@vp_mobile:     400px;

.OnDesktop(@rules) { @media screen and (min-width:@vp_desktop){ @rules(); } };
.OnTablet(@rules) { @media screen and (max-width:@vp_tablet){ @rules(); } };
.OnMobile(@rules) { @media screen and (max-width:@vp_mobile){ @rules(); } };

변수 만 설정하면되고 믹스 인이 나머지를 처리하므로 유지 관리가 매우 쉬우면서도 유연합니다.

div {
  display: inline-block;
  .OnTablet({
    display: block;
  });
}

이 기술은 매우 쉽지만 잘못 사용하면 CSS 출력이 미디어 쿼리로 가득 차게됩니다. 미디어 쿼리를 중단 점당, 파일 당 1 개로 제한하려고합니다. 파일은 header.less 또는 search.less가됩니다.

N.B. This method probably won't compile unless you're using the javascript compiler.


And this is what I have used for my project:

    @mobile:   ~"only screen and (min-width: 320px) and (max-width: 767px)";
    @tablet:    ~"only screen and (min-width: 768px) and (max-width: 991px)";
    @ipad:    ~"only screen and (min-width: 992px) and (max-width: 1024px)";

    @media @mobile {
      .banner{
        width: auto;
      }
    }

    @media @tablet {
      .banner{
        width: 720px;
      }
    }

  @media @ipad {
      .banner{
        width: 920px;
      }
    }

@highdensity:  ~"only screen and (-webkit-min-device-pixel-ratio: 1.5)",
               ~"only screen and (min--moz-device-pixel-ratio: 1.5)",
               ~"only screen and (-o-min-device-pixel-ratio: 3/2)",
               ~"only screen and (min-device-pixel-ratio: 1.5)";

@mobile:        ~"only screen and (max-width: 750px)";
@tab:       ~"only screen and (min-width: 751px) and (max-width: 900px)";
@regular:        ~"only screen and (min-width: 901px) and (max-width: 1280px)";
@extra-large:  ~"only screen and (min-width: 1281px)";

참고URL : https://stackoverflow.com/questions/15837808/fancy-media-queries-with-some-less-magic

반응형