【发布时间】:2014-01-23 07:07:22
【问题描述】:
我有一个基于 Twitter Bootstrap 响应式媒体查询的 Sass mixin 用于我的媒体查询:
@mixin respond-to($media) {
@if $media == handhelds {
/* Landscape phones and down */
@media (max-width: 480px) { @content; }
}
@else if $media == small {
/* Landscape phone to portrait tablet */
@media (max-width: 767px) {@content; }
}
@else if $media == medium {
/* Portrait tablet to landscape and desktop */
@media (min-width: 768px) and (max-width: 979px) { @content; }
}
@else if $media == large {
/* Large desktop */
@media (min-width: 1200px) { @content; }
}
@else {
@media only screen and (max-width: #{$media}px) { @content; }
}
}
我在我的 SCSS 文件中这样称呼它们:
.link {
color:blue;
@include respond-to(medium) {
color: red;
}
}
但是,有时我想用相同的样式设置多个查询的样式。现在我正在这样做:
.link {
color:blue; /* this is fine for handheld and small sizes*/
/*now I want to change the styles that are cascading to medium and large*/
@include respond-to(medium) {
color: red;
}
@include respond-to(large) {
color: red;
}
}
但我在重复代码,所以我想知道是否有更简洁的方法来编写它,以便我可以针对多个查询。像这样的东西,所以我不需要重复我的代码(我知道这不起作用):
@include respond-to(medium, large) {
color: red;
}
对处理此问题的最佳方法有何建议?
【问题讨论】:
-
嗯,它仍然是级联风格,所以什么既适合中型也适合大型(因为您使用的是最小宽度媒体查询)不?
-
不,所以我应该更具体(我会编辑问题)。假设我的默认样式(适用于手持设备和移动设备)是 color:blue;但后来我想要中号和大号颜色:红色;。它确实级联,但我想覆盖中型和大型。这有意义吗?
标签: css sass media-queries