How to Center Align Text in HTML

How to Center Align Text in HTML

When creating a website or webpage, it is important to ensure that the text is properly aligned and visually appealing. One common alignment technique is center aligning text. In this article, we will discuss different ways to center align text in HTML using various methods and CSS properties.

Using text-align CSS Property

One of the simplest ways to center align text in HTML is to use the text-align CSS property. This property is used to specify the horizontal alignment of text within an element. By setting the value to center, the text will be centered within the element.

Example:

<!DOCTYPE html>
<html>
<head>
  <style>
    .center-text {
      text-align: center;
    }
  </style>
</head>
<body>
  <div class="center-text">This text is center aligned using text-align CSS property.</div>
</body>
</html>

Output:

How to Center Align Text in HTML

Using the div Element with CSS

Another way to center align text in HTML is to use the div element with CSS. By applying styles such as display: flex, justify-content: center, and align-items: center, the text can be centered within the div element.

Example:

<!DOCTYPE html>
<html>
<head>
  <style>
    .center-div {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100px;
    }
  </style>
</head>
<body>
  <div class="center-div">This text is center aligned using div element with CSS.</div>
</body>
</html>

Output:

How to Center Align Text in HTML

Using the center Element (Deprecated)

In older versions of HTML, the <center> element was commonly used to center align text. However, this element has been deprecated in HTML5 and it is recommended to use CSS for styling instead.

Example:

<!DOCTYPE html>
<html>
<body>
  <center>This text is center aligned using the center element.</center>
</body>
</html>

Output:

How to Center Align Text in HTML

Using the text-center Class in Bootstrap

If you are using the Bootstrap framework, you can easily center align text by using the text-center class. This class is provided by Bootstrap and can be applied to any element to center align its text.

Example:

<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
  <div class="text-center">This text is center aligned using the text-center class in Bootstrap.</div>
</body>
</html>

Output:

How to Center Align Text in HTML

Using the text-align Property with Inline Styles

In some cases, you may want to apply inline styles to center align text within a specific element. This can be done by using the style attribute and setting the text-align property to center.

Example:

<!DOCTYPE html>
<html>
<body>
  <p style="text-align: center;">This text is center aligned using inline styles.</p>
</body>
</html>

Output:

How to Center Align Text in HTML

Using the <center> Tag

While the <center> tag is deprecated in HTML5, it is still supported by most browsers. You can use this tag to center align text within a block-level element.

Example:

<!DOCTYPE html>
<html>
<body>
  <center>This text is center aligned using the center tag.</center>
</body>
</html>

Output:

How to Center Align Text in HTML

Combining Methods for Center Alignment

Sometimes, you may need to combine multiple methods to achieve center alignment for different elements on a webpage. By using a combination of CSS properties and HTML tags, you can create a visually appealing layout with center aligned text.

Example:

<!DOCTYPE html>
<html>
<head>
  <style>
    .center-heading {
      text-align: center;
    }

    .center-paragraph {
      text-align: center;
    }
  </style>
</head>
<body>
  <h1 class="center-heading">Center Aligned Heading</h1>
  <p class="center-paragraph">This paragraph is center aligned using CSS.</p>
</body>
</html>

Output:

How to Center Align Text in HTML

Conclusion

In this article, we have discussed various methods for center aligning text in HTML. Whether you prefer using CSS properties, HTML tags, or frameworks like Bootstrap, there are multiple ways to achieve center alignment for text on a webpage. Experiment with different techniques to find the best solution for your design needs.

Like(0)

HTML Articles