25
Spent 4 hours trying to center a div with CSS
I was building my first webpage and wanted a box right in the middle. I tried margin: auto, text-align, flexbox, and nothing worked. Turns out I forgot to set a width on the div, so the browser didn't know what to center. It was such a simple fix after all that time. Has anyone else gotten stuck on something that should have been easy?
3 comments
Log in to join the discussion
Log In3 Comments
diana201mo ago
Oh honey, four hours is nothing. I once spent a whole weekend fighting with a float that wouldn't clear. It's just part of the process.
8
Man, that width thing gets everyone at first. I remember trying to center a login box and it just kept clinging to the left side. You finally get it working and it feels so obvious, right? Like, of course the browser needs to know how wide the thing is before it can center it.
6
josephbutler27d ago
Width plus margin: 0 auto was the magic combo for me. Felt like a cheat code after fighting it for hours.
8