HTML center content through margin auto not working

前端 未结 6 1643
轻奢々
轻奢々 2021-01-19 12:59

I have the following Html code

Team

相关标签:
6条回答
  • 2021-01-19 13:53

    Images are inline level elements by default. You need to use display:block; if you want your images and margin to work properly.

    img{
     display: block;
    }
    
    0 讨论(0)
  • 2021-01-19 13:55

    Team needs to have a width to be able to use margin: auto.

    div#team {
        margin: 0 auto;
        width: 400px;
    }
    

    Here is a fiddle: JS Fiddle

    0 讨论(0)
  • 2021-01-19 13:59

    float will upset this too.. float:none; does the trick if you think it may be inheriting a 'float' directive from another rule somewhere.

    0 讨论(0)
  • 2021-01-19 14:02
    #team { 
        margin: auto; 
        width: 400px;
    }
    

    Basically margin depends on width if and only if we want to show our div in the center of the page.

    0 讨论(0)
  • 2021-01-19 14:03

    *Use the display:block; for the images classes.*

    0 讨论(0)
  • 2021-01-19 14:04

    You need to set a width to #team, for example:

    div#team {
       margin: 0 auto;
       width: 800px;
    }
    

    ... which is a shorthand version of:

    div#team {
        margin-top: 0;
        margin-left: auto;
        margin-bottom: 0;
        margin-right: auto;
        width: 800px;
    }
    
    0 讨论(0)
提交回复
热议问题