How do I dynamically adjust css stylesheet based on browser width?

前端 未结 4 1571
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 09:14

We\'re developing an open-source web app for arts teachers across the world to work together. We need a nice website that simply adjusts itself based on the active width of

4条回答
  •  无人及你
    2020-11-29 09:15

    You can either use CSS media queries, like so:

    
    
    
    

    Or jQuery, like so:

    function adjustStyle(width) {
        width = parseInt(width);
        if (width < 701) {
            $("#size-stylesheet").attr("href", "css/narrow.css");
        } else if ((width >= 701) && (width < 900)) {
            $("#size-stylesheet").attr("href", "css/medium.css");
        } else {
           $("#size-stylesheet").attr("href", "css/wide.css"); 
        }
    }
    
    $(function() {
        adjustStyle($(this).width());
        $(window).resize(function() {
            adjustStyle($(this).width());
        });
    });
    

    Both found from: http://css-tricks.com/resolution-specific-stylesheets/

提交回复
热议问题