How can I use media queries more efficiently with LESS?

我只是一个虾纸丫 提交于 2019-12-22 12:37:14

问题


I'm working with Bootstrap and LESS on a responsive webpage design.

One of the reasons I have enjoyed LESS in the past is because it can keep all attributes for HTML elements together.

What I have below is some rules to define a .sponsors block, and then one rule that applies to an element inside that block when the viewport is >= 768px

I don't like how that one rule requires a lot of extra code, and how that rule is apart from the rest of the rules. It also feels wrong.

What is a better way to do this / organize this? Do I need to start out by breaking everything down into top level @media groups?

.sponsors
{
    li
    {
        .thumbnail
        {
            padding-top:20px;
            padding-bottom:15px;
            img
            {
                display:block;
                margin:0 auto;
            }
        }
    }
}


@media (min-width: 768px)
{

    .sponsors
    {
        li
        {
            .thumbnail
            {
                padding-bottom:0px!important;
            }
        }
    }
}

It would be pretty sweet if there was something like:

.thumbnail
{
    &[@mediaWidth >=768]
    {
    padding-bottom:0px!important;
    }
}

回答1:


I think you can nest media queries and LESS (>1.3.0) will 'bubble' them to the root of your stylesheet during compilation.

.sponsors
{
    li
    {
        .thumbnail
        {
            padding-top:20px;
            padding-bottom:15px;

            @media (min-width: 768px) {
                padding-bottom:0px!important;
            }

            img
            {
                display:block;
                margin:0 auto;
            }
        }
    }
}


来源:https://stackoverflow.com/questions/13419298/how-can-i-use-media-queries-more-efficiently-with-less

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!