gpt4 book ai didi

css - SASS 媒体查询最佳实践?

转载 作者:行者123 更新时间:2023-12-04 03:18:06 27 4
gpt4 key购买 nike

可以在元素中嵌套媒体查询吗?如果我想使用 min-width: 480px在其他地方会有巨大的重复。请看我的代码示例。还是只是使用旧的方式?任何的想法?

SASS

.navbar {
height: 500px;
width: 500px;

@media screen and (min-width: 480px) {
background-color: lightgreen;
}
}

.items {
padding: 15px;
color: red;

@media screen and (min-width: 480px) {
border: 1px solid black;
}
}

CSS
@media screen and (min-width: 480px) {
.navbar {
background-color: lightgreen;
}

.items {
border: 1px solid black;
}
}

最佳答案

$pc: 1024px;    // PC screen size.
$tablet: 720px; // Tablet screen size.
$phone: 320px; // Phone screen size.

@mixin responsive($media) {
@if $media= phone {
@media only screen and (max-width: $tablet - 1) {
@content;
}
}
@else if $media= tablet {
@media only screen and (min-width: $tablet - 1) and (max-width: $pc) {
@content;
}
}
@else if $media= pc {
@media only screen and (min-width: $pc + 1) and (min-width: $pc) {
@content;
}
}
@else if $media= pc_tablet {
@media only screen and (min-width: $tablet - 1) {
@content;
}
}
}

例子
body {
@include responsive(pc) {
background: red;
}
@include responsive(tablet) {
background: yellow;
}
@include responsive(phone) {
background: green;
}
}

关于css - SASS 媒体查询最佳实践?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39773851/

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