Hide div element when screen size is smaller than a specific size

前端 未结 9 2161
别跟我提以往
别跟我提以往 2020-12-04 17:41

I have a div element that I want to hide when the width of the browser is less than or equal to 1026px. Is this possible to do with the css: @media only scr

相关标签:
9条回答
  • 2020-12-04 18:13

    You simply need to use a media query in CSS to accomplish this.

    @media (max-width: 1026px) {
        #fadeshow1 { display: none; }
    }
    

    Unfortunately some browsers do not support @media (looking at you IE8 and below). In those cases, you have a few options, but the most common is Respond.js which is a lightweight polyfill for min/max-width CSS3 Media Queries.

    <!--[if lt IE 9]>
        <script src="respond.min.js"></script>
    <![endif]-->
    

    This will allow your responsive design to function in those old versions of IE.
    *reference

    0 讨论(0)
  • 2020-12-04 18:14

    if you are using bootstrap u can just use the hidden-sm ( lg or md or xs) depending on what u want. u can then go into the css file and specify the percentages u want it to show on. in the sample below it will be hiding on large screens, medium ones and extra small ones but show on small screens by taking half of the screen.

    <div class="col-sm-12 hidden-lg hidden-md hidden-xs">what ever you want</div>
    
    0 讨论(0)
  • 2020-12-04 18:18

    This should help:

    if(screen.width<1026){//get the screen width
       //get element form document
       elem.style.display == 'none'//toggle visibility
    }
    

    768 px should be enough as well

    0 讨论(0)
  • 2020-12-04 18:20
    @media only screen and (min-width: 1140px)
    

    should do his job, show us your css file

    0 讨论(0)
  • 2020-12-04 18:23

    The easiest approach I know of is using onresize() func:

       window.onresize = function(event) {
            ...
        }
    

    Here is a fiddle for it

    0 讨论(0)
  • 2020-12-04 18:25

    You can do this with CSS:

    @media only screen and (max-width: 1026px) {
        #fadeshow1 {
            display: none;
        }
    }
    

    We're using max-width, because we want to make an exception to the CSS, when a screen is smaller than the 1026px. min-width would make the CSS rule count for all screens of 1026px width and larger.

    Something to keep in mind is that @media queries are not supported on IE8 and lower.

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