gpt4 book ai didi

html - 为什么这些表格没有正确对齐?

转载 作者:行者123 更新时间:2023-11-28 02:06:08 29 4
gpt4 key购买 nike

在我开始学习 HTML 和 CSS 时,我必须创建一些表格作为作业。这些是我想要创造的,

Table 1

Table 2

代码:表 1 -该表似乎由于某种原因使第四行保留在第三行。

table {
text-align: center;
}

tr,
td,
th {
border: 1px solid #000;
}
<!DOCTYPE html>
<html lang="en">

<head>
<title></title>
<meta charset="utf-8">
</head>

<body>
<table>
<tr>
<th colspan="17">My Work Plan for the Week</th>
</tr>
<tr>
<td rowspan="5">July</td>
<th colspan="1"></th>
<th colspan="3">Monday</th>
<th colspan="3">Tuesday</th>
<th colspan="3">Wednesday</th>
<th colspan="3">Thursday</th>
<th colspan="3">Friday</th>
</tr>
<tr>
<td rowspan="2">Week 1</td>
<td rowspan="2" colspan="3">Meeting at ABC-123 Ltd</td>
<td rowspan="2" colspan="3">Business Lunch at 1pm</td>
<td rowspan="2" colspan="3">Project due by 5pm</td>
<td rowspan="2" colspan="3">Web Conference in London</td>
<td rowspan="2" colspan="3">Early Finish (4pm)</td>
</tr>
<tr>
<td rowspan="2">Week 2</td>
<td rowspan="2" colspan="3">Business Planning Lunch - 1:30pm</td>
<td rowspan="2" colspan="9">Networking and Training Conference</td>
<td rowspan="2">Early Finish (4pm)</td>
</tr>
</table>
</body>

</html>

表 2 -我无法将框正确设置为图像中所需的大小。

tr td {
border: 1px solid #000;
}

table {
border-collapse: collapse;
}

.red {
background: red;
}

.blue {
background: blue;
}

.yellow {
background: yellow;
}

.green {
background: green;
}

.orange {
background: orange;
}

.big {
width: 20px;
}
<!DOCTYPE html>
<html lang="en">

<head>
<title></title>
<meta charset="utf-8">
</head>

<body>
<table>
<tr>
<td rowspan="2" colspan="2" class="red" class="big">0</td>
<td class="blue">1</td>
<td class="yellow">2</td>
<td rowspan="2" colspan="2" class="green" class="big">3</td>
</tr>
<tr>
<td rowspan="2" colspan="2" class="orange" class="big">4</td>
</tr>
<tr>
<td colspan="2"></td>
<td colspan="2"></td>
</tr>
<tr>
<td rowspan="2" colspan="2" class="green" class="big">5</td>
<td colspan="2"></td>
<td rowspan="2" colspan="2" class="yellow" class="big">6</td>
</tr>
<tr>
<td class="blue">7</td>
<td class="red">8</td>
</tr>
</table>
</body>

</html>

我做错了什么?

最佳答案

您需要从最后两行 td 中删除 rowspan...它会导致您的 tr 对齐问题

堆栈片段

table {
text-align: center;
}

tr,
td,
th {
border: 1px solid #000;
}
<table>
<tr>
<th colspan="17">My Work Plan for the Week</th>
</tr>
<tr>
<td rowspan="5">July</td>
<th colspan="1"></th>
<th colspan="3">Monday</th>
<th colspan="3">Tuesday</th>
<th colspan="3">Wednesday</th>
<th colspan="3">Thursday</th>
<th colspan="3">Friday</th>
</tr>
<tr>
<td>Week 1</td>
<td colspan="3">Meeting at ABC-123 Ltd</td>
<td colspan="3">Business Lunch at 1pm</td>
<td colspan="3">Project due by 5pm</td>
<td colspan="3">Web Conference in London</td>
<td colspan="3">Early Finish (4pm)</td>
</tr>
<tr>
<td>Week 2</td>
<td colspan="3">Business Planning Lunch - 1:30pm</td>
<td colspan="9">Networking and Training Conference</td>
<td>Early Finish (4pm)</td>
</tr>
</table>


在第二个表中,您两次使用 class 属性导致了问题...将两个类合并为一个 class 属性

堆栈片段

tr td {
border: 1px solid #000;
text-align: center;
}

table {
border-collapse: collapse;
}

.red {
background: red;
}

.blue {
background: blue;
}

.yellow {
background: yellow;
}

.green {
background: green;
}

.orange {
background: orange;
}

td {
width: 20px;
height: 20px;
}

td.big {
width: 40px;
height: 40px;
}
<!DOCTYPE html>
<html lang="en">

<head>
<title></title>
<meta charset="utf-8">
</head>

<body>
<table>
<tr>
<td rowspan="2" colspan="2" class="red big">0</td>
<td class="blue">1</td>
<td class="yellow">2</td>
<td rowspan="2" colspan="2" class="green big">3</td>
</tr>
<tr>
<td rowspan="2" colspan="2" class="orange big">4</td>
</tr>
<tr>
<td colspan="2"></td>
<td colspan="2"></td>
</tr>
<tr>
<td rowspan="2" colspan="2" class="green big">5</td>
<td colspan="2"></td>
<td rowspan="2" colspan="2" class="yellow big">6</td>
</tr>
<tr>
<td class="blue">7</td>
<td class="red">8</td>
</tr>
</table>
</body>

</html>

关于html - 为什么这些表格没有正确对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49001724/

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