CSS display:table-row does not expand when width is set to 100%

后端 未结 5 714
臣服心动
臣服心动 2020-12-08 01:25

I\'m having a bit of a problem. I\'m using FireFox 3.6 and have the following DOM structure:

相关标签:
5条回答
  • 2020-12-08 01:57

    give on .view-type class float:left; or delete the float:right; of .view-name

    edit: Wrap your div <div class="view-row"> with another div for example <div class="table">

    and set the following css :

    .table {
        display:table;
        width:100%;}
    

    You have to use the table structure for correct results.

    0 讨论(0)
  • 2020-12-08 01:59

    Tested answer:

    In the .view-row css, change:

    display:table-row;
    

    to:

    display:table
    

    and get rid of "float". Everything will work as expected.

    As it has been suggested in the comments, there is no need for a wrapping table. CSS allows for omitting levels of the tree structure (in this case rows) that are implicit. The reason your code doesn't work is that "width" can only be interpreted at the table level, not at the table-row level. When you have a "table" and then "table-cell"s directly underneath, they're implicitly interpreted as sitting in a row.

    Working example:

    <div class="view">
        <div>Type</div>
        <div>Name</div>                
    </div>
    

    with css:

    .view {
      width:100%;
      display:table;
    }
    
    .view > div {
      width:50%;
      display: table-cell;
    }
    
    0 讨论(0)
  • 2020-12-08 02:01

    If you're using display:table-row etc., then you need proper markup, which includes a containing table. Without it your original question basically provides the equivalent bad markup of:

    <tr style="width:100%">
        <td>Type</td>
        <td style="float:right">Name</td>
    </tr>
    

    Where's the table in the above? You can't just have a row out of nowhere (tr must be contained in either table, thead, tbody, etc.)

    Instead, add an outer element with display:table, put the 100% width on the containing element. The two inside cells will automatically go 50/50 and align the text right on the second cell. Forget floats with table elements. It'll cause so many headaches.

    markup:

    <div class="view-table">
        <div class="view-row">
            <div class="view-type">Type</div>
            <div class="view-name">Name</div>                
        </div>
    </div>
    

    CSS:

    .view-table
    {
        display:table;
        width:100%;
    }
    .view-row,
    {
        display:table-row;
    }
    .view-row > div
    {
        display: table-cell;
    }
    .view-name 
    {
        text-align:right;
    }
    
    0 讨论(0)
  • 2020-12-08 02:17

    Note that according to the CSS3 spec, you do NOT have to wrap your layout in a table-style element. The browser will infer the existence of containing elements if they do not exist.

    0 讨论(0)
  • 2020-12-08 02:20

    You can nest table-cell directly within table. You muslt have a table. Starting eith table-row does not work. Try it with this HTML:

    <html>
      <head>
        <style type="text/css">
    .table {
      display: table;
      width: 100%;
    }
    .tr {
      display: table-row;
      width: 100%;
    }
    .td {
      display: table-cell;
    }
        </style>
      </head>
      <body>
    
        <div class="table">
          <div class="tr">
            <div class="td">
              X
            </div>
            <div class="td">
              X
            </div>
            <div class="td">
              X
            </div>
          </div>
        </div>
    
          <div class="tr">
            <div class="td">
              X
            </div>
            <div class="td">
              X
            </div>
            <div class="td">
              X
            </div>
          </div>
    
        <div class="table">
            <div class="td">
              X
            </div>
            <div class="td">
              X
            </div>
            <div class="td">
              X
            </div>
        </div>
    
      </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题