HTML <code> Tag

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) of the smaller size.

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:

<!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

Example of the HTML <code> tag placed 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.

How to style <code> tag?

Common properties to alter the visual weight/emphasis/size of text in <code> tag:

  • CSS font-style property sets the style of the font. normal | italic | oblique | initial | inherit.
  • CSS font-family property specifies a prioritized list of one or more font family names and/or generic family names for the selected element.
  • CSS font-size property sets the size of the font.
  • CSS font-weight property defines whether the font should be bold or thick.
  • CSS text-transform property controls text case and capitalization.
  • CSS text-decoration property specifies the decoration added to text, and is a shorthand property for text-decoration-line, text-decoration-color, text-decoration-style.

Coloring text in <code> tag:

  • CSS color property describes the color of the text content and text decorations.
  • CSS background-color property sets the background color of an element.

Text layout styles for <code> tag:

  • CSS text-indent property specifies the indentation of the first line in a text block.
  • CSS text-overflow property specifies how overflowed content that is not displayed should be signalled to the user.
  • CSS white-space property specifies how white-space inside an element is handled.
  • CSS word-break property specifies where the lines should be broken.

Other properties worth looking at for <code> tag:


Browser support



Do you find this helpful?

Related articles