Friday, September 5, 2025
HomeLanguagesJavascriptHide or show HTML elements using visibility property in JavaScript

Hide or show HTML elements using visibility property in JavaScript

The visibility property is used to hide or show the content of HTML elements. The visibility property specifies that the element is currently visible on the page. The ‘hidden’ value can be used to hide the element. This hides the element but does not remove the space taken by the element, unlike the display property.

Syntax:

element.style.visibility = 'hidden';
element.style.visibility = 'visible';

Example: 

html




<!DOCTYPE html>
<html>
 
<head>
 
    <style>
        .container {
            height: 80px;
            width: 250px;
            border: 2px solid black;
            background-color: green;
            color: white;
        }
    </style>
</head>
 
<body>
    <div class="container">
        <h1>neveropen</h1>
    </div>
 
     
<p>
        Click the buttons to show or hide the green box
    </p>
 
 
    <button onclick="showElement()">
            Show Element
    </button>
 
    <button onclick="hideElement()">
            Hide Element
    </button>
 
    <script type="text/javascript">
        function showElement() {
            element = document.querySelector('.container');
            element.style.visibility = 'visible';
        }
 
        function hideElement() {
            element = document.querySelector('.container');
            element.style.visibility = 'hidden';
        }
    </script>
</body>
 
</html>


Output: 
 

 

 

RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6638 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11866 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7027 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS