Align 3 unequal blocks left, center and right

前端 未结 4 1017
春和景丽
春和景丽 2020-11-22 06:18

I\'m trying to align a top menu which consists of 3 blocks of content.

What I\'m trying to achieve is this:

  • block 1: left aligned
  • block 2: cen
相关标签:
4条回答
  • 2020-11-22 06:46

    Alternative using display table (an ancient supported grid).

    Quote from https://www.w3schools.com/cssref/pr_tab_table-layout.asp

    If no widths are present on the first row, the column widths are divided equally across the table, regardless of content inside the cells

    .container {
       display: table;
       table-layout: fixed
     } // would divide cells equally along table's 100% width. 
    .row {
       display: table-row
     }
    .item {
       display: table-cell
     }
    
    0 讨论(0)
  • 2020-11-22 06:53

    I asked what seems to be a very similar question and stack overflow directed me here. The response from @Paolamoralesval inspired me to realise the required effect can be achieved in CSS grid. Now that grid support is pretty much universal I hope that this meets everyone's needs. This solution is I believe fully responsive to window size as well as height and width of the header items as you should see if you resize the window where you view the snippet.

    .header {
      grid-row: 1;
      grid-column: 1;
      display: grid;
      grid-template-rows: min-content;
      grid-template-columns: 1fr 1fr 1fr;
    }
    .header-left {
      justify-self: start;
      align-self: center;
      text-align: left;
      background-color: red;
    }
    .header-center {
      justify-self: center;
      align-self: center;
      text-align: center;
      background-color: green;
    }
    .header-right {
      justify-self: end;
      align-self: center;
      text-align: right;
      background-color: blue;
    }
    .shrink-kitty {
        width: 200px;
    }
    <html>
      <body>
        <div class="page">
          <div class="header">
              <div class="header-left">
                <img class="shrink-kitty" src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/bb/Kittyply_edit1.jpg/1280px-Kittyply_edit1.jpg"/><br/>
                By David Corby<br/>
                Edited by: <a href="//commons.wikimedia.org/wiki/User:Arad" title="User:Arad">Arad</a><br/><a href="//commons.wikimedia.org/wiki/File:Kittyplya03042006.JPG" title="File:Kittyplya03042006.JPG">Image:Kittyplya03042006.JPG<a><br/><a href="https://creativecommons.org/licenses/by/2.5" title="Creative Commons Attribution 2.5">CC BY 2.5</a>, <a href="https://commons.wikimedia.org/w/index.php?curid=1839754">Link</a>
              </div>
              <div class="header-center">In the middle</div>
              <div class="header-right">
                Much much much much more on the right hand side</br>
                Indeed two lines
              </div>
          </div>
          <div class="body">Body of the page</div>
          <div class="footer">At the bottom</div>
        </div>
      </body>
    </html>

    0 讨论(0)
  • 2020-11-22 06:58

    can you give flex-grow:1 for the item class and check

    .item {
    background-color: blue;
    color: #fff;
    padding: 16px;
    flex-grow:1;
    }
    

    Hope this is what you are looking for

    0 讨论(0)
  • 2020-11-22 07:01

    You can consider flex-grow:1;flex-basis:0% for the left and right elements then use text-align to align content inside. I have added an extra wrapper to keep the background only around the text.

    The trick is to calculate the free space by removing only the middle content and split it equally to the left and right element.

    .container {
      margin: 20px 0;
      padding-top:10px;
      background:linear-gradient(#000,#000) center/5px 100% no-repeat; /*the center*/
    }
    
    .row {
      background-color: lime;
      display: flex;
      color: #fff;
    }
    
    .item:not(:nth-child(2)) {
      flex-basis: 0%;
      flex-grow: 1;
    }
    
    .item:last-child {
      text-align: right;
    }
    
    .item span{
      background-color: blue;
      display:inline-block;
      padding: 16px;
      height:100%;
      box-sizing:border-box;
    }
    <div class="container">
      <div class="row">
        <div class="item"><span>left, slightly longer</span></div>
        <div class="item"><span>center, this item is much longer</span></div>
        <div class="item"><span>right</span></div>
      </div>
    </div>

    You can also do the same by keeping the element close. Simply adjust text-align:

    .container {
      margin: 20px 0;
      padding-top:10px;
      background:linear-gradient(#000,#000) center/5px 100% no-repeat; /*the center*/
    }
    
    .row {
      background-color: lime;
      display: flex;
      color: #fff;
    }
    
    .item:not(:nth-child(2)) {
      flex-basis: 0%;
      flex-grow: 1;
    }
    
    .item:first-child {
      text-align: right;
    }
    
    .item span{
      background-color: blue;
      display:inline-block;
      padding: 16px;
      height:100%;
      box-sizing:border-box;
    }
    <div class="container">
      <div class="row">
        <div class="item"><span>left, slightly longer</span></div>
        <div class="item"><span>center, this item is much longer</span></div>
        <div class="item"><span>right</span></div>
      </div>
    </div>

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