gpt4 book ai didi

html - 使用 CSS 呈现表格数据,其中太宽的单元格不会破坏内容

转载 作者:可可西里 更新时间:2023-11-01 14:58:09 33 4
gpt4 key购买 nike

我有几行数据要以固定大小的列显示。例如:

Name     City       Number
Adam Anaheim 714 555 5555
Bob Barstow 760 555 5555
... ... ...

但是,有时列的数据之一太大而无法容纳。如果发生这种情况,我希望它影响尽可能少的相邻单元格,而不是整行,如果不需要的话。

这是我想要的:

Name     City       Number
Adam Anaheim 714 555 5555
Bob Barstow 760 555 5555
Constantine Canyon 805 555 5555 # Notice how phone number is still aligned :)

这是我想要的:

Name     City       Number
Adam Anaheim 714 555 5555
Bob Barstow 760 555 5555
Constantine Canyon 805 555 5555 # Notice how phone number is also shifted :(

我如何使用 HTML/CSS 执行此操作(请不要使用 javascript)?

这是一个 jsbin使用无效的解决方案,您可以在其中尝试并亲眼看看。

更新当您有 N 列时,这是已接受答案的摘要:

前 N-1 列必须包裹在 DIV style="display: inline-block"中,其最小宽度是前 N-1 列的组合宽度。然后在该 DIV 内部,必须将前 N-2 列包裹在一个类似的 div 中,该 div 的最小宽度是前 N-2 列的组合宽度。继续这样下去……

它不是很漂亮,但它可以工作,并且对于小 N 来说是易于管理的。

最佳答案

好问题。我找不到用普通表来完成此操作的方法(部分原因是需要包装器元素,部分原因是 IE 无法正常运行),因此下面的解决方案使用了 div。

试试这个:

<!DOCTYPE html>
<title>Test Case</title>
<style type="text/css">
body { font-size:11px; font-family: "Courier New"; }
* { margin:0; padding:0; }

.col, .span { display:inline-block; }
.col1 { min-width:12ex; }
.span2 { min-width:30ex; }
.span3 { min-width:40ex; }

/* IE6 */
* html .col { display:inline; padding-right:1ex; }
* html .span { display:inline; white-space:nowrap; }
* html .col1 { width:12ex; }
* html .span2 { width:30ex; }
* html .span3 { width:40ex; }

/* IE7 */
*:first-child+html .col { display:inline; padding-right:1ex; }
*:first-child+html .span { display:inline; }
</style>
<div class="table">
<div class="row">
<div class="span2 span">
<div class="col1 col">Name</div>
<div class="col">City</div>
</div>
<div class="col">Number</div>
</div>
<div class="row">
<div class="span2 span">
<div class="col1 col">Adam</div>
<div class="col">Anaheim</div>
</div>
<div class="col">714 555 5555</div>
</div>
<div class="row">
<div class="span2 span">
<div class="col1 col">Bob</div>
<div class="col">Barstow</div>
</div>
<div class="col">760 555 5555</div>
</div>
<div class="row">
<div class="span3 span">
<div class="span2 span">
<div class="col1 col">Constantine</div>
<div class="col">Canyon</div>
</div>
<div class="col">805 555 5555</div>
</div>
<div class="col"># Notice how phone number is still aligned :)</div>
</div>
</div>

应该清楚如何通过使用包装器“span4”、“span5”等将这个想法扩展到更多列

关于html - 使用 CSS 呈现表格数据,其中太宽的单元格不会破坏内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4177242/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com