Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sass with BEM, inherit selector

Tags:

css

sass

bem

I'm using Sass 3.4.1 and BEM so my scss is:

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

and I want every time hover over .photo-of-the-day something happen with title, that's pretty common so usually in css:

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

the thing is using BEM this is the only way I found and looks kinda ugly

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

so I was wondering if I can inherit .photo-of-the-day selector and use it inside the hover to avoid copy again the full selector.

Ideally would be something like:

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

Or something close to comeback to the parent selector for BEM. Is it possible?

like image 670
alexrqs Avatar asked Oct 10 '14 16:10

alexrqs


People also ask

Should you use Bem CSS?

Benefits of using BEM Because of its unique naming scheme, we won't run into conflicts with other CSS names. BEM also provides a relationship between CSS and HTML. Ambiguous names are hard to maintain in the future⁣. Overall, BEM is my favourite CSS naming scheme, and I strongly suggest you try using it too!


1 Answers

If you insist on nesting everything, the best you can do is this:

.photo-of-the-day {
    $root: &;
    &--title{
        font-size: 16px;
    }
    &:hover{
        #{$root}--title {
            text-decoration: underline;
        }
    }
}
like image 191
cimmanon Avatar answered Oct 27 '22 05:10

cimmanon