CSS表列autowidth

Sha*_*eKm 93 html css xhtml css3

鉴于以下内容,我如何将我的上一列自动调整为其内容?(最后一列应该自动调整宽度到内容.假设我只有1个li元素它应该收缩而不是3个li元素等):

            <table cellspacing="0" cellpadding="0" border="0">
            <thead><!-- universal table heading -->
                <tr>
                    <td class="tc first"><input type="checkbox" value="true" name="data-1-check-all" id="data-1-check-all"></td>
                    <td class="tc">Type</td>
                    <th>File</th>
                    <th>Sample</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Division 1</td>
                    <td>Division 2</td>
                    <td>Division 3</td>
                    <td>Division 4</td>
                    <td>Division 5</td>
                </tr>
                <tr>
                    <td>Division 1</td>
                    <td>Division 2</td>
                    <td>Division 3</td>
                    <td>Division 4</td>
                    <td class="last">
                        <ul class="actions">
                            <li><a class="iconStats" href="#">Statystyki</a></li>
                            <li><a class="iconEdit" href="#">Edytuj</a></li>
                            <li><a class="iconDelete" href="#">Usu?</a></li>

                        </ul>
                    </td>
                </tr>
            </tbody>
        </table>
Run Code Online (Sandbox Code Playgroud)

CSS:

table { table-layout: fixed; width: 100%; }
table tr { border-bottom:1px solid #e9e9e9; }
table thead td, th {border-left: 1px solid #f2f2f2; border-right: 1px solid #d5d5d5; background: #ddd url("../images/sprites4.png") repeat-x scroll 0 100% ; font-weight: bold; text-align:left;}
table tr td, th { border:1px solid #D5D5D5; padding:15px;}
table tr:hover { background:#fcfcfc;}
table tr ul.actions {margin: 0;}
table tr ul.actions li {display: inline; margin-right: 5px;}
Run Code Online (Sandbox Code Playgroud)

Dou*_*mos 205

以下将解决您的问题:

td.last {
    width: 1px;
    white-space: nowrap;
}
Run Code Online (Sandbox Code Playgroud)

  • 您可以将'td:last-child'作为选择器,而不是手动添加'.last'类. (13认同)
  • 完美的工作!(我的情况是表格宽度为100%,其他列没有宽度.将此应用于一列).在IE7/8/9,Firefox 12和Chrome 19中测试过. (3认同)
  • 这个解决方案很好,没有在http://jsfiddle.net/hCkch/1/问题的CSS中设置的`table-layout:fixed` (2认同)

San*_*der 24

如果你想确保最后一行没有换行,从而按照你想要的方式调整大小,请看一下

td {
 white-space: nowrap;
}
Run Code Online (Sandbox Code Playgroud)