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 :(
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.
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.
For the purpose of hiding elements with media queries you simply have to set their display to none inside the specific media query.
Use media queries. Your CSS code would be:
@media screen and (max-width: 1024px) { .yourClass { display: none !important; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With