The Wayback Machine - https://web.archive.org/web/20240723192451/https://www.geeksforgeeks.org/hide-or-show-elements-in-html-using-display-property/
Open In App

Hide elements in HTML using display property

Last Updated : 28 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

The style display property is used to hide or show the content of HTML DOM by accessing the DOM element using JavaScript / jQuery .

  • To hide an element, set the style display property to “none”.
  • Visibility Toggle effectively hides the element, making it invisible on the webpage. To make it visible again, adjust the display property as needed.

As you explore advanced techniques like using the display property in JavaScript to manipulate HTML elements , consider diving deeper into full-stack web development . There is a comprehensive Full Stack Web Development course designed for individuals looking to expand their frontend and backend development skills.

Syntax:

document.getElementById("element").style.display = "none";

As you explore advanced techniques like using the display property in JavaScript to manipulate HTML elements, consider diving deeper into full stack web development . GeeksforGeeks offers a comprehensive Full Stack Web Development course tailored for individuals looking to expand their frontend and backend development skills.

Steps to hide the element :

  • Create some div and assign them an ID or class and then add styling to it.
  • Width and height will set the width and height of the content, border-radius 0% will make a square border, 50% will make a circle and 25% will make a rounded shape and float will make the divs get positioned, margin-right will make them separated with a space at right.
  • Background-color will set the background color of the div.
  • The document.getElementById will select the div with given id.
  • The style.display = “none” will make it disappear when clicked on div.

Example: In this example we will see the implementation of style.display property.

HTML
<!DOCTYPE html>
<html>
<head>

    <title>JavaScript</title>

    <style type="text/css">
        .circle {
            width: 130px;
            height: 130px;
            border-radius: 50%;
            float: left;
            margin-right: 50px;
        }        
        .rounded {
            width: 130px;
            height: 130px;
            border-radius: 25%;
            float: left;
            margin-right: 50px;
        }
        .square {
            width: 130px;
            height: 130px;
            border-radius: 0%;
            float: left;
            margin-right: 50px;
        }        
        #circle {
            background-color: #196F3D;
        }
        #rounded {
            background-color: #5DADE2;
        }        
        #square {
            background-color: #58D68D;
        }
    </style>
</head>

<body>

    <div class="circle" id="circle"></div>
    <div class="rounded" id="rounded"></div>
    <div class="square" id="square"></div>

    <script type="text/javascript">
        document.getElementById("circle").onclick = function() {
            document.getElementById("circle").style.display = "none";
        }

        document.getElementById("rounded").onclick = function() {
            document.getElementById("rounded").style.display = "none";
        }

        document.getElementById("square").onclick = function() {
            document.getElementById("square").style.display = "none";
        }
    </script>
</body>
</html>

Output:

Hide elements in HTML using display property

Hide elements in HTML using display property

HTML is the foundation of webpages and is used for webpage development by structuring websites and web apps. You can learn HTML from the ground up by following this HTML Tutorial and HTML Examples .



Previous Article
Next Article

Similar Reads

How to Hide Elements in Mobile Size and Display in Laptop size using Tailwind CSS ?
Tailwind CSS is a popular utility-first CSS framework that provides a variety of built-in classes for rapid web development. One common requirement is to hide elements on mobile devices while keeping them visible on larger screens, such as laptops or desktops. In this article, we will see two different approaches for hiding elements on mobile devic
3 min read
How to Display/Hide functions using aria-hidden attribute in jQuery ?
The 'aria-hidden' attribute plays an important role in context of web-accessibility. It is a simple way to make web content/applications more accessible to people with disabilities. This attribute is used to indicate that the element and all of its descendants are not visible or perceivable to any user as per the implementation. Now in your mind ma
2 min read
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
1 min read
How to display icon next to show/hide text on button ?
In this article, we will learn how to add icons with the show/hide text button and also you can use that icon as a button. It will be done here by using JavaScript. In this example, with the help of JavaScript, you can easily toggle between show and hide. Add any icons beside the button or you can use icons as a button also. Here you just need to l
2 min read
What is the difference between display: inline and display: inline-block in CSS?
The display property in CSS is a very useful and commonly used property that contains many values. The display property defines how an HTML element should be displayed on the webpage. The property also specifies the type of box used for an HTML element and how it should be laid out on the page. If we need to display the elements that are laid out a
4 min read
How display: inline is different from display: inline-block in CSS ?
In this article, we will know about the display property in CSS, along with understanding the 2 different property values for display property, i.e., display: inline &amp; display: inline-block properties, &amp; will understand their basic differences &amp; implementation through the examples. The display property facilitates setting the element by
3 min read
What is the difference between display:inline-flex and display:flex in CSS ?
In this article, we will see the display property &amp; its values, i.e. the inline-flex &amp; flex, in order to specify the display behavior of an element, along with understanding their basic differences &amp; will illustrate them through the examples. Both CSS display: inline-flex and display: flex properties are used to create flexible layouts.
3 min read
Hide the Email Id in a form using HTML and JavaScript
Here the following task is going to be formed with HTML code: 1.Input field: We are going to enter the email id in this field. 2.Button: On clicking this button hidden email id will be displayed. 3.Output field: Field to display hidden email id. Examples: Input :robin_singh@gmail.com Output :robin*****@gmail.com Input :geeksforgeeks@gmail.com Outpu
1 min read
How to Hide an HTML Element by Class using JavaScript ?
Suppose you have given an HTML document and the task is to hide an HTML element by its class name with the help of JavaScript. There are two approaches to explain with the proper example. Approach 1: In this approach, getElementsByClassName() selector is used to select elements of specific class. Indexing is used to get the element at respective in
2 min read
How to Hide an HTML Element in Mobile View using jQuery ?
Suppose we have given an HTML document and the task is to hide an HTML element in mobile view with the help of jQuery. Approach: Initially, we are required to detect whether our system is 'Mobile' or not, for that window.navigator.userAgent property is used. It returns a string containing information about the name, version, and platform of the bro
2 min read
three90RightbarBannerImg