css表格设置不同宽度,使表格内容适合宽度与表格宽度:100%在css中设置

4 个答案:

答案 0 :(得分:3)

可能colgroup可以帮助你。试试this

HTML是:

Age12GenderMale

答案 1 :(得分:2)

如果我理解正确,这就是你要找的东西:

table { width: 100% }

td:nth-child(odd) { width: 5% }

或者如果您希望内容被单元格“拥抱”:

table { width: 100% }

td:nth-child(odd) { width: 1%; white-space: nowrap }

$(function(){

$(window).load(function() {

updateCellWidth()

})

$(window).resize(function() {

updateCellWidth()

})

})

function updateCellWidth() {

var width = 0, cols = 0

$("table td:nth-child(even)").each(function(){

++cols

width += $(this).width()

})

if (cols > 0) {

var evenCellWidth=($("table").width()-width)/cols

$("table td:nth-child(even)").css("width", evenCellWidth + "px")

}

}

答案 2 :(得分:0)

您是否正在寻找类似this的内容。?

然后border-collapse,cellspacing& cellpadding可能会对您有所帮助。

答案 3 :(得分:0)

这就是我想出的。谢谢你的想法。

table {

border-collapse: collapse;

width: 100%;

}

table td {

border: 1px solid #000;

white-space: nowrap;

}

Age12GenderMaleSome Long LabelSome Long Label Value

var tbl = document.getElementById('tbl1');

var rowCount = tbl.rows.length;

var colCount = tbl.rows[0].cells.length;

for (var i = 0 ; i < colCount; i++) {

if (i%2 == 0) {

tbl.rows[0].cells[i].style.width = tbl.rows[0].cells[i].innerHTML.length + 'px';

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值