DEV Community

Cover image for 5 Ways to Center A Div
Jon Snow
Jon Snow

Posted on

7 2 2 2 2

5 Ways to Center A Div

Watch Tutorial

Don't miss the amazing video we've embedded in this post! Click the play button to be inspired


1. Using flexbox

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

Enter fullscreen mode Exit fullscreen mode

2. Using grid

.parent {
    display: grid;
    place-content: center;
}
Enter fullscreen mode Exit fullscreen mode

3. Using Position

.parent {
    position: relative;
}

.child {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}
Enter fullscreen mode Exit fullscreen mode

4. Using Flex & Margin

.parent {
    display: flex;

}

.child {
    margin: auto;
}
Enter fullscreen mode Exit fullscreen mode

5. Using Grid & Margin

.parent {
    display: grid;
}

.child {
    margin: auto;
}
Enter fullscreen mode Exit fullscreen mode


Latest Post



Thanks for Reading ❤️! Check my website Demo coding for updates about my latest CSS Animation, CSS Tools, and some cool web dev tips. Let's be friends!

Don't forget to subscribe to our channel : Demo code

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay