Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other Why can't we center something vertically using margin: auto 0;?

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #200560
    randomdude
    Participant

    When centering something horizontally in CSS, a common solution for block level elements is:

    margin: 0 auto;
    

    So, to center vertically, why doesn’t

    margin: auto 0;
    

    work? Does anyone know why we can center horizontally like that, but not vertically?

    EDIT:

    I know it’s because the W3C spec says that

    If ‘margin-top’, or ‘margin-bottom’ are ‘auto’, their used value is 0.

    What I want to know is why they made it be 0 for top and bottom margins, but not for left and right margins.

    #200563
    Paulie_D
    Member

    What I want to know is why they made it be 0 for top and bottom margins, but not for left and right margins.

    OK…the obvious answer is to ask the people who created the spec.

    Other than that…I’d have to think about it.

    It’s probably something really simple like there is no default height for block-level elements but there is for widths (100%).

    #200604
    randomdude
    Participant

    @nkrisc and @shaneisme Thanks for answering, guys!

    #201223
    michaellee
    Participant

    @nkrisc that definitely makes sense to me. Thanks for sharing those similarities with physical paper documents. Helped me visualize the reasoning better.

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘Other’ is closed to new topics and replies.