How to hide an HTML code?

Brajagopal Tripathi
2 min readOct 4, 2023

--

Photo by Gabriel Heinzer on Unsplash

There are two ways to hide HTML code:

  1. Use HTML comments. HTML comments are enclosed in the following tags:

Anything between the `` tags will be ignored by the browser and will not be displayed on the page.

2. Use the hidden attribute. The hidden attribute is a boolean attribute that can be used to hide an HTML element from view. To use the hidden attribute, simply add it to the element that you want to hide. For example:

HTML

<div hidden>This element is hidden</div>

When the browser encounters an element with the hidden attribute, it will not display the element on the page.

Both of these methods will hide the HTML code from view, but they will not hide it from screen readers. Screen readers can still read the hidden HTML code and announce it to the user.

If you need to hide HTML code from both the user and screen readers, you can use a JavaScript function to hide the code after the page has loaded.

For example, the following JavaScript code will hide all of the elements with the hidden class:

JavaScript

document.querySelectorAll('.hidden').forEach(element => {
element.style.display = 'none';
});

This code will hide the elements on the page, but it will not remove them from the HTML code. This means that the elements will still be accessible to screen readers.

It is important to note that hiding HTML code can have negative consequences for your website. For example, hiding important HTML code can make your website difficult to navigate or use. It is also important to note that hiding HTML code can make your website less accessible to people with disabilities.

If you are unsure whether or not you should hide HTML code on your website, it is best to consult with a web developer or accessibility expert.

--

--

Brajagopal Tripathi

Student of Computer Application and Network Administration || Cloud Technology and Cyber Security Enthusiast