W3docs

HTML <code> Tag

The <code> tag is used to insert lines of program code. Description of the tag, attributes and examples of usage.

The <code> tag is used to insert variables, fragments of program code, etc. into an HTML document. In the browser, the code is displayed in a monospaced font (a font in which all characters have the same width). The <code> tag is an inline element by default.

The <code> tag alone represents a single code line or code phrase. It should be wrapped within a <pre> element for representing several lines of code.

You can use CSS to achieve better effects.

Syntax

The <code> tag comes in pairs. The content is written between the opening (<code>) and closing (</code>) tags.

Example of the HTML <code> tag:

HTML <code> Tag

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
  </head>
  <body>
    <p>Ordinary text.</p>
    <code>Line of program code.</code>
    <p>Continuation of the ordinary text.</p>
  </body>
</html>

Result

code example

Example of the HTML <code> tag placed inside an HTML <pre> tag:

Example of the HTML <code> tag inside an HTML <pre> tag:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
  </head>
  <body>
    <pre>
      <code>
        body {
          color: yellow;
          font-size: 16px;
          line-height: 1.5;
        }
      </code>
    </pre>
  </body>
</html>

Example of the HTML <code> tag with CSS properties:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .code-style {
        font-size: 20px;
        line-height: 28px;
        background-color: lightblue;
        color: #ffffff;
      }
    </style>
  </head>
  <body>
    <p>Ordinary text.</p>
    <code class="code-style">Line of program code.</code>
    <p>Continuation of the ordinary text.</p>
  </body>
</html>

Attributes

The <code> tag supports the Global Attributes and the Event Attributes.

For example, you can use the title attribute to add a tooltip: <code title="Variable declaration">let x = 10;</code>.

Practice

Practice

What is the correct usage of the HTML <code> tag?