Remove additional elements depending on screen size in JavaScript
It was picking up null objects between the images for some reason.
Then in the loop things would be indexed differently after each item was removed so had to loop backwards.
Changed it to the following and it works the way I want:
var slides = document.getElementsByClassName("trustpilot_slides");
for (var k=slides.length-1; k>=14; k--) {
var this_slide = slides[k];
this_slide.parentNode.removeChild(this_slide);
}
Remove elements on based on window size
Removing the element completely is not the way that the industry is going - you might want to do that still, but please consider the counter-arguments:
- The element won't be showing back if the screen gets resized up over the threshold
- To remove the element after the user resizes down under the threshold, you must detect this behavior with JS, which complexifies the code
- The element might be useful to other parts of your code
The way that is widely adopted in the industry to change display based on size width is to use media queries. Here's a quick demo to show/hide elements based on the screen size (over or under 700px) - resize your window to make it work!
@media (max-width: 700px) { /* mobile CSS: hide .desktop div */ .desktop { display: none }}@media (min-width: 700px) { /* desktop CSS: hide .mobile div */ .mobile { display: none }}
<div class="mobile"> I appear only on mobile devices! (screen width less than 700)</div><div class="desktop"> I appear only on desktop devices! (screen width over 700)</div>
How to remove something in page in a certain width?
Try the @media rule in conjunction with max-width!
Your CSS might look like this:
@media (max-width:575px) {
.remove-me {
display: none;
}
}
Remove div content if width of site is less than x px
You can use media query to achieve this.
@media screen and (max-width: 1000px){
div{
display: none;
}
}
The above css snippet will hide the div if the width is less the 1000px.
Hide div element when screen size is smaller than a specific size
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.
Using JavaScript to delete div in screen size below a certain pixel
Here is a way to hide a div when the width of the screen is smaller then 700px
function myFunction(x) { if (x.matches) { // If media query matches document.getElementById("divIWantedToHide").style.visibility = "hidden"; } else { document.getElementById("divIWantedToHide").style.visibility = "visible"; }}
var x = window.matchMedia("(max-width: 700px)")myFunction(x) // Call listener function at run timex.addListener(myFunction)
<div id="divIWantedToHide">tset</div>
Related Topics
When to Use Rel="Preload"? Why Is Preloading Fonts/Fontawesome a Good Idea
Why Do My Icons Line Up Top-To-Bottom Instead of Flowing Left-To-Right in a Div Layout
How to Change The CSS of Only One Page in Wordpress
Don't Change Link Color When a Link Is Clicked
CSS: How to Make Left Float Div to Adjust Height Dynamically
Why Are These Two Inline-Blocks Not Aligned
CSS Selector: How to Style Items [1-2][5-6][9-10] etc by Pair
How to Make a Simple Modal Window
CSS Layout That Fills Available Space
Prevent Paragraph from Increasing The Width of a Floated Parent
Z-Index Not Working on Pseudo-Element
Custom Fields for Shopify User Registration Form
Compact Arrangement of Divs in Two Directions
How Do Make Ionic 4 Ion-Col The Same Height
Is It Easier/Faster to Evaluate CSS Selectors Ltr or Rtl
CSS Apply Styling to All Elements Except Those in The Last Row
Rails 4: How to Identify and Format Links, Hashtags and Mentions in Model Attributes