OneBite.Dev - Coding blog in a bite size

justify text css

Code snippet for how to justify text css with sample and detail explanation

Title: Justifying Text with CSS - A Simple Guide

Cascading Style Sheets (CSS) provide several ways to control how your text is presented on the webpage. In this short article, we will focus on one specific property, the text-align property, which we will use to justify text in CSS.

Code Snippet: Using text-align Property

When you want your text to evenly spaced and aligned with both the left and right margins, the “justify” value of the “text-align” property can be utilized.

Take a look at the simple code snippet below:

p {
    text-align: justify;
}

In this snippet, we’ve addressed all paragraphs (element ‘p’) in our HTML file and have set their text alignment to ‘justify’ using CSS.

Code Explanation for Using text-align Property

The code provided is a simple yet effective example of how to justify your text using CSS. Let’s break it down:

  1. The ‘p’ at the beginning is the CSS selector which tells the browser to apply the given styles or properties to every ‘p’ or paragraph HTML element on the webpage.

  2. The ’{ }’ brackets are used to contain and define the properties and values that you want to apply to the selected HTML element. In this case, we are applying the ‘text-align’ property.

  3. ‘text-align’ is the CSS property that’s responsible for aligning the text inside the selected element. This property can take several values like left, right, center, and justify, each of them determining the alignment of text within the block element.

  4. ‘justify’ is the property value that we’ve given to the ‘text-align’ property. This property value evenly distributes text so that they align with both the left and right margins. This is identical to how text is arranged in newspaper columns.

From now on, every ‘p’ element inside your HTML will have its text justified according to the CSS rule we’ve set until the rule is unloaded or overridden by another CSS rule. Remember, you can use this property with any other HTML elements like div, section, article, etc., and not just with ‘p’ elements.

Justifying text with CSS is indeed simple yet it makes a significant difference in improving your website’s readability and overall aesthetic appeal.

css