"9 Easy Ways to Center Elements in HTML: A Step-by-Step Guide"

Gerald Jun 16, 2026

Centering in HTML: A Comprehensive Guide

a screen shot of a web page with the words'html input types '
a screen shot of a web page with the words'html input types '

Centering content in HTML can be achieved through various methods, depending on the type of element and the level of control required. In this guide, we will explore the different techniques for centering text, images, and blocks of content in HTML, along with their respective syntax and examples.

HTML Cheat Sheet πŸ“„βœ¨ Quick Guide for Beginners
HTML Cheat Sheet πŸ“„βœ¨ Quick Guide for Beginners

Centering Text

Centering text in HTML is a straightforward process, and there are several ways to achieve it. The most common methods are using CSS styles or HTML attributes.

an info sheet with the text headings in html
an info sheet with the text headings in html

Method 1: Using CSS Styles

  • Wrap the text in a <p> element.
  • Add a CSS style to the <p> element using the margin-left and margin-right properties set to auto.
🧾 How to Create Tables in HTML
🧾 How to Create Tables in HTML

Example:

Centered text using CSS styles.

Method 2: Using HTML Attributes

Excellent Way to Centre div, CSS
Excellent Way to Centre div, CSS
  • Wrap the text in a <p> element.
  • Add the align attribute to the <p> element and set its value to center.

Example:

Centered text using HTML attributes.

10 easy ways center div inside another div vertically and horizontally
10 easy ways center div inside another div vertically and horizontally

Centering Images

Centering images in HTML can be achieved using CSS styles or the <center> element (although the latter is deprecated and not recommended).

HTML introduction for beginners
HTML introduction for beginners
an info sheet with the text basic html structure in blue and white, on top of it
an info sheet with the text basic html structure in blue and white, on top of it
a table with several different types of text and numbers on the top, bottom left corner
a table with several different types of text and numbers on the top, bottom left corner
How to Center a Div | CSS Tutorial
How to Center a Div | CSS Tutorial
Favicon in HTML: Add Bookmark & App Icons 🎨🌍
Favicon in HTML: Add Bookmark & App Icons 🎨🌍
HTML Center Text – How to CSS Vertical Align a Div
HTML Center Text – How to CSS Vertical Align a Div
an image of a computer screen with text
an image of a computer screen with text
HTML + CSS Cheat Sheet (One Page) πŸ’»
HTML + CSS Cheat Sheet (One Page) πŸ’»
How to Center Anything With CSS
How to Center Anything With CSS
HTML Cheat Sheet for Beginners (Quick Guide)
HTML Cheat Sheet for Beginners (Quick Guide)
the three ways to apply css in html and wordpress, including an info sheet
the three ways to apply css in html and wordpress, including an info sheet
Block vs Inline Elements in HTML β€” Beginner-Friendly Explanation with Examples
Block vs Inline Elements in HTML β€” Beginner-Friendly Explanation with Examples
a handwritten diagram with the words html and an image of a computer screen on it
a handwritten diagram with the words html and an image of a computer screen on it
Basics of HTML
Basics of HTML
Web Development programing tricks and tips for beginners free
Web Development programing tricks and tips for beginners free
Using Rhythm Concepts for Making Better Web Designs
Using Rhythm Concepts for Making Better Web Designs
πŸ“ Create Forms in HTML
πŸ“ Create Forms in HTML
π™ƒπ™π™ˆπ™‡ π™π™–π™œπ™¨ π™˜π™π™šπ™–π™© π™¨π™π™šπ™šπ™©
π™ƒπ™π™ˆπ™‡ π™π™–π™œπ™¨ π™˜π™π™šπ™–π™© π™¨π™π™šπ™šπ™©
The Way You’re Centering Text is Wrong β€’ Nose Graze
The Way You’re Centering Text is Wrong β€’ Nose Graze
How to Center Anything With CSS
How to Center Anything With CSS

Method 1: Using CSS Styles

  • Wrap the image in a <div> element.
  • Add a CSS style to the <div> element using the display property set to block and the margin-left and margin-right properties set to auto.

Example:

Centered image using CSS styles

Centering Blocks of Content

Centering blocks of content in HTML can be achieved using CSS styles or the <center> element (although the latter is deprecated and not recommended).

Method 1: Using CSS Styles

  • Wrap the block of content in a <div> element.
  • Add a CSS style to the <div> element using the margin-left and margin-right properties set to auto and the display property set to block.

Example:

Centered paragraph

Using the <center> Element

The <center> element was used in the past to center content, but it has been deprecated in HTML5 and is not recommended for use. However, for legacy reasons, it may still be encountered in older web pages.

Example:

Centered paragraph

Conclusion

Centering content in HTML can be achieved through various methods, including using CSS styles, HTML attributes, and the <center> element (although the latter is deprecated and not recommended). By understanding the different techniques and their respective syntax, developers can effectively center text, images, and blocks of content in HTML, resulting in a more visually appealing and user-friendly web page.

Best Practices

When centering content in HTML, it is essential to follow best practices to ensure cross-browser compatibility and maintain a consistent design. These best practices include:

  • Using CSS styles for centering, as they provide more control and flexibility.
  • Setting the display property to block for blocks of content.
  • Setting the margin-left and margin-right properties to auto for centering.
  • Using the <center> element only for legacy reasons and in older web pages.

By following these best practices, developers can create a well-designed and user-friendly web page that is accessible across various browsers and devices.