To “create a tag” means to write new HTML code using angle brackets to define either a new element or provide additional meaning and structure to existing content. Tags give web pages semantics and accessibility.
When to Create New Tags
There are a few main reasons you may want to create a new tag in your HTML:
Defining New Page Elements
If you want to add a new type of content/layout that doesn’t match any existing HTML tags, creating a new tag allows you to define what that element is. For example, if you wanted a special type of pull quote, you could create a
Adding Extra Semantics
Adding new tags can provide extra semantic meaning to existing content. For example, screen reader accessibility can be improved by adding tags like
Hooking JavaScript/CSS
Custom tags allow developers to easily hook JavaScript or CSS to certain parts of a page. For example, you could define a
Future Proofing Content
As new tags get introduced to HTML over time, using custom tag names can future proof your content. For example you could wrap site headers in a
How to Create New Tags
Creating a new tag is as simple as wrapping any content in angle brackets using either a new tag name you make up, or an existing tag repurposed for extra semantics.
For example:
<newtagname>Some content</newtagname>
Some key points on creating valid new tags:
Use Self Closing Tags When Needed
Some elements like images or inputs don’t wrap around content. These should use a trailing slash to close the tag:
<newinputtag />
Nest Tags Properly
Make sure to properly close tags so they are nested correctly:
<newtag> Some content <innernewtag>More content</innernewtag> </newtag>
Use Lowercase Tag Names
Tag names should be lower case. Upper case tag names are not valid.
Avoid Existing Tag Names
Don’t reuse names of tags that already exist in HTML. This can cause conflicts.
Adding Attributes to Tags
Tags can also have attributes added to provide additional information:
<newtag attribute1="value1" attribute2="value2"> </newtag>
Some common cases to use attributes:
ID and Class For Styling/Selecting
Use id or class attributes to identify tags for styling or selecting with CSS or JavaScript:
<newtag id="cooltag" class="fancy-tag"></newtag>
Data Attributes
Custom data attributes allow storing extra data in the tag:
<newtag data-tooltip="More info!"></newtag>
ARIA Roles for Accessibility
ARIA roles like aria-hidden help convey extra meaning for screen readers:
<newtag aria-hidden="true"></newtag>
Examples Creating New Tags
Here are some examples of creating new tags in HTML:
Semantic Section Tag
Wrap a page section in a custom
<section class="page-intro"> <p>Welcome to our website!</p> </section>
Styled Blockquote
Add a class to style a
differently:
<blockquote class="styled-quote"> <p>To be or not to be, that is the question.</p> </blockquote>Clickable Image Map
Use a