Aligning a float:left div to center?

前端 未结 6 1745
我寻月下人不归
我寻月下人不归 2020-12-12 13:39

I want to have a group of images display horizontally across the page. Each image has a few link below it so I need to put a container around each image/link-group.

6条回答
  •  攒了一身酷
    2020-12-12 14:10

    CSS Flexbox is well supported these days. Go here for a good tutorial on flexbox.

    This works fine in all newer browsers:

    #container {
      display:         flex;
      flex-wrap:       wrap;
      justify-content: center;
    }
    
    .block {
      width:              150px;
      height:             150px;
      background-color:   #cccccc;
      margin:             10px;        
    }
    1
    2
    3
    4
    5

    Some may ask why not use display: inline-block? For simple things it is fine, but if you got complex code within the blocks, the layout may not be correctly centered anymore. Flexbox is more stable than float left.

提交回复
热议问题