8

我正在使用 Sass 3.4.1 和 BEM,所以我的 scss 是:

.photo-of-the-day{
    &--title{
        font-size: 16px;
    }
}

我希望每次将鼠标悬停在.photo-of-the-day标题发生的事情上,这很常见,所以通常在 css 中:

.photo-of-the-day:hover .photo-of-the-day--title{
    font-size:12px
}

问题是使用 BEM,这是我找到的唯一方法,看起来有点难看

.photo-of-the-day{
    &--title{
        font-size: 16px;
    }
    &:hover{
        background: red;
        /* this is ugly */
        .photo-of-the-day--title{
            text-decoration: underline;
        }
    }
}

所以我想知道我是否可以继承.photo-of-the-day选择器并在悬停中使用它以避免再次复制完整的选择器。

理想情况下是这样的:

.photo-of-the-day{
    &--title{
        font-size: 16px;
    }
    &:hover{
        background: red;
        &&--title{
            text-decoration: underline;
        }
    }
}

或者接近回归到 BEM 的父选择器的东西。可能吗?

4

2 回答 2

8

如果您坚持嵌套所有内容,那么您可以做的最好的事情是:

.photo-of-the-day {
    $root: &;
    &--title{
        font-size: 16px;
    }
    &:hover{
        #{$root}--title {
            text-decoration: underline;
        }
    }
}
于 2014-10-10T16:19:26.087 回答
7

您可以使用以下语法:

.photo-of-the-day {
    &--title {
        font-size: 16px;
    }
    &:hover &--title {
        text-decoration: underline;
    }
}
于 2014-10-10T16:20:35.787 回答