gpt4 book ai didi

css - 如何重构媒体查询中的常见CSS规则?

转载 作者:太空宇宙 更新时间:2023-11-04 00:44:33 25 4
gpt4 key购买 nike

我正在定义几个媒体查询,并且有一些重复的代码:

@media only screen and (max-width:768px) {
.list__figure {
margin-right: 20px;
width: 80px;
}
.list__content {
width: calc(100% - 100px);
}
.ico-bim--over-img {
left: 5px;
right: inherit;
top: 5px;
}
.ico-lnb-valid--over-img {
left: 78px;
top: 5px;
}
}
@media only screen and (min-width:769px) and (max-width:1040px) {
.list__figure {
margin-right: 20px;
width: 80px;
}
.list__content {
width: calc(100% - 100px);
}
.ico-bim--over-img {
left: 5px;
right: inherit;
top: 5px;
}
.ico-lnb-valid--over-img {
left: calc(100% - 47px);
top: 5px;
}
}

那里,唯一改变的规则是 ico-lnb-valid--over-img 中的 left

很可能我必须为不同的媒体查询添加一些其他规则,但从并使用一些已经定义的代码开始。

我该如何重构它?

最佳答案

@user8424881 是正确的——只有那些与一个媒体查询不同的东西才需要包含在媒体查询中。

代码可以是这样的:

/* No media queries needed */

.list__figure {
margin-right: 20px;
width: 80px;
}
.list__content {
width: calc(100% - 100px);
}
.ico-bim--over-img {
left: 5px;
right: inherit;
top: 5px;
}
.ico-lnb-valid--over-img {
top: 5px;
}

/* Media queries below */

@media only screen and (max-width:768px) {
.ico-lnb-valid--over-img {
left: 78px;
}
}
@media only screen and (min-width:769px) and (max-width:1040px) {
.ico-lnb-valid--over-img {
left: calc(100% - 47px);
}
}

关于css - 如何重构媒体查询中的常见CSS规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57514965/

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