Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide div on load of webpage

I have a basic script which shows/hides a div. I'm using this for a drop-down menu.

https://www.w3schools.com/howto/howto_js_toggle_hide_show.asp

I'm looking for the div element to be hidden when the page loads instead of it showing and having to click it to toggle it.

Any help is appreciated!

like image 478
Max Avatar asked Oct 28 '25 08:10

Max


2 Answers

Use display: none in div like below

<div id="myDIV" style="display:none">
  This is my DIV element.
</div>

or you can create a class and assign to the div.

<style>
.hide{
   display:none;
}
</style>


<div id="myDIV" class="hide">
  This is my DIV element.
</div>
like image 122
Karthik Sekar Avatar answered Oct 29 '25 22:10

Karthik Sekar


Instead of CSS, you may also use JavaScript to manipulate the display of a web page by taking advantage of events, such as onload.

window.onload = function(){
  document.getElementById("myDIV").style.display='none';
};
<div>
  This is the first DIV element.
</div>

<div id="myDIV">
  This is the 2nd DIV element.
</div>

<div>
  This is the last DIV element.
</div>
like image 30
slevy1 Avatar answered Oct 29 '25 21:10

slevy1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!