Add class or other attribute using media query

前端 未结 5 1039
时光取名叫无心
时光取名叫无心 2021-02-13 18:22

I\'m currently using CSS media queries to target some small/medium screen devices like this:

@media screen and (min-device-width: 480px) {
  ...
}
@media screen          


        
相关标签:
5条回答
  • 2021-02-13 18:36

    It cannot be done with plain CSS.

    Have a look at LESS or SASS. They are designed to make working with CSS more dynamic and flexible.

    • http://lesscss.org
    • http://sass-lang.com

    Once you use them. You can't live without them.


    In case you want to add a existing class selector to the body on certain resolutions.

    SASS

    .some-style { background-color:red; }
    
    @media screen and (min-device-width: 480px) {
        body {
            @extend .some-style;
        }
    }
    
    0 讨论(0)
  • 2021-02-13 18:45

    A JavaScript solution

    if (window.matchMedia("(min-device-width: 480px)").matches {
        // modify dom
    }
    
    0 讨论(0)
  • 2021-02-13 18:53

    Check out Enquire.js. Lightweight, pure JavaScript library for handling media queries. Looks pretty cool.

    0 讨论(0)
  • 2021-02-13 18:54

    For this also can help Modernizr - can detect even if svg enabled or other features is enabled.

    http://modernizr.com/

    0 讨论(0)
  • 2021-02-13 19:02

    take a look at conditionizr this could be what your looking for http://conditionizr.com/

    0 讨论(0)
提交回复
热议问题