Aligning a float:left div to center?

前端 未结 6 1746
我寻月下人不归
我寻月下人不归 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 13:47
    .contentWrapper {
        float: left;
        clear: both;
        margin-left: 10%;
        margin-right: 10%;
    }
    
    .repeater {
        height: 9em;
        width: 9em;
        float: left;
        margin: 0.2em;
        position: relative;
        text-align: center;
        cursor: pointer;
    }
    
    0 讨论(0)
  • 2020-12-12 13:52

    use display:inline-block; instead of float

    you can't centre floats, but inline-blocks centre as if they were text, so on the outer overall container of your "row" - you would set text-align: center; then for each image/caption container (it's those which would be inline-block;) you can re-align the text to left if you require

    0 讨论(0)
  • 2020-12-12 13:59

    Just wrap floated elements in a <div> and give it this CSS:

    .wrapper {
    
    display: table;
    margin: auto;
    
    }
    
    0 讨论(0)
  • 2020-12-12 14:05

    Perhaps this what you're looking for - https://www.w3schools.com/css/css3_flexbox.asp

    CSS:

    #container {
      display:                 flex;
      flex-wrap:               wrap;
      justify-content:         center;
    }
    
    .block {
      width:              150px;
      height:             150px;
      margin:             10px;        
    }
    

    HTML:

    <div id="container">
      <div class="block">1</div>    
      <div class="block">2</div>    
      <div class="block">3</div>          
    </div>
    
    0 讨论(0)
  • 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;        
    }
    <div id="container">
      <div class="block">1</div>    
      <div class="block">2</div>    
      <div class="block">3</div>    
      <div class="block">4</div>    
      <div class="block">5</div>        
    </div>

    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.

    0 讨论(0)
  • 2020-12-12 14:11

    try it like this:

      <div id="divContainer">
        <div class="divImageHolder">
        IMG HERE
        </div>
        <div class="divImageHolder">
        IMG HERE
        </div>
        <div class="divImageHolder">
        IMG HERE
        </div>
        <br class="clear" />
        </div>
    
        <style type="text/css">
        #divContainer { margin: 0 auto; width: 800px; }
        .divImageHolder { float:left; }
        .clear { clear:both; }
        </style>
    
    0 讨论(0)
提交回复
热议问题