Hide an element when the screen is in a certain pixel in Javascript

Asked

Viewed 59 times

0

Friends, I would like to hide an element of the page when the screen is on a certain pixel! I got in this format, but when I open screen on the given pixel, the page Showing the element, the element only hides when I set the screen size dragging with the mouse.

<html lang="en">
  <head>
    <title>Teste JS</title>
    <script>
      window.onresize = function() {
        var w = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
        var x = document.getElementById("teste");
        if (w < 375) {
          x.style.display = "none";
        }
      };
    </script>
  </head>
  <body>
    <select id="teste">
      <option value="">Teste</option>
      <option value="">Teste</option>
    </select> 
  </body>
</html>

1 answer

1


You could solve this using CSS. If for example your DIV has applied a class called "my_div", you could hide it in this way:

@media only screen and (max-device-width: 375px) {
.minha_div{display:none;}
}
  • It worked buddy! thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.