conditional statement for screen resolution?

后端 未结 3 496
慢半拍i
慢半拍i 2020-12-31 12:36

I would like to use a conditional statement to attach a different stylesheet for:

if the clients resolution is <= 1024*768

I\'m pretty sure t

3条回答
  •  情书的邮戳
    2020-12-31 13:11

    Typically people don't "attach another stylesheet" for screen resolution because you could resize the browser after page load, changing the resolution, and you don't want file loading every time you do.

    This will do the trick, in one CSS file:

    Ex:

    /* css as usual */
    .this-class { font-size: 12px; }
    
    /* condition for screen size minimum of 500px */
    @media (min-width:500px) {
    
      /* your conditional / responsive CSS inside this condition */
    
      .this-class { font-size: 20px; }
    
    }
    

    This should change the font size to 20px when the @media query condition is true, in this case when the screen is over 500px.

    As you size your browser up and down you will see the conditional CSS rules take effect automatically, no JS needed.

提交回复
热议问题