Juneikerc.com

How to Justify Text in HTML with CSS

Featured image of the post: How to Justify Text in HTML with CSS

While it's not very common to see web pages these days use justified text, some websites that publish research, scientific articles, studies, etc., may employ it. Presenting paragraph text in a justified manner can be a way to give a more formal and distinctive impression compared to the rest of web pages.

Personally, I wouldn't recommend any website to justify their content. In this article, you have an explanation of why this is generally not a good idea. Link to the article

!My Personal Opinion

In my opinion, the content of your page will look much better aligned to the left (as it comes by default) or, in some special cases, centered.

Justify Text with text-align: justify;

The property we should use to justify text is text-align followed by the value justify, as in the following example:

html
<p class="text-justify">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the standard dummy text of the industry since the year 1500, when an unknown printer (N.B. a person involved in printing) used a gallery of texts and mixed them in such a way that he managed to create a specimen book of texts.
</p>
css
.text-justify {
text-align: justify;
}

Example with all paragraphs justified.

Justify text directly in HTML using inline styles

Although using inline styles is not the recommended way to insert CSS in HTML, it is necessary to know how to do it because sometimes we must resort to this method.

html
<p style="text-align: justify;">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the standard dummy text of the industry since the year 1500, when an unknown printer (N.B. a person involved in printing) used a gallery of texts and mixed them in such a way that he managed to create a specimen book of texts.
</p>

Justifying text with the deprecated HTML align attribute

While researching to write this post, I found some articles on forums and other pages where they used the HTML align attribute to justify the text, as in the following examples:

html
<p align="justify">This is the paragraph in which the text will be justified.</p>
<div align="justify">Div tag with justified text.</div>

Needless to say, the align attribute should no longer be used as it is obsolete, and there are methods like the ones mentioned earlier that are the current standards.

Important Note

Always avoid using outdated HTML tags that are no longer supported by HTML5 standards. After all, CSS has a solution for almost everything; all it takes is a little research to discover how to align your content.

Juneiker Castillo freelance web developer

I am Juneiker Castillo, a passionate front-end web developer deeply in love with programming and creating fast, scalable, and modern websites—a JavaScript enthusiast and a React.js lover ⚛️.

About me