gpt4 book ai didi

css - 媒体查询分组而不是多个分散的媒体查询匹配

转载 作者:技术小花猫 更新时间:2023-10-29 11:46:57 26 4
gpt4 key购买 nike

我正在尝试使用 LESS(不喜欢 SASS 语法)并且一直在尝试找出使用它进行媒体查询的最佳方式。

我通读了this关于如何使用 LESS “执行”媒体查询的博客文章,但它指出了这样一个事实,即这会导致所有媒体查询在生成的 CSS 中分离和分散。这并没有真正打扰我(我不太关心结果,更关心它的工作原理)。让我感到困扰的是一条评论,该评论谈到了从 iOS 设备查看时出现的问题,评论者发现一旦合并了媒体查询,问题就解决了。

有没有人找到将媒体查询与 LESS 结合使用的好解决方案?

我设想的这种工作方式类似于...

//Have an overall structure...
.overall(){
//Have ALL your CSS that would be modified by media queries and heavily use
//variables that are set inside of each media queries.
}
@media only screen and (min-width: 1024px){
//Define variables for this media query (widths/etc)
.overall
}

我知道这可能会有一些问题,但当前的设置似乎没有那么有益。

所以我想我的问题是是否有任何好的解决方案/黑客来允许分组媒体查询?

(以防万一,我使用 dotless 作为引擎来解析我的 .less 文件)

最佳答案

首先,你在问题中给出的解决方案肯定有一些用处。

不过,我想的一件事是,最好将所有媒体查询变量定义为彼此“接近”(您的解决方案将在每个媒体查询调用下都有它们)。因此,我建议将以下内容作为替代解决方案。它也有缺点,一个可能是预先编码更多。

更少的代码

//define our break points as variables
@mediaBreak1: 800px;
@mediaBreak2: 1024px;
@mediaBreak3: 1280px;

//this mixin builds the entire media query based on the break number
.buildMediaQuery(@min) {
@media only screen and (min-width: @min) {

//define a variable output mixin for a class included in the query
.myClass1(@color) {
.myClass1 {
color: @color;
}
}

//define a builder guarded mixin for each break point of the query
//in these is where we change the variable for the media break (here, color)
.buildMyClass1() when (@min = @mediaBreak1) {
.myClass1(red);
}
.buildMyClass1() when (@min = @mediaBreak2) {
.myClass1(green);
}
.buildMyClass1() when (@min = @mediaBreak3) {
.myClass1(blue);
}

//call the builder mixin
.buildMyClass1();

//define a variable output mixin for a nested selector included in the query
.mySelector1(@fontSize) {
section {
width: (@min - 40);
margin: 0 auto;
a {
font-size: @fontSize;
}
}
}

//Again, define a builder guarded mixin for each break point of the query
//in these is where we change the variable for the media break (here, font-size)
.buildMySelector1() when (@min = @mediaBreak1) {
.mySelector1(10px);
}
.buildMySelector1() when (@min = @mediaBreak2) {
.mySelector1(12px);
}
.buildMySelector1() when (@min = @mediaBreak3) {
.mySelector1(14px);
}

//call the builder mixin
.buildMySelector1();

//ect., ect., etc. for as many parts needed in the media queries.
}
}

//call our code to build the queries
.buildMediaQuery(@mediaBreak1);
.buildMediaQuery(@mediaBreak2);
.buildMediaQuery(@mediaBreak3);

CSS 输出

@media only screen and (min-width: 800px) {
.myClass1 {
color: #ff0000;
}
section {
width: 760px;
margin: 0 auto;
}
section a {
font-size: 10px;
}
}
@media only screen and (min-width: 1024px) {
.myClass1 {
color: #008000;
}
section {
width: 984px;
margin: 0 auto;
}
section a {
font-size: 12px;
}
}
@media only screen and (min-width: 1280px) {
.myClass1 {
color: #0000ff;
}
section {
width: 1240px;
margin: 0 auto;
}
section a {
font-size: 14px;
}
}

关于css - 媒体查询分组而不是多个分散的媒体查询匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13503862/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com