Live data from Hacker News

How to Center in CSS

howtocenterincss.com

11–20 of 297 posts

Re: How to Center in CSS

#11
post #6

This might need some updating. I did a quick run through and it recommended me to use a table-cell for vertical centering. You might be able to do that more easily with flexbox.

Depends on what you need for browser support – flexbox is supported in fewer browser versions and requires using multiple rules because the spec changed significantly before finalizing:

http://caniuse.com/#feat=css-table

http://caniuse.com/#feat=flexbox

Re: How to Center in CSS

#12
The table-cell solution for vertically centering text is incorrect. You need to put that display:table-cell div inside a display:table div. It requires the parent in order to work properly.

Example:

Your text here

.item {display:table;} .item > div {display:table-cell;vertical-align:middle;}

I use this method on justindocanto.com to vertically center an unknown amount of text/elements inside a div that changes its size based on the size of the image that's inside it. Really nice technique when used correctly.

Re: How to Center in CSS

#13
Wow CSS is unfriendly, you really realise how poor it is once you learn other layout systems (android's XML and iOS auto layout, etc).

Wish there was another standard that could co-exist side by side on the web, something simple enough to be implemented reliably by the browser vendors.

Re: How to Center in CSS

#14
post #13

Wow CSS is unfriendly, you really realise how poor it is once you learn other layout systems (android's XML and iOS auto layout, etc). Wish there was another standard that could co-exist side by side on the web, something simple enough to be implemented reliably by the browser vendors.

> Wish there was another standard that could co-exist side by side on the web, something simple enough to be implemented reliably by the browser vendors.

That standard exists. It's called flexbox. It was designed to solve exactly this problem (among many others), and does.

That said, as I never tire of mentioning, vertical centering was a part of the design of CSS 2.1. The technique is called "absolute centering". See CSS 2.1 10.6.4 [1]: "If both 'margin-top' and 'margin-bottom' are 'auto', solve the equation under the extra constraint that the two margins get equal values."

[1]: http://www.w3.org/TR/CSS21/visudet.html#abs-non-replaced-hei...

Re: How to Center in CSS

#15
post #10

Doesn't Flexbox pretty much solve this problem? I mean, if you need to maintain backwards compatibility this might be reasonable, but "justify-content: center" and "align-items: center" seem to work unreasonably well for this exact purpose.

Flexbox solves this problem but it's still a bit unwieldy and verbose.

Re: How to Center in CSS

#17
post #15
post #10

Doesn't Flexbox pretty much solve this problem? I mean, if you need to maintain backwards compatibility this might be reasonable, but "justify-content: center" and "align-items: center" seem to work unreasonably well for this exact purpose.

Flexbox solves this problem but it's still a bit unwieldy and verbose.

Verbose? Compared to the other solutions it's a godsend, just use the CSS-tricks guide for it and you'll have nearly zero problems. I absolutely love it and pretty much refuse to use anything else for layout in CSS now.

Re: How to Center in CSS

#18

This is why css is fundamentally broken. Centering should be a feature in itself. I shouldn't be manipulating other attributes to achieve centering.

It is a feature of CSS 2.1. See my reply to chubs above. CSS 2.1 10.6.4 requires that "margin: auto" on both sides of an absolutely-positioned element mean "vertically center it".

Re: How to Center in CSS

#19
post #11
post #6

This might need some updating. I did a quick run through and it recommended me to use a table-cell for vertical centering. You might be able to do that more easily with flexbox.

Depends on what you need for browser support – flexbox is supported in fewer browser versions and requires using multiple rules because the spec changed significantly before finalizing: http://caniuse.com/#feat=css-table http://caniuse.com/#feat=flexbox

Which is why you use something like AutoPrefixer to automagically generate the needed browser prefixes and such, makes it a whole lot easier.
Post reply on HN