3 ms·
I often use this as an interview question for CSS people: "How would you vertically center a box inside another box." I like it because there's like four correc
by menssen 11y ago
I often use this as an interview question for CSS people: "How would you vertically center a box inside another box." I like it because there's like four correct answers, each with drawbacks. The general answers, from worst to best, are usually:
1. margin: auto (wrong)
2. top: 50% (mostly wrong)
3. negative margin-top (correct if you know the size)
4. table-cell or translateY (correct)
5. a correct answer plus a discussion about why it has to be a hack
FWIW, when you set everything to "Unknown", this site tells you to use display: table-cell. If you don't have to support IE8, I generally prefer "top: 50%; translate-Y: -50%"
[Edited for spacing.]