Understanding CSS table-cell and percentage width

后端 未结 2 1035
渐次进展
渐次进展 2020-12-31 17:10

I was checking how Github display the below menu:

\"enter

If you notice, each

相关标签:
2条回答
  • 2020-12-31 17:31

    This has to do with how automatic table layout works. In particular:

    A percentage value for a column width is relative to the table width. If the table has 'width: auto', a percentage represents a constraint on the column's width, which a UA should try to satisfy. (Obviously, this is not always possible: if the column's width is '110%', the constraint cannot be satisfied.)

    In your case, you're setting a minuscule percentage width on every table-cell element. But the browser needs to ensure that the table-cells fill up the width of the table (which itself is as wide as its containing block), so it has to expand the cells to occupy as much space within the table as possible.

    The reason why this results in approximately equal-width cells is because the percentage value is equal for all of them. If, for example, you set a slightly larger width for one of the cells, you'll see that it grows wider and the other cells become narrower to accommodate:

    div {
      border: 1px solid #000;
      border-radius: 4px;
    }
    
    div ul {
      padding: 0;
      margin: 0;
    }
    
    div ul li {
      display: table-cell;
      width: 1%;
      text-align: center;
      border-right: 1px solid #000;
      padding: 10px 0;
    }
    
    div ul li:first-child {
      width: 3%;
    }
    
    div ul li:last-child {
      border-right: 0;
    }
    <div>
      <ul>
        <li><a href="#">Commits</a>
        </li>
        <li><a href="#">Branch</a>
        </li>
        <li><a href="#">Contribution</a>
        </li>
        <li><a href="#">anything</a>
        </li>
      </ul>
    </div>

    However, note that there is a slight difference because some cells have longer content than others, and the length of this content is accounted for when calculating the cell widths.

    The reason why a value as small as 1% is used is so that you can continue to add cells and the browser will still try to distribute the available width as evenly as possible. You're essentially telling the cells that they don't need to be a certain width at minimum so you can add away (although technically, 1% is still something).

    The fact that no elements are assigned display: table is inconsequential, because an anonymous table wrapper will be generated around the table-cells so that they can render correctly. This anonymous table wrapper functions exactly the same as an unstyled element with display: table, which means the table width is auto for the purposes of calculating percentage cell widths.

    0 讨论(0)
  • 2020-12-31 17:35

    This is something called the 1% width table hack.

    This is because the table-cell inherits it's width from the parent div, allowing you to specify a related percentage of the cell width.

    Here is a working demo on CodePen that you can play with and examine further.

    http://codepen.io/ld0rman/pen/FCiLh

        <ul>
      <li><a href="#">One</a></li>
      <li><a href="#">Two</a></li>
      <li><a href="#">Three</a></li>
      <li><a href="#">Four</a></li>
      <li><a href="#">Five</a></li>
    </ul>  
    

    CSS:

      ul {
     list-style-type: none; 
     margin: 100px auto;
      width: 80%;
     }
    
     li {
       display: table-cell; 
       width: 1%;
        text-align: center;
        }
    
          a {
         display: block;
            border: 1px solid black;
          padding: 25px;
          text-decoration: none;
          color: white;
       text-transform: uppercase;
    font-weight: bold;
    background: grey;
    
     &:hover {
    text-decoration: none; 
    color: yellow;
    background: darken(grey, 10%);
      }
       }
    

    As you can see, it is coded similarly to your github example. Any more questions, ask away!

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