微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

html – 如何使表格单元格收缩宽度仅适合其内容?

我想要的表格宽度为100%,但只有一列应该有一个空格,如:
| A | B | C                                                                  |

所以A和B列的宽度应与其内容的宽度相匹配,但C的宽度应该继续,直到表结束。

好的,我可以指定A和B的宽度(以像素为单位),但是问题是这些列的内容的宽度不是固定的,所以如果我设置了一个固定的宽度,它将不完全匹配内容:(

PS:我没有使用实际的表项,而是包含在div中的DL列表。 div有显示:table,dl有显示:table-row和dt / dd显示:table-cell …

解决方法

如果我理解你,你有表格数据,但是你想在浏览器中使用div元素(AFAIK表和带有display:table的表格大致相同)表示。

(这里是一个工作的jsfiddlehttp://jsfiddle.net/roimergarcia/CWKRA/)

标记

<div class='theTable'>
    <div class='theRow'>
        <div class='theCell' >first</div>
        <div class='theCell' >test</div>
        <div class='theCell bigCell'>this is long</div>
    </div>
    <div class='theRow'>
        <div class='theCell'>2nd</div>
        <div class='theCell'>more test</div>
        <div class='theCell'>it is still long</div>
    </div>
</div>​

和CSS:

.theTable{
    display:table; 
    width:95%; /* or whatever width for your table*/
}
.theRow{display:table-row}
.theCell{
    display:table-cell;
    padding: 0px 2px; /* just some padding,if needed*/
    white-space: pre; /* this will avoid line breaks*/
}
.bigCell{
    width:100%; /* this will shrink other cells */
}​

可悲的是,一年前我无法做到这一点,当时真的需要它-_-!

原文地址:https://www.jb51.cc/html/232855.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