Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide div if screen is smaller than a certain width

I want to hide a floating div if the user screen is < 1024px as it will overlay with my blog content area. I found this jQuery on the net but I am not sure how to use it.

$(document).ready(function() {  if ((screen.width>1024)) {     // if screen size is 1025px wide or larger     $(".yourClass").css('display', 'none'); // you can also use $(".yourClass").hide(); } elseif ((screen.width<=1024))  {     // if screen size width is less than 1024px     $(".yourClass").css('display', 'block'); // here you can also use show(); } }); 

If my floating div class name is sharecontent, should I replace the above script like below? If yes, it's not working.

$(document).ready(function() {  if ((screen.width>1024)) {     // if screen size is 1025px wide or larger     $(".sharecontent").css('display', 'none'); // you can also use $(".yourClass").hide(); } elseif ((screen.width<=1024))  {     // if screen size width is less than 1024px     $(".sharecontent").css('display', 'block'); // here you can also use show(); } }); 

I also tried replacing the screen.width with window.width but still no success :(

like image 825
dzul Avatar asked Nov 28 '10 08:11

dzul


People also ask

How do I hide div on small screen?

To hide an element in a responsive layout, we need to use the CSS display property set to its "none" value along with the @media rule. The content of the second <p> element having a "hidden-mobile" class will be hidden on devices smaller than 767px.

How do I hide a div but keep the space?

The visibility: hidden rule, on the other hand, hides an element, but the element will still take up space on the web page. If you want to hide an element but keep that element's space on the web page, using the visibility: hidden; rule is best.

How do I hide a div in media query?

For the purpose of hiding elements with media queries you simply have to set their display to none inside the specific media query.


1 Answers

Use media queries. Your CSS code would be:

@media screen and (max-width: 1024px) {     .yourClass {         display: none !important;     } } 
like image 112
Eric Avatar answered Sep 30 '22 03:09

Eric