Last updated: Mar 4, 2024
Reading timeยท5 min
To hide an element after a few seconds:
setTimeout
method to invoke a function after a delay.style.display
property to none
.Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>bobbyhadz.com</title> </head> <body> <div id="box" style=" height: 150px; width: 150px; background-color: salmon; " > Box </div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
setTimeout(() => { const box = document.getElementById('box'); // ๐๏ธ removes element from DOM box.style.display = 'none'; // ๐๏ธ hides element (still takes up space on page) // box.style.visibility = 'hidden'; }, 1000); // ๐๏ธ time in milliseconds
We used the setTimeout() method to set a timer that invokes a function after a delay.
setTimeout
function takes is the delay after which we want to call the function (in milliseconds).The function in the example is invoked after 1000 ms
(one second).
The first step is to get access to the DOM element you want to hide after a
delay. We used the document.getElementById
method in the example.
To hide the element, we set its display
CSS property to none
. However, you might need the
visibility
property depending on your use case.
display
property is set to none
, the element is removed from the DOM and does not affect the layout. The document is rendered as though the element does not exist.On the other hand, when an element's visibility
property is set to hidden
,
it still takes up space on the page, however, the element is invisible (not
drawn).
The element still affects the layout on your page as normal.
Here is an example that uses the visibility
property to make the element
invisible after a few seconds.
setTimeout(() => { const box = document.getElementById('box'); // ๐๏ธ hides element (still takes up space on the page) box.style.visibility = 'hidden'; }, 1000);
Even though the element becomes invisible, it still takes up space on the page.
This is the difference between setting display
to none
and visibility
to
hidden
.
If you need to hide an element a few seconds after a button is clicked:
button
element.setTimeout
method to invoke a
function after a delay.display
property to none
.<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>bobbyhadz.com</title> </head> <body> <div id="box" style="height: 150px; width: 150px; background-color: salmon"> Box </div> <button id="btn">Hide Box after delay</button> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const btn = document.getElementById('btn'); btn.addEventListener('click', () => { setTimeout(() => { const box = document.getElementById('box'); // ๐๏ธ removes element from DOM box.style.display = 'none'; // ๐๏ธ hides element (still takes up space on page) // box.style.visibility = 'hidden'; }, 1000); });
We added an event listener on the button
element.
When the button is clicked, we wait one second before hiding the div
element.
To show an element after delay:
setTimeout()
method to invoke a function after a delay.visibility
property to visible
.setTimeout
method.Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <title>bobbyhadz.com</title> <meta charset="UTF-8" /> <style> #box { visibility: hidden; background-color: salmon; width: 100px; height: 100px; } </style> </head> <body> <div id="box">Some content here</div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const el = document.getElementById('box'); setTimeout(() => { el.style.visibility = 'visible'; // ๐๏ธ if you used `display` to hide the element // el.style.display = 'block'; }, 2000); // ๐๏ธ delay in milliseconds
We selected the element we wanted to show using the document.getElementById() method.
We used the setTimeout() method to invoke a function after a delay.
The first parameter we passed to the method is the function we want to invoke
and the second - the delay
in milliseconds.
In the example, we used 2000
ms as the delay, which is equal to 2
seconds.
visibility
property back tovisible
.Notice that we initially set the property to hidden
in the style
tag in our
HTML.
In the example, we used the visibility property to hide and show the element, however, you might need to use the display property if that's what you used to hide the element initially.
display
property is set to none
, the element is removed from the DOM and has no effect on the layout. The document is rendered as though the element does not exist.On the other hand, when an element's visibility
property is set to hidden
,
it still takes up space on the page, however, the element is invisible (not
drawn). It still affects the layout of your page as normal.
Usually, when showing an element after a delay, you should use the visibility
property to avoid cumulative layout shift on the page, which can be confusing to
users.
Here is an example that uses the display
property to show an element after a
delay.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <style> #box { display: none; background-color: salmon; width: 100px; height: 100px; } </style> </head> <body> <div>One, two, three</div> <div id="box">Some content here</div> <div>One, two, three</div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const el = document.getElementById('box'); setTimeout(() => { el.style.display = 'block'; }, 2000); // ๐๏ธ delay in milliseconds
If you load the page, you'll see how when the element is hidden, it takes no space on the page and other elements take its space.
display
property to block
, it pushed down the content of the next elements.Had we used the visibility
property, the element would still take up space on
the page, even when it is hidden.
You can learn more about the related topics by checking out the following tutorials: