在下面的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