Swapnil Mane Profile picture
Dec 5, 2021 โ€ข 6 tweets โ€ข 4 min read
All the Ways to Center a Div in CSS! ๐Ÿ‘‡
Thread ๐Ÿงต

#CSS #Frontend #Javascript Image
โœ… For this, let's consider the following Image which is inside a div tag and the parent element of this div tag is the header tag.

So lets now look at all the ways we can center the div in CSS!๐Ÿ‘‡ ImageImage
1) Traditional Way:

For the parent element,
Set the position to 'relative' and height to '100vh'.

For the child element,
Set top, and left to '50%' and transform to 'translate(-50%,-50%)'

โญ And Done... Your Child Div is now set to Center! ImageImage
2) Using Flexbox:
โœ… Just add the following properties to the parent element and the child div tag will get centered automatically!

height: 100vh;
display: flex;
justify-content: center;
align-items: center; ImageImage
3) Using CSS Grid
โญ This is the Shortest and easiest way to center a div tag! โญ
Just add the following 3 lines to the parent element and the child div tag will get centered automatically!

height: 100vh;
display: grid;
place-items: center center; ImageImage
Great, now you know all the ways to center a div tag.๐Ÿ˜Š
Let me know if you know any other ways to center a div tag down below ๐Ÿ‘‡ or let me know if you have any questions!

And if you liked the thread, then consider sharing... It means a lot to me! โค

โ€ข โ€ข โ€ข

Missing some Tweet in this thread? You can try to force a refresh
ใ€€

Keep Current with Swapnil Mane

Swapnil Mane Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

:(