this post was submitted on 20 Dec 2025
7 points (100.0% liked)

CSS

666 readers
1 users here now

founded 2 years ago
MODERATORS
 

I have a .container div just after body that contains everything in page how to center it?

top 9 comments
sorted by: hot top controversial new old
[–] teft@piefed.social 6 points 5 days ago (1 children)

https://www.w3schools.com/howto/howto_css_center_website.asp

Use a container element and set a specific max-width . A common width many websites use is 960px. To actually center the page, add margin: auto .

[–] claim_arguably@lemdro.id 2 points 5 days ago* (last edited 5 days ago) (2 children)

How to make it centered vertically as well?

[–] teft@piefed.social 2 points 5 days ago

I would suggest poking around on that page to find what you're looking for. It has a decent search function.

[–] emb@lemmy.world 2 points 5 days ago (1 children)
[–] claim_arguably@lemdro.id 2 points 5 days ago (2 children)

Is it a good idea to make body a flexbox? Is there any standards for doing this?

[–] brian@programming.dev 3 points 5 days ago

there are more aggressive css "resets" that set the default display type to flex. there's no problem making anything a flexbox if it displays the way you want it to

[–] emb@lemmy.world 2 points 5 days ago

Not sure what best practice is broadly, but in general display: flex is the first tool I reach for if I need to position child elements a little more carefully.

It's good for most things more complex than just margin/padding/text-align. If it gets too complex for flexbox, dig into CSS grid.

[–] emmanuelw@jlai.lu 2 points 5 days ago (1 children)

Why not body {margin: auto;}?

[–] claim_arguably@lemdro.id 2 points 5 days ago

I'm a beginner doing the odin project. So i don't really know.