gpt4 book ai didi

html - table 上边距为负的奇怪东西

转载 作者:太空狗 更新时间:2023-10-29 16:05:36 24 4
gpt4 key购买 nike

问题不是很短,但很容易理解。

这是 jsFiddle

我有两个嵌套表,像这样:

enter image description here

这是标记:

<table class="outer">
<tr>
<td></td>
<td>
<table class="inner">
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</td>
<td></td>
</tr>
</table>

<style>
table, tr, td {
border: 1px solid black;
border-collapse: collapse;
}

table {
width: 100%;
}

.outer {
height: 100px;
}

.inner {
background-color: #ccc;
height: 50px;
}
</style>

第一个奇怪的事情

然后,我想在内表中添加一个负的水平外边距:

.inner {
margin: 0 -10%;
}

预期的输出是这样的:

enter image description here

但相反,我得到了这个:

enter image description here

将内表放在div中可能会解决问题:

<!-- outer table -->
<div class="wrapper">
<table class="inner-wrapped">
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</div>
<!-- outer table -->

<style>
.wrapper {
margin: 0 -10%;
}

.inner-wrapped {
background-color: rgb(0,200,0);
height: 50px;
}
</style>

第二个奇怪的事情

如果我设置负水平边距 -10px(之前我们使用的是百分比,而不是像素),那么另外表格只会向左移动(就像第一种情况),它的宽度会显着减小:

.inner {
margin: 0 -10px;
}

enter image description here

问题

  • 为什么会发生这两种奇怪的事情?

  • 有什么办法可以解决?像我一样简单地使用包装器是一种好的做法,还是我应该使用另一种更聪明的方法?

最佳答案

如果主表是width:100%;会一直展开,内表会取初始的100%作为引用。只要没有内容,负边距就不会扩展它。它会工作如果:https://jsfiddle.net/md2tx2d4/2/

.inner { margin:0 -10%; width:120%;}   

或者如果你让它在没有宽度的情况下存在并让它从它的内容中增长

table { }
td {width:200px;/* instead real content missing here */}

https://jsfiddle.net/md2tx2d4/1/

关于html - table 上边距为负的奇怪东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35712461/

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