在下面的html代码中,我创建了一个表格。但列宽不正确。我也添加了CSS代码和输出图像。请告诉我我错在哪里,为什么表列宽度不相等。提前致谢。如何在HTML中设置正确的表格宽度?
body {
width: 800px;
margin: auto;
margin-top: 100px;
}
header {
background-color: #EC7063;
border-radius: 10px;
text-align: center;
}
main {
background-color: #76D7C4;
border-radius: 10px;
padding: 20px;
}
h1 {
color: #21618C;
}
#cat {
border-radius: 10px;
background-color: #27AE60;
padding: 10px;
margin-right: 20px;
float: left;
height: 200px;
}
h2 {
color: #21618C;
}
#prod {
border-radius: 10px;
background-color: #AF7AC5;
height: 200px;
padding: 10px;
overflow: hidden;
}
table {
display: block;
overflow: auto;
height: 100px;
table-layout: fixed;
width: 100%;
}
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}
td {
text-align: center;
}
footer {
background-color: #EC7063;
border-radius: 10px;
text-align: center;
font-size: 25px;
color: #F8F9F9;
clear: both;
}
<header>
<h1>My Guitar Shop</h1>
</header>
<header>
<h1>Product Manager</h1>
</header>
<main>
<h1>Product List</h1>
<section id="cat">
<!-- display a list of categories -->
<h2>Categories</h2>
<nav>
<ul>
<li><a href="?category_id=1">Guitars</a>
</li>
<li><a href="?category_id=2">Basses</a>
</li>
<li><a href="?category_id=3">Drums</a>
</li>
<li><a href="?category_id=4">Flutes</a>
</li>
<li><a href="?category_id=5">Trumpets</a>
</li>
</ul>
</nav>
</section>
<section id="prod">
<!-- display a table of products -->
<h2>Guitars</h2>
<table>
<tr>
<th>Code</th>
<th>Name</th>
<th>Price</th>
<th>Delete</th>
</tr>
<tr>
<td>gu</td>
<td>ytr</td>
<td>0000000546</td>
<td>
<form action="#" method="post">
<input type="hidden" name="action" value="delete_product" />
<input type="hidden" name="id" value="19" />
<input type="hidden" name="category_id" value="1" />
<input type="submit" value="Delete" />
</form>
</td>
</tr>
<tr>
<td>yui</td>
<td>uyt</td>
<td>0000000888</td>
<td>
<form action="#" method="post">
<input type="hidden" name="action" value="delete_product" />
<input type="hidden" name="id" value="23" />
<input type="hidden" name="category_id" value="1" />
<input type="submit" value="Delete" />
</form>
</td>
</tr>
<tr>
<td>oi</td>
<td>iu</td>
<td>0000000098</td>
<td>
<form action="#" method="post">
<input type="hidden" name="action" value="delete_product" />
<input type="hidden" name="id" value="24" />
<input type="hidden" name="category_id" value="1" />
<input type="submit" value="Delete" />
</form>
</td>
</tr>
</table>
<p><a href="?action=show_add_form">Add Product</a></p>
</section>
</main>
<footer>
<p>© 2017 My Guitar Shop, Inc.</p>
</footer>
尝试从您的表中删除'display:block' – Pete
试过。尽管它将列宽设置为正确,但它无济于事。它删除滚动。 – KawaiKx
如果您需要更多帮助,请提供实际可用的代码 - 即生成的HTML代码,而不是PHP代码,即使他们尝试过,也无法在系统中正确执行代码。 [mcve] – CBroe