CSS DRY 按钮状态

标签 css

有没有办法做到这一点,因为它只是重复相同的代码?

.button-hover {
  color:                           $button-hover-font-color;
  background-color:                $button-hover-color;
  &:hover, &:focus, &:active, &:visited {
    color:                           $button-hover-font-color;
    background-color:                $button-hover-color;
  }
}

最佳答案

是的,试试纯 CSS 语法:

.button-hover, .button-hover:hover, .button-hover:focus, .button-hover:active, .button-hover:visited {
    color:            $button-hover-font-color;
    background-color: $button-hover-color;
}

或者使用 mixin:(我只知道 LESS 语法,但我猜它对于你使用的任何预处理器都是相似的)

.button-hover {
    color:            @button-hover-font-color;
    background-color: @button-hover-color;

    &:hover, &:focus, &:active, &:visited {
        .button-hover();
    }
}

关于CSS DRY 按钮状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17700012/

相关文章:

css - CSS 中的相对 URL 真的相对于文件本身吗?

html - 将悬停下拉菜单转换为可点击的下拉菜单

css - 如何处理 AMP 中的 "the author stylesheet specified in tag ' 样式 amp-custom' 太长?

javascript - jQuery 下一个和上一个按钮

html - 每个部分的响应式背景图片

javascript - 下拉菜单的可用性

html - 使用 Bootstrap 响应图像限制图像高度?

html - 单击按钮时,AngularJS 在给定条件下隐藏表中的行

html - 跨不同浏览器的一致网站呈现

css - 如何在 div 的中间(水平)创建边框?