The <em>
tag in HTML is used to indicate emphasis on a certain word or phrase. In this tutorial, we will cover how to use the <em>
tag to emphasize text and apply styling to it.
Basic Usage
The basic syntax of the <em>
tag is as follows:
<p>This is a <em>very</em> important word.</p>
In this example, we have used the <em>
tag to emphasize the word “very”. When the HTML is rendered in a browser, the emphasized text will be displayed in italics.
Styling the Emphasized Text
You can use CSS to style the <em>
tag and its child elements. Here’s an example of how to use CSS to apply some basic styling to emphasized text:
<style>
em {
font-style: italic;
color: red;
}
</style>
<p>This is a <em>very</em> important word.</p>
In this example, we use CSS to apply the following styles to the emphasized text:
font-style: italic;
is used to make the text italic.color: red;
is used to set the color of the text to red.
Accessibility Considerations
When using the <em>
tag, it’s important to ensure that the emphasized text is accessible to all users, including those who use assistive technologies such as screen readers. Here are some tips for creating accessible content with the <em>
tag:
- Use the
<strong>
tag to indicate stronger emphasis, and use<em>
for weaker emphasis. - Use emphasis sparingly and only where it is necessary to convey meaning.
- Avoid using emphasis purely for decorative purposes.
- Ensure that the content is readable and has sufficient contrast between the text and the background.
Conclusion
The <em>
tag is a useful HTML element that is used to indicate emphasis on a certain word or phrase. By using CSS to style the emphasized text, you can create a visually appealing and user-friendly layout. By following best practices for accessibility and using emphasis sparingly, you can create content that is both functional and accessible to all users.