Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax to combine media query with a qualifying selector

In our current design, form elements get styled differently on a mobile device:

@media screen and (max-width: 759px) {
    form label {
        display: block;
        float: none;
    }
    /*There's more...*/
}

Given the limited screen space, we're basically forcing a vertical form layout (#3-4 in this classic uxmatters link).

Because some form labels have to be insanely verbose For sensible reasons, we now want to apply this style to some desktop forms, i.e. outside of scope of the media query. This, however, is invalid SASS syntax:

@media screen and (max-width: 759px), .vertical-form {
    /*...*/
}

How can the ruleset be applied on output within the media query and to .vertical-form form label (or, ideally, form.vertical-form label) elements?

like image 960
Oleg Avatar asked Jan 12 '23 04:01

Oleg


1 Answers

Media queries are not selectors, they are special language constructs. You can't combine them with anything other than media queries. You will have to use a mixin to do what you're asking for because extends don't work across media queries either.

@mixin vertical-form {
    label {
        display: block;
        float: none;
    }
}

@media screen and (max-width: 759px) {
    form {
        @include vertical-form;
    }
}

.vertical-form {
    @include vertical-form;
}
like image 130
cimmanon Avatar answered Jan 16 '23 19:01

cimmanon