DEV Community

Cover image for 7 Ways to Escape CSS Hell

7 Ways to Escape CSS Hell

Ryland @ Stackbit on October 12, 2021

Ever have this happen? lol Yeah, me too. Here are the 7 ways to completely center whatever you want with CSS. 1. text-align: center; ...
Collapse
 
slaweksk13 profile image
Sławomir Skrzeczyński

{ display: grid;
place-items: center}

would do as well

Collapse
 
rylandking profile image
Ryland @ Stackbit

@slaweksk13 Good catch! This is why they have me writing articles and not the product's code. lol

See #7! 🙏❤️

Collapse
 
bcowley1220 profile image
Brendan Cowley

I know I'm a bit fried... but I only count 6.

Collapse
 
abhilearnstocode profile image
Abhii
  1. float: center; lol (You cannot center floated elements.)

That too with one lol thing 😂

Collapse
 
rylandking profile image
Ryland @ Stackbit

lol

Collapse
 
rylandking profile image
Ryland @ Stackbit

lol

Collapse
 
rylandking profile image
Ryland @ Stackbit

I actually missed one that @slaweksk13 caught. Lucky enough we now have 7!

Collapse
 
dippas profile image
dippas

some notes;

  • vertical-align applies to inline-level and table-cell elements. It also applies to ::first-letter and ::first-line

  • for alignment in flexbox there's no need align-content nor flex-direction:column

Collapse
 
chrisgreening profile image
Chris Greening

.please-god-why-have-you-forsaken-me

Collapse
 
rylandking profile image
Ryland @ Stackbit

Been there. Far too often. lol

Collapse
 
drmalus profile image
Szklenár Ferenc

Maybe i don't understand exactly, what problem this article was designed to solve.

I mostly use 2 or 3 solutions depending on the "situation".
You have an easy time, when you are working with flex or grid items because you can easily center elements horizontally, vertically or both in it.
If you are in block or inline block context margin: 0 auto, margin: x auto y auto, text align: center can works for horizontal centering. But as you mentioned, you need to set the width if your display property is inline-block, because in that case your element inherits the block and inline level behaviors.

If you want to center vertically and you have fix height, the line-height "hack" can work. Otherwise you can play with items height, margin, padding etc.
You have another help in the form of absolute positioning.
You can absolute position the item in both axis (x,y) with the value 50% then corrigate with transform translate(x,y), translateX or translateY if your container elements position is other than static.

I think you have much easier time if you use flex or grid.

Good to know that flex and grid also creates Block Formatting Context which suppress margin collapsing for example.

Collapse
 
rylandking profile image
Ryland @ Stackbit

It can be hard to both vertically and horizontally center elements. Goal was to show the ways to do that.

Collapse
 
drmalus profile image
Szklenár Ferenc

Understand. Thanks for your answer! :) Yes you are right.
But what about display:flex; justify-content: center; align-items: center solution?
It isn't worked for you in some cases or something?

Collapse
 
alvaroantonio profile image
álvaro antonio

1 and #6 read "text-image: center", but this is not valid css. I understand #1 was meant as text-align, but I don't get what you meant in #6...

Collapse
 
alexandrostzoumas profile image
Alexandros Tzoumas

6 should have title

"Centering with flexbox"

Collapse
 
rylandking profile image
Ryland @ Stackbit

*updated the post too. thank you!

Collapse
 
rylandking profile image
Ryland @ Stackbit

Good catch! I got tired towards the end of this. :-)

Collapse
 
rylandking profile image
Ryland @ Stackbit

I got a bit tired/lazy and overlooked #6's title. Just updated! 👍

Collapse
 
yoshida profile image
Masao Yoshida

vertical-align works on the table cell too.

Collapse
 
saroj8455 profile image
Saroj Padhan

Thank you

Collapse
 
rylandking profile image
Ryland @ Stackbit

Thanks for reading, Saroj!

Collapse
 
lanrecodes profile image
Lanre Sanni

I use
.center
{
display: flex;
justify-content: center;
align item: center;
}

Collapse
 
amk profile image
Amran AL Ketara

Thanks!

Collapse
 
code_rabbi profile image
Emeka Orji

CSS is not hell

Collapse
 
rylandking profile image
Ryland @ Stackbit

For some, my friend

Collapse
 
fahadattech profile image
fahad-attech

First Point confused me. I tried this but it works with block level elements too.
did a bit of research about it turns out its used for the horizontal alignment of content inside a block element.
If an element is wrapped inside a block level element its enough. Thats the only requirement to center using text-align : center.
Here is the mdn link developer.mozilla.org/en-US/docs/W...

Collapse
 
csemorf profile image
csemorf • Edited

😂

Collapse
 
andrewright profile image
Andrew Garbuzov

Boolshit!
vertical-align: middle;
Works just for table-cell
developer.mozilla.org/en-US/docs/W...

Collapse
 
mikaleb profile image
Mikaleb

Or you have the excellent howtocenterincss.com that does that

Collapse
 
dova profile image
doVa • Edited

"margin-inline: auto;" better than "margin: 0 auto;". But ie problem :D

Collapse
 
Sloan, the sloth mascot
Comment deleted