r/programming • • Jun 24 '14

Simpsons in CSS

http://pattle.github.io/simpsons-in-css/
2.2k Upvotes

371 comments sorted by

View all comments

Show parent comments

394

u/Cuddlefluff_Grim Jun 24 '14

"Want to vertically center something? TOO BAD, FUCKER!" -w3c

40

u/Norci Jun 24 '14

I'll be just sitting in my corner, waiting on flexbox to become standard.. ;(

→ More replies (5)

49

u/Town-Portal Jun 24 '14

I know this might be a joke post, but here is how i vertical align everything.

.children
{ top: 50%; transform: translateY(-50%); }

Easiest way to vertically align any object, even unknown heights.

19

u/prajo2 Jun 24 '14

That's great, but still seems like a workaround to me. Why can't we just have vertical-align: middle?

17

u/Seeders Jun 24 '14

The problem here is three-fold:

  1. HTML layout traditionally was not designed to specify vertical behavior. By its very nature, it scales width-wise, and the content flows to an appropriate height based on the available width. Traditionally, horizontal sizing and layout is easy; vertical sizing and layout was derived from that.

  2. The reason vertical-align:middle isn't doing what is desired is because the author doesn't understand what it's supposed to do, but …

  3. … this is because the CSS specification really screwed this one up (in my opinion)—vertical-align is used to specify two completely different behaviors depending on where it is used.

http://phrogz.net/CSS/vertical-align/

6

u/Town-Portal Jun 24 '14

Agree, but i just work with the tools i am given.
Also, vertical-align: middle works for some stuff, text etc.

4

u/thisisafullsentence Jun 24 '14 edited Jun 24 '14

You can with 2 inline-block elements next to each other:

.one { display: inline-block; height: 100%; vertical-align: middle }

.two { display: inline-block; vertical-align: middle; }

Dynamic heights! woo

Edit: Japanese flag example on Plunker

4

u/alexanderpas Jun 24 '14

Try this and be surprised:

html, body {
  height: 100vh;
}
<element> {
  height: <value>;
  margin-top: auto;
  margin-bottom: auto;
}
→ More replies (2)
→ More replies (4)

19

u/SquareWheel Jun 24 '14

Interesting technique. Transforms are still pretty unsupported, sadly.

I wait patiently for the day that the majority of users are on auto-updating browsers.

7

u/oocha Jun 24 '14

85% is pretty unsupported?

39

u/SquareWheel Jun 24 '14

Definitely. If 15% of your users are not showing the correct layout, you have a major problem. And that's assuming you're using CSS prefixes; you lose at least 50% if you're using the rule unprefixed.

8

u/nj47 Jun 24 '14

Why wouldn't you be using prefixes? That point seems completely irrelevant.

Something to consider though is 85% of global users != 85% of YOUR users. For example, the average across my websites is roughly 5% Internet explorer (any version). I don't remember the specific percent of those who are greater than IE 8, but it is over 50% I believe.

So in those cases, only 2% of users are affected, and I already show them a warning that the site won't work in their browser and they need to update to a modern browser (if lower than IE9, not IE in general)

On the flipside, I used to do work for a healthcare company. Internet explorer was close to 80% of our visitors (mostly older people). On that project, I was fully supporting IE6 as that was 20% of the viewers! And this was less than a year ago!

→ More replies (3)
→ More replies (2)

2

u/jvnk Jun 24 '14

85% of average users, for niche industries it's a whole lot less. Even 15% is still too high.

→ More replies (7)

144

u/lobehold Jun 24 '14 edited Jun 24 '14

Use tables.

Edit: Downvote me all you like, you can choose between using a table or 20 divs with strange CSS hacks to get them to barely vertically center which breaks in strange edge cases.

People say don't use table for layout because it's not "semantic", but neither is using a shit ton of divs. There is also no consequences if you want to restyle because you can change the "display" property of tables to act like divs but not vice versa due to lack of browser support for the different variations of display: table, table-row, table-cell.

And I haven't actually seen any real-world harm in using tables for layout when done with restraint. I think a lot of people just read some old articles about "tables are bad" from outspoken web designers and regurgitated that back out as if it's their own opinion.

43

u/CSMastermind Jun 24 '14

There are definitely cases where tables are way easier, the trick is using them sparingly.

20

u/lobehold Jun 24 '14

Agreed, table should be used only when it makes sense, the point is that sometimes it DOES make sense to use tables.

I think a lot of people went from one extreme (use tables for everything) to another (no table for layout ever, even if it meant doing extremely backwards-bending, convoluted CSS hacks that barely works).

9

u/Neebat Jun 24 '14

Spare the table, spoil the child element.

26

u/Carlos_Sagan Jun 24 '14

I hate tables. I use this instead.

<div style="display:table;">
<div style="display:table-cell; vertical-align:middle;">
     Your vertically centered content.
</div></div>

19

u/drysart Jun 24 '14

You're turning divs into a table through CSS, so all you've effectively done is changed the name of the HTML element you use. I don't see how that's a "better" solution on any level.

14

u/Carlos_Sagan Jun 24 '14

Well a table usually has unique styles in the stylesheet. It's easier to start with a plain div than it is to remove all of the default table styling.

→ More replies (1)
→ More replies (9)
→ More replies (14)

12

u/Nickoladze Jun 24 '14 edited Jun 24 '14

Tables make your website really difficult for screen readers (blind people). They think you're trying to display tabular data with named columns (the thing that tables are supposed to do).

If you're just trying to center some text, the screen reader would say something like "row 1, column 1, blank. row 1, column 2, Welcome to My Website! row 1, column 3, blank". What a nightmare.

14

u/lwl Jun 24 '14

That argument was made in the first book about HTML I bought, 12 years ago. I bet it's a tricky problem to solve, but in that time screen reader designers could have come up with some workarounds at their end.

→ More replies (4)
→ More replies (1)

2

u/sharrice Jun 24 '14

I think the point is that using tables to center certain things is considered a hack. I'd much rather use flex-display, or try auto margins before I think of using tables.

2

u/sizlack Jun 24 '14
display: table-cell
→ More replies (25)
→ More replies (2)