Design Converter
Education
Software Development Executive - I
Last updated on Nov 7, 2024
Last updated on Nov 7, 2024
For developers and designers, knowing how to highlight text in HTML isn’t just a nice-to-have—it’s an essential skill that can dramatically impact user experience. Whether you’re emphasizing key points or guiding readers’ attention, strategic text highlighting can make content more readable, interactive, and visually appealing.
In this all-encompassing guide, we’ll explore effective HTML and CSS techniques to help you highlight text like a pro. By the end, you’ll be fully equipped to add vibrant highlights that enhance engagement and usability on your web pages.
Before we dive into the details, let's start with the basics. The most straightforward way to highlight text in HTML is by using the <mark>
tag. The <mark>
tag is an HTML element that is used to highlight text. By default, most browsers will render the highlighted text with a yellow background color.
Here's an example of how to use the <mark>
tag:
1<!DOCTYPE html> 2<html> 3<body> 4<p>This is a <mark>highlighted text</mark> in a complete paragraph.</p> 5</body> 6</html>
In the above HTML code, the words "highlighted text" will appear with a yellow background color in most browsers. The <!DOCTYPE html>
at the beginning of the HTML document is necessary to ensure that the browser knows it's dealing with an HTML5 document.
While the <mark>
tag is handy, it only provides a yellow background color. What if you want to highlight text with a different background color? This is where CSS comes into play. CSS, or Cascading Style Sheets, is a language used to style HTML documents. You can use inline CSS to change the background color of the <mark>
tag.
Here's an example of how to use inline CSS to change the background color:
1<!DOCTYPE html> 2<html> 3<body> 4<p>This is a <mark style="background-color: green;">highlighted text</mark> in a complete paragraph.</p> 5</body> 6</html>
In the above HTML code, the words "highlighted text" will appear with a green background color. The style attribute is used to apply inline CSS to the <mark>
tag.
While inline CSS is useful, it can become cumbersome if you need to highlight text in multiple places throughout your HTML document. A more efficient approach is to define a CSS class in a separate CSS file and then apply that class to the HTML tags where you want to highlight text.
Here's an example of how to create a CSS class for highlighting text:
1/* In your CSS file */ 2.highlight { 3 background-color: green; 4}
And here's how to apply that CSS class to an HTML tag:
1<!DOCTYPE html> 2<html> 3<head> 4<link rel="stylesheet" type="text/css" href="styles.css"> 5</head> 6<body> 7<p>This is a <mark class="highlight">highlighted text</mark> in a complete paragraph.</p> 8</body> 9</html>
In the above HTML code, the words "highlighted text" will appear with a green background color. The class attribute is used to apply the CSS class to the <mark>
tag.
<span>
Tag to Highlight TextWhile the <mark>
tag is useful for highlighting text, it's not the only HTML tag that can be used for this purpose. The <span>
tag is another HTML tag that can be used to highlight text. The tag is an inline element for applying styles to a specified portion of text.
Here's an example of how to use the <span>
tag to highlight text:
1<!DOCTYPE html> 2<html> 3<body> 4<p>This is a <span style="background-color: yellow;">highlighted text</span> in a complete paragraph.</p> 5</body> 6</html>
In the above HTML code, the words "highlighted text" will appear with a yellow background color. The style attribute is used to apply inline CSS to the <span>
tag.
Another interesting way to highlight text in HTML is to change the background color when the user hovers over the text. This can be achieved by using the :hover pseudo-class in CSS.
Here's an example of how to highlight text on hover:
1/* In your CSS file */ 2.highlight:hover { 3 background-color: yellow; 4}
And here's how to apply that CSS class to an HTML tag:
1<!DOCTYPE html> 2<html> 3<head> 4<link rel="stylesheet" type="text/css" href="styles.css"> 5</head> 6<body> 7<p>This is a <span class="highlight">highlighted text</span> in a the complete paragraph.</p> 8 9</body> 10</html>
In the above HTML code, the words "highlighted text" will appear with a yellow background color only when the user hovers over them. The :hover pseudo-class is used to change the background color property when the user hovers over the text.
While HTML and CSS are great for static highlighting, JavaScript can be used for dynamic highlighting. For example, you can use JavaScript to highlight a specific word or phrase in a complete paragraph.
Here's an example of how to use JavaScript to highlight text:
1<!DOCTYPE html> 2<html> 3<body> 4<p id="myPara">This is a complete paragraph.</p> 5 6<script> 7var para = document.getElementById("myPara"); 8var text = para.innerHTML; 9text = text.replace(/complete paragraph/g, "<mark class='highlight'>complete paragraph</mark>"); 10para.innerHTML = text; 11</script> 12 13</body> 14</html>
In the above HTML code, the words "complete paragraph" will be highlighted with the background color defined in the 'highlight' CSS class. The JavaScript code uses the replace() method to replace the words "complete paragraph" with the same words wrapped in a <mark>
tag with the 'highlight' CSS class.
Bringing attention to essential details through text highlighting in HTML is a simple yet powerful way to engage readers. From the versatile <mark>
and <span>
tags to custom CSS classes and dynamic JavaScript, you now have a variety of techniques to emphasize important information within your web pages.
We hope this guide has equipped you with the know-how to elevate your HTML content with visually compelling highlights. Now, go ahead and implement these strategies to make your content shine and captivate your audience effectively!
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.