How To Center Elements In HTML: CSS, Flexbox, Grid



Discover the best techniques to center elements in HTML, including using CSS, Flexbox, and Grid. From text alignment to block and inline elements, master the art of centering with ease.

Using CSS to Center Elements

Text Alignment

When it comes to using CSS to center elements on a webpage, one of the key aspects to consider is text alignment. Text alignment refers to how the text within an element is positioned in relation to the surrounding content. By adjusting the text alignment property in CSS, you can easily center text horizontally or vertically within an element.

Block Elements

Block elements are elements that take up the full width available to them on a webpage. When centering block elements using CSS, one common method is to set the left and right margins to auto. This effectively centers the block element within its containing element, regardless of the width of the browser window.

To center a block element horizontally using CSS:
.element {
margin: 0 auto;

Inline Elements

Inline elements, on the other hand, only take up as much width as necessary to display their content. When centering inline elements using CSS, the margin property alone may not work as expected. In this case, you can use the text-align property set to center on the parent element to achieve horizontal centering.

To center an inline element horizontally using CSS:
.parent-element {
text-align: center;
.element {
display: inline-block;

By understanding text alignment, block elements, and inline elements, you can effectively use CSS to center elements on your webpage in a visually appealing way. Experiment with different techniques and properties to achieve the desired centering effect that complements your overall design.

Centering with Margin Auto

Setting Margin

When it comes to centering elements on a webpage, using the margin: auto; property is a popular technique. This property allows you to automatically adjust the margins of an element, effectively centering it within its parent container. By setting the margin to auto, you are telling the browser to evenly distribute the space on either side of the element, pushing it towards the center.

Applying Margin Auto

To apply the margin: auto; property, you first need to specify a width for the element you want to center. This is crucial because the browser needs to know how much space the element should take up before it can calculate the margins. Once you have set the width, you can simply add margin: auto; to the CSS code for that element.

One common use case for applying margin: auto; is centering a div element horizontally on the page. By setting the width of the div and applying the margin property, you can achieve a perfectly centered layout that adapts to different screen sizes.

In summary, setting the margin to auto is a simple yet effective way to center elements on a webpage. By understanding how this property works and applying it correctly, you can create visually appealing designs that are both balanced and aesthetically pleasing.

Centering with Flexbox

Flexbox is a powerful tool in CSS that allows you to create flexible layouts with ease. By using Flexbox, you can easily center elements both horizontally and vertically on a webpage.

Flex Container

To begin centering elements with Flexbox, you first need to understand the concept of a flex container. A flex container is simply an element that contains one or more flex items. By setting the display property of a container to “flex”, you can enable Flexbox layout for its children.

Once you have designated a flex container, you can then utilize Flexbox properties to align its child elements. The most commonly used properties for centering elements are “justify-content” and “align-items”.

Justify Content

The “justify-content” property in Flexbox allows you to align items along the main axis of the flex container. By default, the main axis runs horizontally from left to right. When you set the value of “justify-content” to “center”, all items within the container will be centered along this axis.

An example of using “justify-content: center;” in CSS would look like this:


.flex-container {
display: flex;
justify-content: center;

This simple line of code will center all child elements within the flex container horizontally. It’s a quick and easy way to achieve perfect alignment without the need for complex calculations or additional markup.

Centering with Grid

When it comes to centering elements using CSS grid, understanding the concept of grid container and align items is crucial. The grid container serves as the parent element that holds all the grid items, allowing you to create a layout with rows and columns. By using the align items property, you can control how the items are aligned within the grid container.

Grid Container

The grid container is where the magic happens. It provides a structured layout by defining rows and columns for the grid items to be placed in. To create a grid container, you can use the following CSS code:


.grid-container {
display: grid;
grid-template-columns: 1fr 1fr 1fr; /* 3 columns with equal width */
grid-template-rows: auto; /* <strong>automatic row sizing</strong> */
gap: 10px; /* spacing between grid items */

With this code, you are setting up a grid container with three columns of equal width and automatic row sizing. The gap property allows you to control the spacing between the grid items, giving your layout a clean and organized look.

Align Items

Once you have your grid container set up, you can use the align items property to control how the grid items are aligned vertically within the container. There are different values you can use for the align items property, such as start, center, end, and stretch.

To align the items at the center of the grid container, you can use the following CSS code:


.grid-container {
display: grid;
align-items: center;

This code will align the grid items in the center of the grid container both vertically and horizontally, giving your layout a balanced and visually appealing design.

In conclusion, mastering the use of grid container and align items in CSS grid layout is essential for creating professional and visually appealing designs. By understanding how these properties work together, you can achieve a centered and well-organized layout that enhances the overall user experience. So, why not give it a try and see the difference it can make in your web design projects?

Leave a Comment


3418 Emily Drive
Charlotte, SC 28217

+1 803-820-9654
About Us
Contact Us
Privacy Policy



Join our email list to receive the latest updates.