#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
HTML Fundamentals

HTML Computer Code

HTML has a small group of elements for showing computer-related text: <code> for pieces of code, <pre> for preformatted blocks, <kbd> for keyboard input, <samp> for program output and <var> for variables. They tell browsers, screen readers and search engines what kind of text this is, and browsers display most of them in a monospace font by default. You use them in documentation, tutorials, blog posts and help pages.

The five elements at a glance

ElementMeaningExample use
<code>A fragment of computer codeThe <code>map()</code> method
<pre>Preformatted text; keeps spaces and line breaksMulti-line code samples, ASCII art
<kbd>Text the user types or keys they pressPress Ctrl + S
<samp>Sample output from a programAn error message shown in a terminal
<var>A variable in maths or codeThe area is width times height

Inline code versus code blocks

<code> on its own is an inline element. Use it inside a sentence for short things like a function name, a file name or a property. It does not preserve line breaks, so multi-line code collapses into one line.

For longer snippets, wrap <code> inside <pre>. The <pre> element keeps every space, tab and newline exactly as typed, which matters for indentation. The <code> inside it keeps the meaning that the content is code. This combination is what most documentation sites and syntax highlighters use.

<p>Run <kbd>npm install</kbd> and wait for the message
  <samp>added 42 packages</samp>.</p>

<p>Press <kbd><kbd>Ctrl</kbd> + <kbd>C</kbd></kbd> to copy.</p>

<p>The area is <var>w</var> × <var>h</var>.</p>

<pre><code>function greet(name) {
  return `Hello, ${name}!`;
}</code></pre>

<p>Wrap text in a paragraph with <code>&lt;p&gt;</code>.</p>

Nesting <kbd> inside <kbd>, as in the copy shortcut above, is the documented way to describe a key combination made of individual keys.

All of these elements can be restyled. A common touch is giving <kbd> a light border and rounded corners so it looks like a physical key, and giving inline <code> a soft background colour so it stands out from the surrounding sentence.

The trap: showing HTML inside HTML

The biggest surprise for beginners comes when the code being shown is itself HTML. If you write <code><p>Hello</p></code>, the browser does not display the tags. It treats <p> as a real paragraph and shows only "Hello". The <code> and <pre> elements do not switch off HTML parsing.

To display a less-than sign you must write the entity &lt;, and for greater-than &gt;. An ampersand itself becomes &amp;. The last line of the example shows this: the source contains entities, and the reader sees a literal <p>. When code is inserted with JavaScript, setting textContent instead of innerHTML escapes it for you.

Another small issue is that very long lines inside <pre> do not wrap, which can push the page wider than a phone screen. Adding overflow-x: auto to pre in your CSS gives the block its own scrollbar instead.

Remember

  • Use <code> for inline snippets and <pre><code> for blocks.
  • Use <kbd> for input, <samp> for output and <var> for variables.
  • Escape <, > and & when the code contains them.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course