When placing content on web pages developers often wish to underline specific text for multiple reasons such as draw users’ attention, style the text or maybe represent a misspelled text. Whatever the reason maybe, if you wish to underline some in html then there are various ways through which this task can be accomplished. These approaches have been discussed in this blog.
How to underline text using <u> tag
For the purpose of underlining a text using the <u> tag simply place the text to be underlined within this tag. The purpose of this tag is to basically to represent a text that is implicit, styled differently or misspelled. The tag was initially eliminated from HTML but was included again in HTML5.
HTML
In the above code, we are simply making a heading and a paragraph. However, we wish to underline a single word inside the paragraph therefore we are wrapping that particular word inside the <u> tag.
Output
This is how you can underline text using the <u> tag.
There is another way that lets you underline text and has been discussed in the upcoming section.
How to underline text using style attribute
Another way of underlining text in html is by using the style attribute that defines an inline style for a particular element.
HTML
In the above code, you will notice that we are using the <span> element to underline the word and setting its style attribute to “text-decoration: underline”. This way our desired word will be underlined.
Output
The word was successfully underlined using the style attribute.
If you wish to underline a text using CSS then see the code snippet below.
HTML
Here we are first of all wrapping the word to be underlined inside a <span> element and then using the internal CSS to set the text-decoration property of the <span> element to underline. The code above will generate the same output as above.
Conclusion
For the purpose of underlining a text in html make use of the <u> tag or the style attribute. Simply wrap the text to be underlined inside the <u> tag or when using the style attribute set the text-decoration property to underline. The purpose of the <u> tag is to basically to represent a text that is implicit, styled differently or misspelled, whereas, the style attribute defines an inline style for a particular element.