Top 4 Ways Centering a Div or Text Inside a Div using CSS

  • css
  • 49 Views
  • 4 Min Read
  • 4 May 2024

In this article, we'll demonstrate four simple techniques for centering a div container or text line both horizontally and vertically using CSS. These four methods are easy to use.

 

So, let's get started.

 

Center div horizontally and vertically

 

Top 4 Ways for Vertically and Horizontally Centering a Div

 

1. Using a display grid + place items

 

Using CSS Grid, we can easily center any element with just two lines of code. To center an element, we set its parent element with the "display: grid" and "place-items: center" properties.

 

The "display: grid" makes the parent a grid, and "place-items: center" positions the item in the center both horizontally and vertically. This property combines "align-content" (for vertical centering) and "justify-content" (for horizontal centering) into one, simplifying the process.

 

<head>
    <style>
        .parent {
            height: 100vh;
            display: grid;
            place-items: center;
        }

        .child {
            background-color: steelblue;
            height: 150px;
            width: 150px;
            /* margin: auto; */
        }
    </style>
</head>

<body>
    <div class="parent">
        <div class="child"></div>
    </div>
</body>

 

Center div

 

If you use the "margin: auto" property on the child element instead of "place-items", it will still center the element. However, it's crucial to apply "margin: auto" to the child element, not the parent.

 

It is important to note that this method will only work if you have specified the width and height of the child elements.

 

 

2. Using CSS display flex + margin auto

 

With CSS flex, centering any element is a breeze with only two lines of code. To achieve centering, set the parent element to "display: flex" and apply "margin: auto" to its child element.

 

By setting the parent as "display: flex," it becomes a flex container while applying "margin: auto" to the child element ensures it's centered both horizontally and vertically.

 

<head>
    <style>
        .parent {
            height: 100vh;
            display: flex;
        }

        .child {
            background-color: thistle;
            margin: auto;
            height: 150px;
            width: 150px;
        }
    </style>
</head>

<body>
    <div class="parent">
        <div class="child"></div>
    </div>
</body>

 

Center div

 

If you apply the "display: flex" and "place-item: center" properties to the parent element, the element will only center vertically, not horizontally. The "place-item: center" property is effective when the element is set to "display: grid".

 

It is important to note that this method will only work if you have specified the width and height of the child elements.

 

 

3. Using table cell + display inline block

 

Developers often overlook the versatility of the "display: table-cell" property for centering content and elements within a container.

 

To center elements using this method, apply "display: table-cell" to the parent element, treating it as a table cell. Ensure that the parent element has its height and width specified for effective centering of the inner element.

 

Next, set the "text-align: center" and "vertical-align: middle" properties on the parent element to center the element both horizontally and vertically. Finally, set the child element to "display: inline-block" to complete the centering process.

 

<head>
    <style>
        .parent {
            display: table-cell;
            height: 100vh;
            width: 100vw;
            text-align: center;
            vertical-align: middle;
        }

        .child {
            display: inline-block;
            height: 150px;
            width: 150px;
            background-color: rosybrown;
        }
    </style>
</head>

<body>
    <div class="parent">
        <div class="child"></div>
    </div>
</body>

 

Center div

 

 

4. Using position absolute + margin auto

 

To center any element using the "position: absolute" method, follow these steps:

 

  • Set the element's position to absolute.

 

  • Assign a value of 0 to the top, bottom, left, and right properties.

 

  • Apply "margin: auto" to the element to achieve centering.

 

It's important to note that in this method, only the child element is styled, and the parent element remains unaffected. Additionally, the parent element's default height is set to absolute.


<head>
    <style>
        div {
            width: 150px;
            height: 150px;
            background-color: skyblue;
            position: absolute;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            margin: auto;
        }
    </style>
</head>

<body>
    <div></div>
</body>

 

Center div

 

 

Top 4 Ways for Vertically and Horizontally Centering Text Within a Div

 

1. Using Text-Align property for horizontal center

 

As the name suggests, "text-align: center" can be utilized to horizontally center text. Similarly, you can employ this property to horizontally center images as well.

 

In the following example, we've demonstrated the horizontal centering of both text and images to help clarify the concept.

 

<head>
    <style>
        .center-text {
            font-size: 20px;
            border: 1px solid black;
            text-align: center;
        }
    </style>
</head>

<body>
    <div class="center-text">
        <p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Ratione, nostrum.</p>
        <img src="https://media.istockphoto.com/id/1295274245/photo/random-multicolored-spheres-computer-generated-abstract-form-of-large-and-small-balls-3d.jpg?s=612x612&w=0&k=20&c=q7NOl28YxIIOqKu6em50VlKrg6ISFyVww_nLOCr5W_A="
            height="200px" alt="">
    </div>
