To hide text in HTML, you can use the hidden attribute, CSS display: none, visibility: hidden, or visually-hidden CSS depending on whether the text should be completely removed from the page, remain in the layout, or stay available to screen readers.
The simplest method is the HTML hidden attribute:
<p hidden>This text is hidden.</p>
The browser will not display the paragraph. When you need more control, CSS provides several other options.
1. Hide Text Using the HTML hidden Attribute
The hidden attribute is the simplest HTML-only method:
<p hidden>This text is hidden from the page.</p>
You can use it on many HTML elements:
<span hidden>Hidden text</span>
<div hidden>Hidden content</div>
<h2 hidden>Hidden heading</h2>
The element remains in the HTML document, but browsers normally do not render it.
You can also use JavaScript to show or hide the element by changing the hidden property:
<p id="message" hidden>This message is hidden.</p>
<button onclick="document.getElementById('message').hidden = false">
Show Text
</button>
The hidden attribute is useful when the content has a hidden/shown state that may be controlled dynamically.
2. Hide Text Using display: none
CSS display: none completely removes an element from the page’s visual layout:
<p class="hidden-text">This text is hidden.</p>
<style>
.hidden-text {
display: none;
}
</style>
The element still exists in the HTML and can be found in the DOM, but it does not take up space on the page.
You can also apply it directly:
<p style="display: none;">This text is hidden.</p>
For reusable styles, a CSS class is generally cleaner than inline CSS.
display: none is commonly used for content that should not currently be displayed, such as menus, messages, panels, and elements that are revealed by JavaScript.
3. Hide Text Using visibility: hidden
The visibility property provides a different result:
<p class="hidden-text">This text is hidden.</p>
<style>
.hidden-text {
visibility: hidden;
}
</style>
The text becomes invisible, but the element still occupies its original space in the layout.
For example:
<p>First paragraph</p>
<p class="hidden-text">Hidden paragraph</p>
<p>Third paragraph</p>
The second paragraph is not visible, but its space remains between the first and third paragraphs.
This makes visibility: hidden useful when you want to hide an element without changing the surrounding layout.
4. Hide Text With opacity: 0
You can also make text completely transparent:
<p class="hidden-text">This text is transparent.</p>
<style>
.hidden-text {
opacity: 0;
}
</style>
Unlike display: none, the element still occupies space. It can also continue to participate in interaction depending on the element and other CSS properties.
Because opacity: 0 makes content transparent rather than actually hiding or removing it, it is usually not the best choice when your goal is simply to hide text.
5. Hide Text From Sight but Keep It Available to Screen Readers
Sometimes you need text to be invisible visually while keeping it available to assistive technologies such as screen readers. This is different from using display: none.
A common CSS pattern is:
<span class="visually-hidden">
Additional information for screen reader users.
</span>
<style>
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
</style>
This technique is useful for accessibility. For example, a button might contain a visible icon while additional text explains its purpose to screen-reader users.
Do not use this technique simply to hide ordinary content from visitors. It is intended for content that should remain accessible to users who rely on assistive technology.
6. Hide Text When a Condition Is Met
CSS classes can also be used to control whether text is displayed.
<p id="text" class="hidden">This text is hidden.</p>
<style>
.hidden {
display: none;
}
.visible {
display: block;
}
</style>
JavaScript can then switch the class:
<button onclick="document.getElementById('text').classList.toggle('hidden')">
Show/Hide Text
</button>
This approach is useful for interactive interfaces where text needs to be shown and hidden without reloading the page.
Which Method Should You Use?
The correct method depends on what you want to happen to the element.
| Method | Visible? | Takes up space? | Main use |
|---|---|---|---|
hidden | No | No | Simple HTML hiding |
display: none | No | No | Completely hide an element visually |
visibility: hidden | No | Yes | Hide while preserving layout |
opacity: 0 | No | Yes | Make an element transparent |
| Visually-hidden CSS | No visually | Usually no | Keep text available to screen readers |
For basic HTML hiding, use:
<p hidden>Hidden text</p>
For CSS-controlled hiding, use:
.hidden {
display: none;
}
For hiding text while preserving its layout space, use:
.hidden {
visibility: hidden;
}
For accessibility-specific text that should be invisible but still available to screen readers, use a properly implemented visually-hidden class.
Can You Hide Text Using the Same HTML Element?
Yes. You do not need to change the HTML element just because you want to hide its text. For example:
<h2 hidden>Hidden heading</h2>
<p hidden>Hidden paragraph</p>
<span hidden>Hidden span</span>
The hiding method can generally be applied to the element containing the text.
Important Difference Between Hiding and Removing Text
Hiding text does not necessarily remove it from the HTML source or DOM.
For example:
<p style="display: none;">This text is hidden.</p>
The text is still present in the HTML and can be accessed through browser developer tools or JavaScript.
If you remove the element entirely from the HTML, it is no longer part of the document. Hiding and removing are therefore different operations.
Also, hiding text should not be used to manipulate search engines or deceive users. Content should be hidden for legitimate interface, state-management, design, or accessibility reasons.
Conclusion
The simplest way to hide text in HTML is to use the hidden attribute:
<p hidden>This text is hidden.</p>
Use display: none when CSS should completely hide the element and remove it from the visual layout. Use visibility: hidden when the element should remain in the layout while being invisible. For accessibility, use a visually-hidden CSS technique when text needs to remain available to screen readers but should not appear visually.
The right method depends on whether you want to remove the element from the layout, preserve its space, make it transparent, or keep the information accessible to assistive technologies.
Also Read More HTML Guide
What Are Tags In Websites
What Does Hr Stand For In Html
What Is The Difference Between Css And Html
How To Add An Image From A Folder In Html
How To Add Expires Headers In Html Catswhocode
How To Copy Html Code From A Website
How To Create A Banner In Html
How To Insert A Horizontal Line In Html
What Is The Correct Html For Adding A Background Colour
How To Convert Html To Powerpoint
How To Convert Ipynb To Html
How To Link An Email In Html
How To Open A Link In A New Tab Html
How To Upload Html File To WordPress
How To Add Schema Markup To Html
How To Call A Function In Javascript From Html
How To Center An Iframe In Html
How To Change Plain Text Email To Html
How To Make Text Wrap Around An Image In Html
Are Html And Css Programming Languages
Do My Html Homework
How To Add Alt Tags To Images In Html
