gpt4 book ai didi

html - 使表比容器宽

转载 作者:太空狗 更新时间:2023-10-29 12:30:19 24 4
gpt4 key购买 nike

我有一张放在容器内的 table 。

我希望表格的两边都比容器宽 20px,但我无法让它工作。

我尝试将表格元素上的 margin: 0 auto; 更改为 margin: 0 -40px; 以便它在容器的每一侧悬垂 20px,但这只会将整个表格向左移动。

.container {
width: 400px;
display: block;
border: 2px solid red;
margin: 0 auto;
}

table {
border-spacing: 1;
border-collapse: collapse;
background: white;
border-radius: 10px;
overflow: hidden;
width: 100%;
margin: 0 auto;
position: relative;
}

table * {
position: relative;
}

table td,
table th {
padding-left: 8px;
}

table thead tr {
height: 60px;
background: #36304a;
}

table tbody tr {
height: 50px;
}

table tbody tr:last-child {
border: 0;
}

table td,
table th {
text-align: left;
}

table td.l,
table th.l {
text-align: right;
}

table td.c,
table th.c {
text-align: center;
}

table td.r,
table th.r {
text-align: center;
}

.header th {
font-size: 18px;
color: #fff;
line-height: 1.2;
font-weight: unset;
}

tbody tr:nth-child(even) {
background-color: #f5f5f5;
}

tbody tr {
font-size: 15px;
color: #808080;
line-height: 1.2;
font-weight: unset;
}

tbody tr:hover {
color: #555555;
background-color: #f5f5f5;
cursor: pointer;
}

.column {
width: 160px;
}

.column:first-child {
padding-left: 40px;
}

.column:last-child {
padding-right: 40px;
}
<div class="container">
<table>
<thead>
<tr class="header">
<th class="column">1</th>
<th class="column">2</th>
<th class="column">3</th>
</tr>
</thead>
<tbody>
<tr>
<td class="column">mnbvbmvn</td>
<td class="column" data-title="Points">28761528</td>
<td class="column" data-title="Points">Some text here</td>
</tr>
<tr>
<td class="column">adsfasdf</td>
<td class="column" data-title="Points">12341234</td>
<td class="column" data-title="Points">blah blah blah</td>
</tr>
<tr>
<td class="column">ajgsgdsdjha</td>
<td class="column" data-title="Points">85765</td>
<td class="column" data-title="Points">some other text</td>
</tr>
</tbody>
</table>
</div>

最佳答案

您可以设置最小宽度 min-width: 100%; 而不是 width 并根据需要设置负边距,以使表格水平居中。

.container {
width: 400px;
display: block;
border: 2px solid red;
margin: 0 auto;
}

table {
border-spacing: 1;
border-collapse: collapse;
background: white;
border-radius: 10px;
overflow: hidden;
min-width: 100%;
position: relative;
margin: 0 -20px;
}

table * {
position: relative;
}

table td,
table th {
padding-left: 8px;
}

table thead tr {
height: 60px;
background: #36304a;
}

table tbody tr {
height: 50px;
}

table tbody tr:last-child {
border: 0;
}

table td,
table th {
text-align: left;
}

table td.l,
table th.l {
text-align: right;
}

table td.c,
table th.c {
text-align: center;
}

table td.r,
table th.r {
text-align: center;
}

.header th {
font-size: 18px;
color: #fff;
line-height: 1.2;
font-weight: unset;
}

tbody tr:nth-child(even) {
background-color: #f5f5f5;
}

tbody tr {
font-size: 15px;
color: #808080;
line-height: 1.2;
font-weight: unset;
}

tbody tr:hover {
color: #555555;
background-color: #f5f5f5;
cursor: pointer;
}

.column {
width: 160px;
}

.column:first-child {
padding-left: 40px;
}

.column:last-child {
padding-right: 40px;
}
<div class="container">
<table>
<thead>
<tr class="header">
<th class="column">1</th>
<th class="column">2</th>
<th class="column">3</th>
</tr>
</thead>
<tbody>
<tr>
<td class="column">mnbvbmvn</td>
<td class="column" data-title="Points">28761528</td>
<td class="column" data-title="Points">Some text here</td>
</tr>
<tr>
<td class="column">adsfasdf</td>
<td class="column" data-title="Points">12341234</td>
<td class="column" data-title="Points">blah blah blah</td>
</tr>
<tr>
<td class="column">ajgsgdsdjha</td>
<td class="column" data-title="Points">85765</td>
<td class="column" data-title="Points">some other text</td>
</tr>
</tbody>
</table>
</div>

关于html - 使表比容器宽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51610314/

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