CSS Hide Element: A Step-By-Step Guide (2024)

You can hide an element in CSS using the CSS properties display: none or visibility: hidden. display: none removes the entire element from the page and mat affect the layout of the page. visibility: hidden hides the element while keeping the space the same.

You may encounter a scenario where you want to hide an element on the web page. For instance, you may have a block of text that you want to remain hidden and appear when a user clicks a button.

Find your bootcamp match

GET MATCHED

By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunitiesfrom Career Karma by telephone, text message, and email.

X

By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

CSS Hide Element

There are two main approaches for hiding an element in CSS, using:

  • The display: none attribute.
  • The visibility: hidden attribute.

The first approach may affect the layout of your web page. But, the second method is nonetheless popular.

CSS Hide Element: display

The display property controls how an element is displayed on a web page. Every element in an HTML document has a default value for the display property, although that value depends on the element. For most elements, the default display value is either block or inline.

If you don’t want an element to display on an element at all, you can set the value of the display property to none.

The following style rule hides an element on a web page:

display: none;

When you set the value of display to none, the affected element will disappear. This means the element will no longer take up any space on the web page.

display: none Example

Suppose we are designing an About Us web page for a local cookery club. In our initial design, we added an image of a cake to the page. Now the club wants us to hide the image because they are not sure about the final design.

We could hide the image on the page using the following code:

<html><div><h1>About Us</h1><img src="https://images.unsplash.com/photo-1518047601542-79f18c655718?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80" height="200" width="200" /><p>The Superstar Bakers Club, founded in 2014, is a community of passionate cookers and bakers from the Charleston, S.C. area. The club has over 100 members and meets each week to share ideas and experiment with new recipes.</p></div><style>img {display: none;}<style>

Click theCSS Hide Element: A Step-By-Step Guide (1) button in the code editor above to see the output of our HTML/CSS code.

We defined three elements within a HTML <div> tag. The <h1> element is used to create our header, and contains the text About Us. Our HTML <img> element creates an image of a cake, which is 200px tall and 200px wide. The <p> element contains a short description of the club.

In our CSS code, we use the “img” selector to select all image tags on our page. This rule sets the display property of all <img> tags to none. Or, in other words, the style rule hides our image.

Although our image is hidden, it still exists on our web page. If we want to bring our image back, we can either delete the display:none; style. Or, we can specify another display style (like block or inline).

You could define this rule as an inline HTML attribute:

<img style=”display: none;” src=”https://images.unsplash.com/photo-1518047601542-79f18c655718?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80″ height=”200″ width=”200″ />

To learn more about inline HTML and CSS properties, check out our inline CSS guide.

CSS Hide Element: visibility

The CSS visibility property is used to control whether an element is visible on the web page.

By default, the value of the visibility property is visible. However, if you want to make an image invisible, you can set the value of visibility to hidden.

The following style rule sets the visibility of an element to hidden:

visibility: hidden;

Let’s return to the cooking club example from earlier. Suppose we want to hide the image of the cake on our About Us web page. We could do so using this code:

<html><div><h1>About Us</h1><img src="https://images.unsplash.com/photo-1518047601542-79f18c655718?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80" height="200" width="200" /><p>The Superstar Bakers Club, founded in 2014, is a community of passionate cookers and bakers from the Charleston, S.C. area. The club has over 100 members and meets each week to share ideas and experiment with new recipes.</p></div><style>img {display: visibility;}<style>

Click theCSS Hide Element: A Step-By-Step Guide (2) button in the code editor above to see the output of our HTML/CSS code.

Our HTML code is the same in this example. In our CSS file, instead of using the display property, we used visibility to hide our element. As you can see above, our image has been hidden from the web page.

While our image is gone, there is now a space between our header and our paragraph where the image would have been placed. This is a feature of the visibility: hidden; style. The space of the original element is still kept by the web page. But, the element is hidden.

CSS Visibility vs. Display

The two methods we have discussed of hiding an element appear to be the same, but there is a difference between the two.

The display: none rule removes an element from an HTML document. While the code for the hidden element is still present, the element itself will not be displayed.

The visibility: hidden rule, on the other hand, hides an element, but the element will still take up space on the web page. If you want to hide an element but keep that element’s space on the web page, using the visibility: hidden; rule is best.

CSS Hide Element: A Step-By-Step Guide (3)

"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"

Venus, Software Engineer at Rockbot

Conclusion

