gpt4 book ai didi

html - 如何为 HTML 表格中的每一列设置不同的宽度?

转载 作者:太空宇宙 更新时间:2023-11-04 00:36:06 27 4
gpt4 key购买 nike

我有一个水平滚动的 HTML 表格。我想为每一列设置不同的宽度。我尝试了宽度的内联样式。我试过 <th>width属性。但是没有什么对我有用。我不知道我哪里做错了。请注意,我不想更改特定于滚动的此布局的 HTML 结构和 CSS。

这是我的代码:

#tb_wrapper {
border: 1px solid #cccccc;
max-width: 960px;
margin: 50px auto;
padding: 10px;
}

.tb_responsive {
overflow-x: auto;
overflow-y: hidden;
}

.tb_records {
border-collapse: collapse;
text-align: center;
width: 100%;
}

.tb_records th {
background-color: #5F5F5F;
color: #FFFFFF;
font-weight: bold;
}

.tb_records th,
.tb_records td {
padding: 10px;
border: 1px solid #000000;
font-size: 15px;
white-space: nowrap;
}

.tb_records tr:nth-child(even) {
background-color: #F2F2F2;
}

.tb_records tr:hover {
background-color: #F5F5F5;
}
<div id="tb_wrapper">
<div class="tb_responsive">
<table class="tb_records">
<tr>
<th>Post ID</th>
<th>Post Name</th>
<th>Post URL</th>
<th style="width:200px;">Post Title</th>
<th width="300">Post Heading</th>
<th>Post Content</th>
<th>Post Date Published</th>
<th>Post Date Modified</th>
<th>Post Image</th>
<th>Post Views</th>
</tr>
<tr>
<td>1</td>
<td>My Post</td>
<td>my-post</td>
<td>My Post</td>
<td>My Post</td>
<td>Lorem ipsum dolor sit amet.</td>
<td>2017-07-21 13:58:16</td>
<td>2019-07-30 22:38:36</td>
<td>my-post.jpg</td>
<td>7</td>
</tr>
<tr>
<td>2</td>
<td>My Post</td>
<td>my-post</td>
<td>My Post</td>
<td>My Post</td>
<td>Lorem ipsum dolor sit amet.</td>
<td>2017-07-21 13:58:16</td>
<td>2019-07-30 22:38:36</td>
<td>my-post.jpg</td>
<td>7</td>
</tr>
<tr>
<td>3</td>
<td>My Post</td>
<td>my-post</td>
<td>My Post</td>
<td>My Post</td>
<td>Lorem ipsum dolor sit amet.</td>
<td>2017-07-21 13:58:16</td>
<td>2019-07-30 22:38:36</td>
<td>my-post.jpg</td>
<td>7</td>
</tr>
<tr>
<td>4</td>
<td>My Post</td>
<td>my-post</td>
<td>My Post</td>
<td>My Post</td>
<td>Lorem ipsum dolor sit amet.</td>
<td>2017-07-21 13:58:16</td>
<td>2019-07-30 22:38:36</td>
<td>my-post.jpg</td>
<td>7</td>
</tr>
<tr>
<td>5</td>
<td>My Post</td>
<td>my-post</td>
<td>My Post</td>
<td>My Post</td>
<td>Lorem ipsum dolor sit amet.</td>
<td>2017-07-21 13:58:16</td>
<td>2019-07-30 22:38:36</td>
<td>my-post.jpg</td>
<td>7</td>
</tr>
<tr>
<td>6</td>
<td>My Post</td>
<td>my-post</td>
<td>My Post</td>
<td>My Post</td>
<td>Lorem ipsum dolor sit amet.</td>
<td>2017-07-21 13:58:16</td>
<td>2019-07-30 22:38:36</td>
<td>my-post.jpg</td>
<td>7</td>
</tr>
<tr>
<td>7</td>
<td>My Post</td>
<td>my-post</td>
<td>My Post</td>
<td>My Post</td>
<td>Lorem ipsum dolor sit amet.</td>
<td>2017-07-21 13:58:16</td>
<td>2019-07-30 22:38:36</td>
<td>my-post.jpg</td>
<td>7</td>
</tr>
<tr>
<td>8</td>
<td>My Post</td>
<td>my-post</td>
<td>My Post</td>
<td>My Post</td>
<td>Lorem ipsum dolor sit amet.</td>
<td>2017-07-21 13:58:16</td>
<td>2019-07-30 22:38:36</td>
<td>my-post.jpg</td>
<td>7</td>
</tr>
<tr>
<td>9</td>
<td>My Post</td>
<td>my-post</td>
<td>My Post</td>
<td>My Post</td>
<td>Lorem ipsum dolor sit amet.</td>
<td>2017-07-21 13:58:16</td>
<td>2019-07-30 22:38:36</td>
<td>my-post.jpg</td>
<td>7</td>
</tr>
<tr>
<td>10</td>
<td>My Post</td>
<td>my-post</td>
<td>My Post</td>
<td>My Post</td>
<td>Lorem ipsum dolor sit amet.</td>
<td>2017-07-21 13:58:16</td>
<td>2019-07-30 22:38:36</td>
<td>my-post.jpg</td>
<td>7</td>
</tr>
</table>
</div>
</div>

最佳答案

您只需向表中添加几个属性。 “宽度”和“样式”的作用相同。

table{
table-layout: fixed;
width: 100%;
}
<div id="tb_wrapper">
<div class="tb_responsive">
<table class="tb_records">
<tr>
<th>Post ID</th>
<th>Post Name</th>
<th>Post URL</th>
<th style="width:200px;">Post Title</th>
<th width="300">Post Heading</th>
<th>Post Content</th>
<th>Post Date Published</th>
<th>Post Date Modified</th>
<th>Post Image</th>
<th>Post Views</th>
</tr>
<tr>
<td>1</td>
<td>My Post</td>
<td>my-post</td>
<td style="width:200px;">My Post</td>
<td width="300">My Post</td>
<td>Lorem ipsum dolor sit amet.</td>
<td>2017-07-21 13:58:16</td>
<td>2019-07-30 22:38:36</td>
<td>my-post.jpg</td>
<td>7</td>
</tr>

</table>
</div>
</div>

关于html - 如何为 HTML 表格中的每一列设置不同的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59378127/

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