[英]Extending in a media query in Sass and Bootstrap 4
I am updating to the new Bootstrap version 4 which now uses Sass over Less, and my application that uses Bootstrap used the Less files directly rather than the fully compiled css distribution.我正在更新到新的 Bootstrap 版本 4,它现在使用 Sass over Less,而我使用 Bootstrap 的应用程序直接使用了 Less 文件,而不是完全编译的 css 发行版。
But now I've hit a snag - I understand that Sass doesn't allow you to use an @extend
within a @media
query, but what I don't understand is how I get around the simple problem of overloading a style on a larger screen.但是现在我遇到了一个障碍——我知道 Sass 不允许你在
@media
查询中使用@extend
,但我不明白的是我如何解决在一个样式上重载一个简单的问题更大的屏幕。
For example, a stripped down version of my Sass looks like:例如,我的 Sass 的精简版如下所示:
.box {
//mobile styles
background: green;
.logout-button {
//mobile styles for logout button
background: red;
}
}
//everything over 767px
@media (min-width: 768px) {
.box {
.logout-button {
@extend .btn-link; //bring in the Bootstrap button link style here
}
}
}
In this example, I want the .logout-button to use the .btn-link
style from Bootstrap.在此示例中,我希望 .logout-button 使用 Bootstrap 中的
.btn-link
样式。 But because you can't @extend
like this, I'm totally confused as to how to achieve this.但是因为你不能像这样
@extend
,我完全不知道如何实现这一点。
Is there a completely different approach required in Sass compared to Less?与 Less 相比,Sass 是否需要一种完全不同的方法? Less allows you to do this so I'd be surprised if this was a limitation considering Bootstrap's recent switch.
Less 允许您执行此操作,因此考虑到 Bootstrap 最近的切换,如果这是一个限制,我会感到惊讶。
Thanks in advance!提前致谢!
You are right, you can not @extend
like this.你是对的,你不能像这样
@extend
。 But you can @include
some @mixin
.但是你可以
@include
一些@mixin
。
There is unfortunately no @mixin
to create .btn-link
variant by default in Bootstrap 4.不幸的是,Bootstrap 4 中默认没有
@mixin
来创建.btn-link
变体。
If you wanted some other variant, you could use these @mixin
s which are part of Bootstrap 4:如果你想要一些其他的变体,你可以使用这些
@mixin
s,它们是 Bootstrap 4 的一部分:
@include button-variant($background, $border, $active-background: darken($background, 7.5%), $active-border: darken($border, 10%))
or this或者这个
@include button-outline-variant($color, $color-hover: #fff)
(Useful list of Boostrap 4 mixins ) ( Boostrap 4 mixin 的有用列表)
But if you need .btn-link
you have to make your own @mixin
.但是如果你需要
.btn-link
你必须制作你自己的@mixin
。 Something like this (it's copy/paste style of .btn-link
in to new mixin):像这样的东西(它是将
.btn-link
复制/粘贴到新 mixin 的样式):
//
// Link buttons
//
// Make a button look and behave like a link
@mixin btn-link() {
font-weight: $font-weight-normal;
color: $link-color;
background-color: transparent;
@include hover {
color: $link-hover-color;
text-decoration: $link-hover-decoration;
background-color: transparent;
border-color: transparent;
}
&:focus,
&.focus {
text-decoration: $link-hover-decoration;
border-color: transparent;
box-shadow: none;
}
&:disabled,
&.disabled {
color: $btn-link-disabled-color;
pointer-events: none;
}
// No need for an active state here
}
And then you can use it as you wish:然后你可以随意使用它:
//everything over 767px
@media (min-width: 768px) {
.box {
.logout-button {
@include btn-link;
}
}
}
Nice article about this: SCSS - Extending Classes Within Media Queries关于这个的好文章: SCSS - 在媒体查询中扩展类
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.