我有几个div元素,其中每个div内部使用以下代码在某些段落(p)之间有一个表:
< p style =“display:inline-block;”> 桌子前面的东西 &安培; …
这是一个简单易用的解决方案:
.before-table { min-width: 250px; display: inline-block; } <p style="display: inline-block;"> <div class="before-table">Something BEFORE the table</div> <table style="display: inline-block;"> <thead> <tr> <th>header</th> </tr> </thead> <tbody> <tr> <td>data</td> </tr> </tbody> </table> <span>Something AFTER the table</span> </p>
要设置绝对位置,您可以使用 position: absolute 在你想要定位的元素上。它将定位在它的第一个非静态父级的坐标(0,0)(左上角)。 (默认情况下,每个元素的位置都设置为static,直到您将其显式设置为其他位置)。这意味着你还需要为父母分配一个职位( p 我是你的情况)所以它会覆盖默认值。就像是 position: relative 在父母应该做的工作。
position: absolute
p
position: relative
之后,您可以分别使用“顶部,右侧,底部,左侧”CSS属性从父级的顶部/右侧/底部/左侧边框设置自定义位置。
p { position: relative; } table { position: absolute; left: 150px; // or whatever distance works best }
像这样的东西,或它的等效内联版本应该这样做。
将其包裹在表格结构中。这可以使用div的样式表来完成。通过这种方式,您可以使其响应。
!不要再将其他块级元素放在p元素中
看到: 为什么&lt; table&gt;内部不允许&lt; p&gt;
以下是您需要的HTML:
.table{ display: table; widht: 100%; } .row { display: table-row; } .cell{ display: table-cell; padding: 0 7.5px; }
<div class='table'> <div class='row'> <div class='cell'> Something BEFORE the table </div> <div class='cell'> <table> <thead> <tr> <th>header</th> </tr> </thead> <tbody> <tr> <td>data</td> </tr> </tbody> </table> </div> <div class='cell'> Something AFTER the table </div> </div> <div class='row'> <div class='cell'> Something LONGGGGGG BEFORE the table </div> <div class='cell'> <table> <thead> <tr> <th>header</th> </tr> </thead> <tbody> <tr> <td>data</td> </tr> </tbody> </table> </div> <div class='cell'> Something AFTER the table </div> </div> </div>