HTML Highlighted Text

HTMLBeginner
Practice Now

Introduction

In HTML, the <mark> tag is used to highlight or mark the portion of text in order to show the importance of the text. In this lab, we will learn how to use the <mark> tag to create highlighted text.

Note: You can practice coding in index.html and learn How to Write HTML in Visual Studio Code. Please click on 'Go Live' in the bottom right corner to run the web service on port 8080. Then, you can refresh the Web 8080 Tab to preview the web page.

This is a Guided Lab, which provides step-by-step instructions to help you learn and practice. Follow the instructions carefully to complete each step and gain hands-on experience. Historical data shows that this is a beginner level lab with a 100% completion rate. It has received a 100% positive review rate from learners.

Set up the HTML file

To begin, create an index.html file and set up the basic structure of the HTML file. Insert the following code:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Highlighted Text with Mark Tag</title>
  </head>
  <body></body>
</html>

Add Text to Markup

In between the <body> tags, add some text. In this example, we will highlight the text "Brown fox" within a sentence.

<body>
  <p>The quick <mark>brown fox</mark> jumped over the lazy dog.</p>
</body>

Add a CSS style to change the default color

By default, the highlighted text will have a yellow background color and black text color. You can change these colors by adding CSS to the <mark> tag. For example, to change the highlighted background color to red and the text color to white, add the following CSS style:

<style>
  mark {
    background-color: red;
    color: white;
  }
</style>

Optional): Add a Class Selector for Styling

If you have multiple instances of highlighted text on a page and you want to style them all the same way, you can add a class to the <mark> tag and add a CSS class selector. In the following example, we will add a class named highlight to the <mark> tag and then style it using CSS.

<body>
  <p>
    The quick <mark class="highlight">brown fox</mark> jumped over the lazy dog.
  </p>
  <p>The <mark class="highlight">sun</mark> is a star.</p>
</body>

<style>
  .highlight {
    background-color: green;
    color: white;
  }
</style>

Summary

That's it! You have learned how to create highlighted text with the <mark> tag in HTML. By using the <mark> tag and a few CSS styles, you can easily draw your readers' attention to important parts of your text.