</body>

 

Center text

 

 

2. Using the Justify-Content property for horizontal center

 

The "justify-content" property enables horizontal text centering, but only when the same element has "display: flex." Without "display: flex," the text won't be centered.

 

In this example, we'll create a div element similar to the one shown above. To turn the div into a flex container, we'll set its display property to flex, and justify-content property to center.

 

<head>
    <style>
        .center-text {
            display: flex;
            font-size: 20px;
            border: 1px solid black;
            justify-content: center;
        }
    </style>
</head>

<body>
    <div class="center-text">
        <p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Ratione, nostrum.</p>
    </div>
</body>

 

Center text

 

 

3. Using the Padding property for vertical center

 

By employing the padding property, we can center text vertically. The text utilizes the padding property to define two values: one for top and bottom, and one for left and right. The initial value can be either a positive length or a percentage to achieve centering, while the second value should be set to 0.

 

<head>
    <style>
        .center-text {
            font-size: 20px;
            display: flex;
            border: 1px solid black;
            padding: 100px 0;
        }
    </style>
</head>

<body>
    <div class="center-text">
        <p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Ratione, nostrum.</p>
    </div>
</body>

 

Center text

 

After demonstrating how to horizontally center text with two properties, you may be wondering about vertical centering. To achieve this, set "justify-content: center" in the container of the first element. This will vertically center the text, allowing for both vertical and horizontal text centering.

 

 

4. Using the Align-Items property for vertical center

 

The "align-items" property facilitates vertical text centering. By applying "display: flex" and adjusting the element's height, you can vertically center the text. If the height isn't specified and "align-items" is used alone, the centering will match the default height of the element.

 
<head>
    <style>
        .center-text {
            display: flex;
            font-size: 20px;
            border: 1px solid black;
            height: 50vh;
            align-items: center;
        }
    </style>
</head>

<body>
    <div class="center-text">
        <p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Ratione, nostrum.</p>
    </div>
</body>
 
Center text

 

 

Conclusion

 

To center any div or text within another div, you can select any one of the top 4 methods given above. However, many developers use the display grid + place item to center the div horizontally and vertically.

 

If you have any questions about this article or related to web development, feel free to ask us using the question box provided below.

Didn't find your answer? Add your question.

Share

Comments (0)

No comments yet. Be the first to comment!

About Author

Username

Meet Soni ( meetsoni )

Software Engineer

Joined On 13 Feb 2024

More from Meet Soni

Top 10 React Carousel Component Libraries for 2024

react-js

9 Jul 2024

Discover the top 10 React carousel libraries for 2024. Choose the best carousel based on i....

What is the spread operator in JavaScript with example?

javascript

9 Jul 2024

Learn how we can easily manipulate arrays, strings, and objects with the help of the sprea....

Top 4 Ways Centering a Div or Text Inside a Div using CSS

css

4 May 2024

Learn 4 techniques to make it easier to center a div container both horizontally and verti....

20 Amazing CSS Properties You Need to know - Part 2

css

4 May 2024

Learn about such properties of CSS with which you can easily make your website more attrac....

20 Amazing CSS Properties You Need to know - Part 1

css

9 May 2024

Learn 10 CSS properties that many developers do not know about. By understanding these all....

30 Keyboard Shortcuts You Might Not Know for Windows

programming

4 May 2024

Learn 30 Windows shortcut keys that will make it easier to use the system. In this post, w....

Popular Posts from Code Mafias

10 Fun Websites for Stress Relief and Relaxation During Coding Breaks

programming

11 Nov 2024

Top 10 fun websites for coders to relax during breaks. Recharge with interactive games, ar....

Mastering HTML: Top 12 Unique HTML Tags with Examples

html

4 May 2024

Through this article, learn those essential HTML tags that many developers do not know abo....

Top 60 Eye-Catching Button Designs Users Can’t Wait to Click - With Source Code

ui-ux

11 Oct 2024

Discover 60 eye-catching button designs with source code, crafted with HTML and CSS to enh....

How to Upload Code to GitHub: Quick Steps and Detailed Instructions for Beginners

github

16 Sep 2024

In order to upload (push) your project to GitHub, it involves multiple steps that need to ....

How to install MongoDB on windows in 2024

mongodb

2 May 2024

MongoDB is a Database Management System based on NoSQL (Not Only SQL) and utilizes JSON-li....

How to Implement Undo Functionality for Deleting Items in Your React App

react-js

28 Sep 2024

Learn how to implement undo functionality for deleting items in React. Follow a step-by-st....