How to Hide HTML Code

Posted in: Computers - HTML

There are a few ways to "hide" HTML.

  1. make the HTML element invisible
  2. make the HTML element invisible AND take up no space
  3. never create the HTML element in the first place

An Example:

Lets say you have a piece of HTML code that you want to be hidden. For instance:

<p>This paragraph will be hidden from view</p>

There are three ways you can "hide" this code.

Make It Invisible

<p style="visibility: hidden;">This paragraph will be hidden from view</p>

It's important to note that the paragraph element will still be rendered by the browser.  This means the pargraph element will take up the same amount of space, it will just be invisible.

Display Nothing

<p style="display: none;">This paragraph will be hidden from view</p>

Use this way of hiding HTML if you don't want the element to be displayed at all (invisible and takes up no space).

Do Not Create the HTML Element

<!-- We are commenting out this part of the code. It will not be displayed
<p>This paragraph will be hidden from view</p> -->

In the previous two examples the HTML structure remained exactly the same.  CSS was the reason the two examples were displayed differently.  In this third example, that is not the case.  In this example, we are completely eliminating the HTML element.  The browser considers this code to be only a comment.

Hide HTML Source Code

If you are looking for a way to hide HTML source code from the user (like if they right click and 'view source'), you're out of luck. There's always a way to get access to it. For instance, you could use wget or run the URL through an HTML validator. Pathetic attempts to stop source code view (like javascript disable right click) will only make legitimate users angry.