The display: none rule removes an element from a document and hides it from view. The visibility: hidden rule hides an element on a document and leaves the space in which the element would have appeared empty.

Do you want to learn more about CSS? Read our How to Learn CSS guide. This guide is full of actionable advice on how to learn CSS. You’ll also find a list of top resources you can use to advance your knowledge.

CSS Hide Element: A Step-By-Step Guide (2024)

FAQs

How to hide an element with CSS? ›

The easiest method of hiding an element is to remove it entirely. The display:none property does just that. It removes whatever element you attach it to completely. That piece of the page will simply not render anymore, and the space it takes up on the page will be removed and the layout readjusted.

How do you visually hide an element in CSS? ›

CSS opacity and filter properties

The opacity or filter: opacity properties can be useful for visually hiding an element by controlling its level of opacity (which is the opposite of transparency). For example, we can specify opacity: 0 or filter: opacity(0) to hide an element.

How do you hide an element in CSS without affecting the layout? ›

In CSS, the visibility: hidden property is used to hide an element without affecting the layout of the webpage. When an element is set to visibility: hidden, it becomes invisible but still takes up space on the webpage.

How do you hide elements off screen in CSS? ›

Offscreen
  1. Remove all borders and padding and hide the element's overflow.
  2. Use clip to define that no part of the element is shown.
  3. Make the height and width of the element 1px and negate them using margin: -1px .
  4. Use position: absolute so that the element does not take up space in the DOM.
Oct 11, 2021

How to create a hidden element in HTML? ›

The first is via inline CSS, where you add a “style” attribute with “display: none;” in your HTML element. The second method is with external or internal CSS. In your CSS file or <style> tag, target the element you want to hide using a class, id, or any selector, and set “display: none;” for that selector.

How do you hide an element in display? ›

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”.

How do I hide and show property in CSS? ›

The visibility CSS property shows or hides an element without changing the layout of a document. The property can also hide rows or columns in a <table> .

How do I hide a figure in CSS? ›

To hide an image in CSS, you can use the display property and set it to none . This will effectively hide the image from the webpage.

How do you hide an element in CSS print? ›

The media query is used to hide an element when printing web pages. Use @media print query and set the visibility hidden to that element that needs to hide at printing. Example 1: In this example, hide the element h1 at printing time. To hide the element h1 use media query and set visibility:hidden.

How do I hide and show a button in CSS? ›

How to hide the button after it is clicked
  1. Use the SASS/CSS :focus or active element to activate a display: none code (requires some trial and error) .conditional-btn:active{ display: none; }
  2. Use a non-persistent or persistent entity with a boolean that is switched when the button is pressed.
Jun 28, 2022

How do I hide an empty element in CSS? ›

A tiny tip today, but a good one: use the :empty pseudo-class to hide pesky empty elements (commonly found in user-generated content). This will hide paragraph elements that have no children, or contain no text nodes.

How to hide an item in CSS? ›

By setting the value of display to "none", the element will be hidden and won't take up any space on the page.

How do you hide from display in CSS? ›

You can do this in different ways. Two common approaches include using the display property with a none value or the visibility property with a hidden value. Although both approaches hide the element visually, they cause the element to behave in different ways.

What is the opposite of visibility hidden? ›

The opposite of visibility: hidden is visibility: visible .

How do you make an element disappear in CSS? ›

The CSS height , width and overflow properties may be used to hide elements. For example, setting height: 0 , width: 0 and overflow: hidden on an element will effectively hide it from view.

How to hide specific text in CSS? ›

If you only need to hide the text temporarily, then using the display: none property is the simplest option. If you need to hide the text permanently, then using the visibility: hidden property is a better choice.

Top Articles
What is a Personal Budget and How to Make One
How to Create a Simple Personal Budget That Works | Simplifi by Quicken
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Pearson Correlation Coefficient
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
Latest Posts
Article information

Author: Chrissy Homenick

Last Updated:

Views: 6154

Rating: 4.3 / 5 (74 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Chrissy Homenick

Birthday: 2001-10-22

Address: 611 Kuhn Oval, Feltonbury, NY 02783-3818

Phone: +96619177651654

Job: Mining Representative

Hobby: amateur radio, Sculling, Knife making, Gardening, Watching movies, Gunsmithing, Video gaming

Introduction: My name is Chrissy Homenick, I am a tender, funny, determined, tender, glorious, fancy, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.