Forums

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

Home Forums CSS Browser support for using margin left/right to align left or right

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

    Can all (major) browsers left align or right align elements using margin-right: auto and margin-left: auto, respectively?

    I did this last year (I think) with Mozilla Firefox, and it didn’t work. But when I tried now, it actually aligns it perfectly. I just want to know if this is a valid way, and a relatively well supported method?

    Edit:

    I do not mean center aligning stuff (already know how to do that.) I mean, like this:

    https://jsfiddle.net/akhurshid/2sswp/

    And I do know about using floats to align. I just want to know if setting one property to be auto is “correct.”

    #239829
    Paulie_D
    Moderator

    Yes.

    As for what didn’t work last year…we can’t comment as we don’t know what you tried.

    That said, I can’t think doing it this way is very useful.

    #239830
    pcenero
    Participant

    Really? Horizontally aligning something without making the proceeding content flow seems useful, i.e.:

    .foobar {
        margin-left: auto;
    }
    

    vs.

    .foobar {
        float: right;
        clear: both;
    }
    
    #239831
    Paulie_D
    Moderator

    As a single element on a row sure…but put another element on the same row and you see why floats are usually used.

    https://jsfiddle.net/ggqymnes/

    These days, I’d use flexbox where it does make sense to use margin-x:auto

    https://jsfiddle.net/rukpv5gh/

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