How to Horizontally Center a Div in CSS

Avatar

By squashlabs, Last Updated: Oct. 6, 2023

How to Horizontally Center a Div in CSS

To horizontally center a <div> element in CSS, you can use one of the following methods:

Method 1: Using margin: auto;

You can use the margin property with the value auto to horizontally center a <div>. Here's how you can do it:

<div style="margin: 0 auto">Centered div</div>

In the above example, we set the margin property of the <div> to 0 auto. The 0 value for the top and bottom margins ensures that the <div> doesn't have any vertical spacing. The auto value for the left and right margins automatically distributes the remaining space equally on both sides, effectively centering the <div> horizontally.

Related Article: How to Horizontally Center a Div Element in CSS

Method 2: Using flexbox;

Another way to horizontally center a <div> is by using flexbox. Here's an example:

<div style="justify-content: center">Centered div</div>

In the above example, we set the display property of the <div> to flex to create a flex container. The justify-content property with the value center aligns the flex items (in this case, the <div>) along the horizontal axis, effectively centering it.

Best Practices:

- It's recommended to use CSS classes instead of inline styles for better maintainability and reusability. For example:

    .centered {
        margin: 0 auto;
        display: flex;
        justify-content: center;
    }


<div class="centered">Centered div</div>

- If you want to center a <div> both horizontally and vertically, you can use the align-items property in combination with justify-content. For example:

    .centered {
        display: flex;
        justify-content: center;
        align-items: center;
    }


<div class="centered">Centered div</div>

- If you need to support older browsers that don't fully support flexbox, you can use a combination of display: table and text-align: center to achieve horizontal centering. However, this method is less flexible compared to flexbox. Here's an example:

    .centered {
        display: table;
        margin: 0 auto;
        text-align: center;
    }


<div class="centered">Centered div</div>

Alternative Ideas:

- Another approach to horizontally center a <div> is by using the position property. You can set position: absolute on the <div> and then set left: 50% and transform: translateX(-50%) to center it. However, this method requires the parent container to have a non-static position (e.g., position: relative).

    .parent-container {
        position: relative;
    }

    .centered {
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
    }


<div class="parent-container">
    <div class="centered">Centered div</div>
</div>

- If you only need to center a single line of text within a <div>, you can use the text-align property instead. For example:

<div style="text-align: center"><a href="https://www.squash.io/centering-text-with-css-tricks-and-techniques/">Centered text</a></div>

More Articles from the Cascading Style Sheets (CSS) Guide series:

How to Change HTML Input's Placeholder Color with CSS

Learn how to change the color of an HTML input's placeholder text using CSS. Discover two methods: using the color property and using the ::placehold… read more

How to Center an Absolutely Positioned Element in a Div

Centering an absolutely positioned element in a div can be achieved using various approaches in CSS. This article provides a step-by-step guide on ho… read more

CSS Position Relative: A Complete Guide

This tutorial is a comprehensive resource that dives deep into the fundamentals, practical examples, real-world applications, and advanced techniques… read more

How to Use CSS Hex Code Colors with Alpha Values

Learn how to incorporate transparency in CSS hex code colors for stunning visual effects. This article provides a comprehensive guide on using alpha … read more

How to Vertically Center Text with CSS

This article provides a guide on vertically centering text using CSS properties and techniques. You'll learn how to achieve this using flexbox, CSS g… read more

How to Fix CSS Text Overflow Ellipsis Issue

Resolving CSS text-overflow ellipsis issues can be frustrating, but it doesn't have to be. This article provides a simple guide to fix this problem b… read more

How to Make a Div Vertically Scrollable Using CSS

Making a div vertically scrollable in CSS can be achieved through different methods. This article provides a guide on two solutions: using the overfl… read more

How to Style a Disabled Button with CSS

Styling disabled buttons using CSS can greatly enhance the user interface of your web application. This article provides a step-by-step guide on two … read more

How To Set the CSS Background Opacity Property

Adjusting the opacity of CSS backgrounds is made easy with these two methods. Learn how to use RGBA color values or the CSS opacity property to achie… read more

How to Style CSS Scrollbars

Learn how to customize scrollbars using CSS in this tutorial. Discover different use cases and best practices for styling scrollbars, ensuring consis… read more