less.js icon indicating copy to clipboard operation
less.js copied to clipboard

@media queries should behave like nested rules

Open tylertate opened this issue 16 years ago • 7 comments

I've been working closely with @media queries for a new version of the 1KB CSS Grid that will be written in LESS.js, but I have noticed two crucial inconsistencies in how @media queries are currently being handled:

You shouldn't need to define a selector within a media query, but should be able to specify CSS attributes directly. For instance:

header {
    width: 50%;

    @media (max-width: 720px) {
        width: 100%;
    }
}

Secondly, @media queries in LESS.js should be aware of their position within other nested rules. For example:

article {
    header {
        width: 50%;

        @media (max-width: 720px) {
            &:hover {
                width: 100%;
            }
        }
    }
}

Should result in:

article header:hover { width: 100%; }

But unfortunately, it currently results in:

header:hover { width: 100%; }

I am more than happy to be a champion for @media query support in LESS, so please let me know if there's anything else I can do to help!

tylertate avatar Nov 28 '10 14:11 tylertate

I'm not sure I follow, you can specify attributes directly:

 @media (max-width: 720px) { width: 100% }

results in in the exact same CSS.

Regarding your 2nd point, you aren't supposed to put @media queries inside other elements, because it would lead to invalid css, ie:

header { @media {} }

would result in:

header @media {}

which is invalid.

cloudhead avatar Dec 02 '10 20:12 cloudhead

When using @media queries, you could NOT normally write this (because there is no selector):

@media (max-width: 720px) {
    width: 100%;
}

Instead, you would have to write:

@media (max-width: 720px) {
    header {
        width: 100%;
    }
}

What I'm calling for is that LESS.js be smart about how it handles @media queries. Following the normal rules, yes, this would result in invalid syntax:

header { @media { width: 100%; } }

But what I'm suggesting is that LESS.js know to handle @media queries differently from other selectors so that the above would actually compile as:

@media {
    header {
        width: 100%;
    }
}

This would make @media queries much easier to work with. At the moment, they conflict with LESS's principle of nested selectors, but this new behavior would be revolutionary in terms of making fluid layouts really easy to create.

tylertate avatar Dec 02 '10 21:12 tylertate

Sass does it this way, and it works pretty well.

jacobrask avatar Aug 18 '11 20:08 jacobrask

gotcha, will look into it.

cloudhead avatar Jan 04 '12 08:01 cloudhead

Yes, SASS does this and it's fantastic.

dmackerman avatar Jan 05 '12 01:01 dmackerman

Consider this another "me too" on this one. SASS calls it "@media Bubbling."

Here's the case I'm thinking of:

.selector {
    p {
        font-size: 12px;
    }
}

Obviously, that produces:

.selector p { font-size: 12px; }

But if I want to change the font size of .selector p when we reach a particular width, I currently do:

.selector {
    p {
        font-size: 12px;
    }
}

@media (min-width: 1200px) {
    .selector {
        p {
            font-size: 14px;
        }
   }
}

This produces a lot of repeated CSS and puts selectors in different parts of the document and is, in general, not very DRY. It's a maintenance headache waiting to happen.

What I'd like to be able to do is this:

.selector {
    p {
        font-size: 12px;
        @media (min-width: 1200px) {
            font-size: 14px;
        }
    }
}

It's similar to using "&" combinator in a nested rule. Both of the last two code blocks would compile to:

.selector p { font-size: 12px; }
@media (min-width: 1200px) { .selector p { font-size: 14px; } }

davesimon avatar Feb 02 '12 18:02 davesimon

this is implemented in less now I believe.

lukeapage avatar Jul 17 '12 22:07 lukeapage