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

JavascriptHtmlCss

Javascript Problem Overview


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 screen and (min-width: 1140px) {} If it isn't possible with css, Is there any alternative?

Extra info: When the div element is hidden, I don't want a blank white gap. I'd like the page to flow as it would if I deleted the div element entirely from the code.

The div I am hiding is <div id="fadeshow1"></div>.

HTML5 Doctype.

I used javascript to place a gallery into that div.


I want it to look like this when it is bigger than 1026px width: http://i.stack.imgur.com/REBPi.png


I want it to look like this when it is less than 1026px width: http://i.stack.imgur.com/XdiL4.png

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

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

Any time the screen is less than 1026 pixels wide, anything inside the { } will apply.

Some browsers don't support media queries. You can get round this using a javascript library like Respond.JS

Solution 3 - Javascript

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>

Solution 4 - Javascript

I don't know about CSS but this Javascript code should work:

    function getBrowserSize(){
       var w, h;

         if(typeof window.innerWidth != 'undefined')
         {
          w = window.innerWidth; //other browsers
          h = window.innerHeight;
         } 
         else if(typeof document.documentElement != 'undefined' && typeof      document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) 
         {
          w =  document.documentElement.clientWidth; //IE
          h = document.documentElement.clientHeight;
         }
         else{
          w = document.body.clientWidth; //IE
          h = document.body.clientHeight;
         }
       return {'width':w, 'height': h};
}

if(parseInt(getBrowserSize().width) < 1026){
 document.getElementById("fadeshow1").style.display = "none";
}

Solution 5 - Javascript

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

Solution 6 - Javascript

@media only screen and (min-width: 1140px)

should do his job, show us your css file

Solution 7 - Javascript

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

Solution 8 - Javascript

You have to use max-width instead of min-width.

<style>
	@media (max-width: 1026px) {
		#test {
			display: none;
		}
	}
</style>
<div id="test">
	<h1>Test</h1>
</div>

Solution 9 - Javascript

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

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

Here is a fiddle for it

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrett MerrifieldView Question on Stackoverflow
Solution 1 - JavascriptCerbrusView Answer on Stackoverflow
Solution 2 - JavascriptAndyView Answer on Stackoverflow
Solution 3 - JavascriptKabengwa PatrickView Answer on Stackoverflow
Solution 4 - JavascriptNaijaProgrammerView Answer on Stackoverflow
Solution 5 - JavascriptJRulleView Answer on Stackoverflow
Solution 6 - JavascriptMircoView Answer on Stackoverflow
Solution 7 - JavascriptVaibhavView Answer on Stackoverflow
Solution 8 - Javascriptuser4051844View Answer on Stackoverflow
Solution 9 - JavascriptDeeDubView Answer on Stackoverflow