Live data from Hacker News

How to Center in CSS (2015)

howtocenterincss.com

11–20 of 74 posts

Re: How to Center in CSS (2015)

#11
post #10

Here's what I've been doing for years (The can be any element, like ): WFM YMMV

you really use the same id twice on a page?! Heresey! Also.. I do the same (essentially: margin: 0 auto) but it works with display: block too, and you don't need text-align center on the parent.

It's not the same id: wrapper vs wrapped

Re: How to Center in CSS (2015)

#12
post #4
post #2

This is a straw man. Today you can simply use flexbox on the container: display: flex; justify-content: center; // horizontal align-items: center; // vertical Doesn't work in IE without a JS polyfill (which is available), but has worked in all other browsers for six years or so.

Flexbox (and grid) have made frontend work tolerable again for me. You can do in a handful of lines what used to take all kinds of jiggerypokery, hacks and extra markup to wrap things.

For me as well, specially grid, since it was originally submitted by XAML team and is heavily influenced by its grid layout engine.

Re: How to Center in CSS (2015)

#13
post #2

This is a straw man. Today you can simply use flexbox on the container: display: flex; justify-content: center; // horizontal align-items: center; // vertical Doesn't work in IE without a JS polyfill (which is available), but has worked in all other browsers for six years or so.

What does a JS polyfill have to do with CSS? Also, try that out in IE 11 without a polyfill.

This works in IE without a JavaScript polyfill (lol): TESTING

Re: How to Center in CSS (2015)

#15
Pinboard tells me, I "previously saved april 2015". So I bookmarked this more than five years ago.

Back then, this site was addressing a big problem and, to some degree, it's still a confounding problem sometimes.

So high-five to its creator. And to the HN haterz in these comments, take a hike.

Re: How to Center in CSS (2015)

#16
post #6
post #2

This is a straw man. Today you can simply use flexbox on the container: display: flex; justify-content: center; // horizontal align-items: center; // vertical Doesn't work in IE without a JS polyfill (which is available), but has worked in all other browsers for six years or so.

Yes, with flex we don't need anything else. If flex is not available, the browser is supposedly too old to care if we used tables for layout so I'll just throw in `display: table-cell`.

that can cause some issues in Chrome oddly enough, depending on how you're using it.

Re: How to Center in CSS (2015)

#17

I am not into the CSS game anymore, if anything needs to be done I'd just go with whatever widgets any CSS framework offers, but why has it been so hard for so long to center things with CSS (I believe flexbox finally fixed it) ?

I just throw a in, it works well enough for most cases.

Re: How to Center in CSS (2015)

#18
post #2

This is a straw man. Today you can simply use flexbox on the container: display: flex; justify-content: center; // horizontal align-items: center; // vertical Doesn't work in IE without a JS polyfill (which is available), but has worked in all other browsers for six years or so.

That is exactly the code the website will generate if you select no IE support.

Actually it also works in IE11, as it should. Flex does work in IE11 with some known bugs - https://caniuse.com/#search=display%20flex

Re: How to Center in CSS (2015)

#19
post #9

Earlier quoted context omitted.

That is exactly the code the website will generate if you select no IE support.

Yet it opens with this misleading blurb: > Centering in CSS is a pain in the ass. There seems to be a gazillion ways to do it, depending on a variety of factors. Which is no longer true in the great majority of cases.

It is always the minority of cars that kill you. If it only has to 95% work, that’s always been easy.
Post reply on HN