Live data from Hacker News

CSS is hard no matter how good you are at it

aha.io

21–30 of 83 posts

Re: CSS is hard no matter how good you are at it

#22
The problem is caused by vertical-align. The default value for vertical-align is `baseline`. The flexbox container inside pill2 "hides" the inner text, so pill2 is treated as having no text at all (for layout purposes). This is treated the same as a font having zero height. So the browser aligns a 16px font (or whatever) with a 0px font, and it comes out misaligned.

My two solutions in order of preference would be:

1. Get rid of the inner flexbox, so the layout engine can see through to the text inside and use its baseline for alignment. Rely on the pill's parent for vertical alignment. I don't think I've ever come across a double-nested (or div) that couldn't be flattened out.

2. Give .pill `vertical-align: middle` so its baseline is not used for alignment at all--but then the other non-pill items will need `vertical-align: middle` too in order to line up.

The author went the opposite way as #1 and added dummy text inside the pill, but outside the flex. Since the text is not inside a block element, the layout engine can see it. It has the same font size as the outer text, so with `vertical-align: baseline` both baselines line up again.

Re: CSS is hard no matter how good you are at it

#28
https://www.w3.org/TR/css-flexbox-1/#flex-baselines

    first/last main-axis baseline set
    When the inline axis of the flex container matches its main axis, its baselines are determined as follows:

    [...]

    Otherwise, if the flex container has at least one flex item, the flex container’s first/last main-axis baseline set is generated from the alignment baseline of the startmost/endmost flex item. [...]
It uses the first flex item (the first child controlled by the flexbox) to set the baseline for the flexbox.

(for https://codepen.io/jsteel64/pen/rNrPYNQ) The first img sets the baseline for the .wrapper, which sets it for the pill, and then two pills are aligned according to their baselines.

https://imgur.com/Bt4e6gU

And yes, anything that has to do with baselines is a source of pain and bugs, it sucks.

Re: CSS is hard no matter how good you are at it

#29
post #7

What tactics do employ when a CSS issue has stumped you?

!important generally.

The retort is that you should rely on specificity, by way of selectors.

Good thing that we have the specificity checker, that makes sure that the semantics of your application of styles remain sound if you change the structure of the document in ways that could affect the selectors!

Oh wait, there's no such thing, bummer.

Re: CSS is hard no matter how good you are at it

#30
EDIT See comment above, mostly related to vertical-align things, not flexbox. The author's point still stands: CSS is hard.

Smells like baseline / vertical alignment issues. The author hones in at the end,

  
    
      
      Text
    
  
The presences of that empty tag is what changes the alignment. This also requires the flex container to be wrapper in an inline-block element, and for the flex container to align-items: center.

The flexbox [spec][1] talks about how flex items become "blockified," but I don't see anything that indicates empty children which demonstrate this weird behavior.

[1]: https://www.w3.org/TR/css-flexbox-1/#flex-items

Post reply on HN