Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fancy Media Queries with some LESS Magic

It would be nice to wrap css-styles for different resolutions within some css-classes using less.

I'd like to do something like:

footer {   width: 100%; }  .tablet {   footer {     width: 768px;   } }  .desktop {   footer {     width: 940px;   } } 

At the end something like this should be the result:

footer {   width: 100%; }  @media screen and (min-width: 768px) {   footer {     width: 768px;   } }  @media screen and (min-width: 992px) {   footer {     width: 940px;   } } 

.tablet could look somehow like this:

@media screen and (min-width: 768px) {   .tablet {    } } 

Hope somebody has a nice idea!

like image 447
Andre Zimpel Avatar asked Apr 05 '13 15:04

Andre Zimpel


People also ask

How do I use media query in less?

As you can see, you can have multiple conditions for the same media query. In order to test this, you need to resize your window so that it has a lower width. This can be done by pressing F12 in your browser. If you are using Chrome, a new area should appear, either on the side, bottom, or as a separate window.

Is media query still used?

And even though media queries are still a valid tool to create responsive interfaces, there are many situations where it's possible to avoid using width at all. Modern CSS allow us to create flexible layouts with CSS grid and flex that adapts our content to the viewport size without a need to add breakpoints.

What is a media query in its simplest description?

Media queries are a key part of responsive web design, as they allow you to create different layouts depending on the size of the viewport, but they can also be used to detect other things about the environment your site is running on, for example whether the user is using a touchscreen rather than a mouse.


1 Answers

Here is what I've done in my projects:

@desktop:   ~"only screen and (min-width: 960px) and (max-width: 1199px)"; @tablet:    ~"only screen and (min-width: 720px) and (max-width: 959px)";  @media @desktop {   footer {     width: 940px;   } }  @media @tablet {   footer {     width: 768px;   } } 

This allows you to only define your media queries once and you can use it throughout your less files. Also a little easier to read. :)

like image 193
Hai Nguyen Avatar answered Oct 11 '22 20:10

Hai Nguyen