<code> HTML Tag

Disclosure: Your support helps keep the site running! We earn a referral fee for some of the services we recommend on this page. Learn more
Element of
Learn How Fonts And Web Typography Work In HTML: A Beginner's Guide
What does <code> HTML Tag do?
The <code> element is used to define enclosed text as computer code. It is often paired with the <pre> element to preserve line breaks and indentation when presenting blocks of computer code.

Code Example

The <code>&lt;code&gt;</code> element defines inline code.
The <code> element defines inline code.

Not all you need

If you want to present code samples in your site, the <code> element is not the only thing you’ll need. For multi-line (“block”) code presentation, the most common approach is to use the <pre> element, and then put the <code> element inside that. Additionally, special characters that don’t render properly in HTML (like angle-brackets and ampersands) may need to be converted into their appropriate HTML Character Entity codes. This is highly error-prone work if done by hand. If you are writing a lot of code samples for your site, you may want to look at automated code rendering via a system like Markdown or an embedding service like Github’s gist.

Adam is a technical writer who specializes in developer documentation and tutorials.

Browser Support for code