Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to align text in HTML

The Basics of Aligning Text in HTML

When you're just starting to learn programming, you might find yourself wondering how to align text in HTML, one of the most fundamental languages of the web. Let's picture it this way: imagine that your webpage is a blank canvas, and you're the artist who gets to decide where every element goes. Just like you would arrange the elements of a painting to create balance and harmony, you can align your text in different ways to make your website more visually appealing.

What is Text Alignment?

Text alignment refers to the horizontal placement of text on a webpage. It can be aligned to the left, right, center or can be justified. Just as you'd arrange your words on paper when you're writing a letter—maybe you align your address to the right and the body of your letter to the left—you can also decide how the text on your webpage is aligned.

The Basic HTML Elements for Text Alignment

In HTML, you'll primarily use the <p>, <h1> to <h6>, and <div> elements to contain and arrange text. These are like the building blocks of your webpage. Just as a builder uses bricks to build a house, you'll use these elements to build your webpage. Here's what a basic HTML element looks like:

<p>This is a paragraph.</p>

The <p> tag is used to define a paragraph, and the text between the opening <p> tag and the closing </p> tag is the content of the paragraph.

How to Align Text with CSS

To align text in HTML, you'll actually use something called CSS, or Cascading Style Sheets. Think of CSS like the paint you'd use on your canvas—it lets you add color, style, and layout to your webpage.

To align text, you'll use the text-align property. This property can be set to "left", "right", "center", or "justify". Here is how you can use it:

<p style="text-align: center;">This text is centered.</p>

In this example, the style attribute within the <p> tag is used to add CSS to the text. The "text-align: center" within the quotation marks is the CSS. It's telling the browser to align the text in the center.

Aligning Headings

Just as you can align paragraphs, you can also align headings. Headings in HTML are defined with the <h1> to <h6> tags. <h1> defines the most important heading, while <h6> defines the least important heading.

Here's how you can center a heading:

<h1 style="text-align: center;">This is a centered heading.</h1>

Aligning Multiple Elements

What if you want to align lots of text in the same way? You could add style="text-align: center;" to every single element, but that would take a lot of time. Instead, you can use a <div> element to contain and style multiple elements at once.

A <div> tag defines a division or a section in an HTML document. It's like a big box that you can put other elements inside. Here's how you can use it to center multiple paragraphs and headings:

<div style="text-align: center;">
   <h1>This is a centered heading.</h1>
   <p>This is a centered paragraph.</p>
   <p>So is this one!</p>
</div>

In this example, the <div> tag contains two paragraphs and a heading. Because the text-align: center; is applied to the <div>, all the text within it is centered.

Justify Text Alignment

There's a fourth option for the text-align property: justify. When text is justified, it's aligned along both the left and right margins. This gives a clean edge to both sides of the text, much like in a newspaper or in a book. Here's how you can justify text:

<p style="text-align: justify;">This text is justified.</p>

Conclusion

Just like an artist with a canvas, you have the power to arrange the elements on your webpage in whatever way you think looks best. With the text-align property in CSS, you can align your text to the left, right, center, or justify it. Whether you're aligning a single paragraph, a heading, or multiple elements within a <div>, remember to use the right tools for the job, and you'll have a beautifully arranged webpage in no time. Happy coding!