How to center block of inline-blocks?

前端 未结 2 980
有刺的猬
有刺的猬 2020-12-04 01:14

How do I make the green box (.b) tight around its contents so that I can horizontally center it?

相关标签:
2条回答
  • 2020-12-04 01:24

    As I mentioned in my comment that I wasn't sure if it could be done with CSS alone, I created a quick JS/jQuery solution to calculate the width based on the first row of contents.

    function resizeGrid() {
      var rowPos = 0;
      var col = 0;
      $('.b').removeAttr('style');
      $('.c').each(function() {
        var thisRowPos = $(this).offset().top;
        if (thisRowPos != rowPos) {
          if (rowPos != 0) {
            /* Break out of the $.each() loop */
            return false;
          } else {
            col++;
          }
          rowPos = thisRowPos;
        } else {
          col++;
        }
      });
      $('.b').css('width', $('.c').outerWidth(true) * col + 'px');
      console.log('Number of columns: ' + col);
    }
    
    $(window).on('resize', resizeGrid);
    resizeGrid();
    * {
      -moz-box-sizing: border-box;
      -ms-box-sizing: border-box;
      -o-box-sizing: border-box;
      -webkit-box-sizing: border-box;
      box-sizing: border-box;
    }
    
    .a {
      border: 1px solid red;
    }
    
    .b {
      margin: 0 auto;
      background: lightgreen;
    }
    
    .b:after {
      content: '';
      display: block;
      width: 100%;
      height: 0;
      clear: both;
    }
    
    .c {
      padding: 5px;
      height: 100px;
      display: inline-block;
      float: left;
    }
    
    .c table {
      background-color: blue;
      height: 100%;
      width: 100%;
      float: left;
    }
    
    .c table td {
      vertical-align: top;
    }
    
    body {
      padding: 50px;
    }
    <div class="a">
      <div class="b">
        <div class="c">
          <table>
            <tbody>
              <tr>
                <td>x x x x x</td>
              </tr>
            </tbody>
          </table>
        </div>
        <div class="c">
          <table>
            <tbody>
              <tr>
                <td>x x x x x</td>
              </tr>
            </tbody>
          </table>
        </div>
        <div class="c">
          <table>
            <tbody>
              <tr>
                <td>x x x x x</td>
              </tr>
            </tbody>
          </table>
        </div>
        <div class="c">
          <table>
            <tbody>
              <tr>
                <td>x x x x x</td>
              </tr>
            </tbody>
          </table>
        </div>
        <div class="c">
          <table>
            <tbody>
              <tr>
                <td>x x x x x</td>
              </tr>
            </tbody>
          </table>
        </div>
        <div class="c">
          <table>
            <tbody>
              <tr>
                <td>x x x x x</td>
              </tr>
            </tbody>
          </table>
        </div>
        <div class="c">
          <table>
            <tbody>
              <tr>
                <td>x x x x x</td>
              </tr>
            </tbody>
          </table>
        </div>
        <div class="c">
          <table>
            <tbody>
              <tr>
                <td>x x x x x</td>
              </tr>
            </tbody>
          </table>
        </div>
        <div class="c">
          <table>
            <tbody>
              <tr>
                <td>x x x x x</td>
              </tr>
            </tbody>
          </table>
        </div>
        <div class="c">
          <table>
            <tbody>
              <tr>
                <td>x x x x x</td>
              </tr>
            </tbody>
          </table>
        </div>
        <div class="c">
          <table>
            <tbody>
              <tr>
                <td>x x x x x</td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    0 讨论(0)
  • 2020-12-04 01:27

    Here is a solution that will not make the .b to be smaller in order to fit all the .c but it will make the .c to be stretched a litte in order to avoid having the white spaces.

    UPDATE

    I added some media queries to make sure all the elements always stay the same width. Of course this is based on exact values, so for a generic solution we may need to include calc or some variable.

    * {
      box-sizing: border-box;
    }
    
    .c {
      background-color: blue;
      margin: 5px;
      height: 100px;
      min-width: 80px;
      flex: 1;
      display: inline-block;
    }
    
    .a {
      border: 1px solid red;
      display: flex;
    }
    
    .b {
      margin: 0 auto;
      border: 1px solid green;
      display: flex;
      flex-wrap: wrap;
    }
    
    .b:after {
      content: "";
      flex: 1;
    }
    
    @media all and (max-width:1110px) {
      .b:after {
        margin: 5px;
      }
    }
    
    @media all and (min-width:1020px) and (max-width:1110px) {
      .b:after {
        flex: 10;
      }
    }
    
    @media all and (min-width:930px) and (max-width:1020px) {
      .b:after {
        flex: 7.75;
      }
    }
    
    @media all and (min-width:840px) and (max-width:930px) {
      .b:after {
        flex: 5.5;
      }
    }
    
    @media all and (min-width:750px) and (max-width:840px) {
      .b:after {
        flex: 3.25;
      }
    }
    
    @media all and (min-width:570px) and (max-width:660px) {
      .b:after {
        flex: 4.33;
      }
    }
    
    body {
      padding: 50px;
    }
    <div class="a">
      <div class="b">
        <div class="c">
          <table>
            <tbody>
              <tr>
                <td>x x x x x</td>
              </tr>
            </tbody>
          </table>
        </div>
        <div class="c">
          <table>
            <tbody>
              <tr>
                <td>x x x x x</td>
              </tr>
            </tbody>
          </table>
        </div>
        <div class="c">
          <table>
            <tbody>
              <tr>
                <td>x x x x x</td>
              </tr>
            </tbody>
          </table>
        </div>
        <div class="c">
          <table>
            <tbody>
              <tr>
                <td>x x x x x</td>
              </tr>
            </tbody>
          </table>
        </div>
        <div class="c">
          <table>
            <tbody>
              <tr>
                <td>x x x x x</td>
              </tr>
            </tbody>
          </table>
        </div>
        <div class="c">
          <table>
            <tbody>
              <tr>
                <td>x x x x x</td>
              </tr>
            </tbody>
          </table>
        </div>
        <div class="c">
          <table>
            <tbody>
              <tr>
                <td>x x x x x</td>
              </tr>
            </tbody>
          </table>
        </div>
        <div class="c">
          <table>
            <tbody>
              <tr>
                <td>x x x x x</td>
              </tr>
            </tbody>
          </table>
        </div>
        <div class="c">
          <table>
            <tbody>
              <tr>
                <td>x x x x x</td>
              </tr>
            </tbody>
          </table>
        </div>
        <div class="c">
          <table>
            <tbody>
              <tr>
                <td>x x x x x</td>
              </tr>
            </tbody>
          </table>
        </div>
        <div class="c">
          <table>
            <tbody>
              <tr>
                <td>x x x x x</td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>

    0 讨论(0)
提交回复
热议问题