【发布时间】:2021-02-17 23:01:36
【问题描述】:
我正在尝试创建一个个人资料框,用户可以在其中看到自己的个人资料图片以及其他帐户特定信息和实用程序,例如他们的用户名、设置按钮、个人资料页面按钮等。我的方式这样做是使用 flex 居中的表格元素。然后,我给我的 div 的背景上色,看看它们在做什么。我注意到表格单元格之间的白线,尝试了一些东西,做了一些研究,找到了border-collapse 属性,并使用了它。问题是,我的台词只有 一些 消失了,如下图所示。更奇怪的是,当我使用 ctrl + 滚动放大和缩小时,它似乎消失了。我猜这是某种舍入误差。
怎么办?
.Leftside2 {
flex: 20%;
background-color: red;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-around;
}
.profile {
width: 90%;
border: 2px solid black;
display: flex;
justify-content: center;
border-collapse: collapse;
}
#profile_picture {
height: 100%;
padding: 5px;
background-color: orange;
display: flex;
justify-content: center;
}
#profile_picture img {
width: 80%;
height: 80%;
}
.friend_list {
width: 90%;
}
<div class="Leftside2">
<table class="profile">
<tr>
<td style="height: 30vh;border-width: 0px">
<div id="profile_picture"><img src="https://via.placeholder.com/450x400"></div>
</td>
</tr>
<tr>
<td style="border: 0 solid black; background-color: orange">Jill</td>
</tr>
<tr>
<td style="border-width: 0px">Eve</td>
</tr>
<tr>
<td style="border-width: 0px">John</td>
</tr>
</table>
<table class="friend_list">
<tr>
<td>Friends List</td>
</tr>
<tr>
<td>content</td>
</tr>
</table>
</div>
编辑:我尝试将 cellpadding="0" 和 cellspacing="0" 放入我的内部,但没有成功。我还尝试明确声明所有表格元素中的边距 = 0,填充 = 0。正如许多人在下面所建议的那样,我认为这不是边距/填充问题。
修改后的代码:
.profile {
width: 90%;
border: 2px solid black;
display: flex;
justify-content: center;
border-collapse: collapse;
padding: 0;
margin: 0;
}
.profile td {
padding: 0;
margin: 0;
}
第二次编辑:
<html lang = "en">
<head>
<link rel="stylesheet" href="../css/style.css">
<title>Find a Friend</title>
</head>
<body>
<div class="HeaderMenu">
<div style="margin-left:40px;margin-right:100px;background-color: #008aed;">
<a href="logout.php" target="_self" class="logout_button_link"><button class="logout_button">Logout</button></a>
</div>
</div>
<div class="row">
<div class = "left_space"></div>
<div class="Leftside2">
<table class="profile" cellpadding="0" cellspacing="0">
<tr>
<td style="height: 30vh;border-width: 0px">
<div id="profile_picture"><img src="../img/placeholder.png"></div>
</td>
</tr>
<tr>
<td style="border: 0 solid black; background-color: orange">Jill</td>
</tr>
<tr>
<td style="border-width: 0px">Eve</td>
</tr>
<tr>
<td style="border-width: 0px">John</td>
</tr>
</table>
<table class="friend_list">
<tr>
<td>Friends List</td>
</tr>
<tr>
<td>content</td>
</tr>
</table>
</div>
<div class="Centerside2">
</div>
<div class="Rightside2">
</div>
<div class = "right_space"></div>
</div>
</body>
【问题讨论】:
-
请注意,这被视为滥用表格,使用屏幕阅读器的用户无法访问。
-
我应该改用什么?将 div 相互堆叠会更好吗?
-
Divs 将是更好的选择,并且在语义上更正确(您应该只将表格用于表格数据或电子邮件模板中)。此外,我真的不认为需要表格,因为无论如何每行只有一列,所以在这种情况下表格甚至对布局没有帮助
-
我会使用
figure元素作为图片,也许使用figcaption元素作为名称。然后你可以使用ul和lis 作为好友列表。基本上,从语义上考虑元素是什么,并使用最接近的匹配元素。 -
这能回答你的问题吗? Extra white space on table cells
标签: html css html-table