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

543

u/kelinu Jun 24 '14

It's funny how people can do this and even freaking 3D graphics on the web yet I can't get elements to centre on a page without breaking everything.

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.

→ More replies (9)
→ 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.

→ More replies (6)
→ More replies (27)
→ More replies (2)

41

u/[deleted] Jun 24 '14

I don't know if it's just me but CSS feels horribly non-deterministic. After googling for solution to problems for half a hour it's usually.

  1. The stylings do nothing because you have a slightly different order of elements or you've hit an edge case scenario.

  2. They horribly break something else that would require 5x the amount of work to fix.

  3. 1 out of 10 times it works after fiddling around with DOM inspector and tweaking. But it's a hack that is not IE compatible or even the recommended solution. But hey, it works.

21

u/[deleted] Jun 24 '14

CSS can be a biggoh pain in the ass, definitely when you don't fully understand how it works. I've been doing frontend web dev for about a decade and I still get into pickles like you describe. Here's some quick tips that may make your life easier:

  1. The most-specific selector wins, i.e. styles under .foo will be overwritten by the styles of .foo a or #bar. IDs are more-specific than classes.

  2. Add a class to those elements or an ID to their container so you can pinpoint your styles or figure out a more-specific selector.

  3. There's always a solution that works, even if its just using an image and saying "fuck it!" And if you have to sacrifice a bit of user experience on IE7, so be it.

Also, using SASS or LESS can help a ton. I suggest using them, but only after you have a firm grasp on normal CSS. Here's my favorite Windows compiler (less and sass): Prepros.

*Prepos works for OSX too, neat.

→ More replies (2)

5

u/ToucheMonsieur Jun 24 '14

Agreed. For styling ui's, I find CSS ridiculously unintuitive and full of hacks for any non-trivial project. I'm aware this is probably due to a lack of experience, and CSS isn't just designed to style ui's, but 'tis still rather aggravating at times. The syntax isn't bad compared to many of the alternatives (declarative XML styling, anybody?), but how CSS actually functions never really aligns to expectations.

5

u/sizlack Jun 24 '14

Usually the problems are caused by too much CSS. Most developers just pile on HTML5 Boilerplate, Bootstrap, and whatever else, then half their styles are overriding the styles in the stupid frameworks they're using. And they don't know how CSS really works, so they have no idea how to organize their code or encapsulate their styles properly. CSS is deterministic. It's just that so few people bother to really learn how it works that 99% of the code they write is absolute shit.

→ More replies (1)

5

u/ggggbabybabybaby Jun 24 '14

Easy, just force everyone to browse your website with IE6 at 1024x786. Show an error message for everyone else.

→ More replies (10)