问题描述
在我的HTML项目中,我将表格tr
的背景色设置为白色,但是它以padding = 10px;
越过表格的边缘
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>--!</title>
<link href="https://fonts.googleapis.com/css2?family=Kumbh+Sans:wght@300&display=swap" rel="stylesheet">
<style>
body {
margin: 0;
background-color: #e8e8e8;
font-family: 'Kumbh Sans',sans-serif;
}
.topnav {
background-color: #11a642;
overflow: hidden;
}
.topnav a {
float: left;
color: #f2f2f2;
text-align: center;
padding: 18px 20px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #0a6127;
}
.topnav a.active {
background-color: #11a642;
color: white;
}
.topnav a.active:hover {
background-color: #0a6127;
}
#noCss {
display: none;
}
#page {
margin-left: 50px;
margin-right: 50px;
}
tr,td {
border: 1px solid black;
}
table {
width: 100%;
}
.box {
width: 100%;
padding: 10px;
background-color: white;
}
</style>
<body>
<div class = "topnav">
<a href="/home" class="active"><b>--</b></a>
<a href="/stuff">My Folders</a>
<a href="/search">Search</a>
<a href="/home" id="yellow" style="float:right;">Sign up</a>
<a href="/home" style="float:right;">Log In</a>
</div>
<br><br>
<center>
<div id="noCss" style="background-color: #a7ab35;width: 70%; border: 1px solid black;">
<br>
<h2>⚠️ Uh-oh ⚠️</h2>
It looks like the CSS didn't load. Please reload the page.
<br><br><span style="color:#a7ab35;">.</span>
</div>
</center>
<div id="page">
<h2>--</h2>
<table>
<tr>
<td><div class="box">
hi
</div></td>
</tr>
</table>
</div>
</body>
</html>
运行时注意,白色背景会突出显示。
Image of background going over edge
我该如何解决?
解决方法
尝试
您在这里给width:100%;padding:10px;
做的
宽度= 100%+ 20px(左右)
因此,一种在CSS中使用 calc()
来减去20px填充的方法
.box {
padding: 10px;
background-color: white;
width: calc(100% - 20px); // minus padding of both left and right
}
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>--!</title>
<link href="https://fonts.googleapis.com/css2?family=Kumbh+Sans:wght@300&display=swap" rel="stylesheet">
<style>
body {
margin: 0;
background-color: #e8e8e8;
font-family: 'Kumbh Sans',sans-serif;
}
.topnav {
background-color: #11a642;
overflow: hidden;
}
.topnav a {
float: left;
color: #f2f2f2;
text-align: center;
padding: 18px 20px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #0a6127;
}
.topnav a.active {
background-color: #11a642;
color: white;
}
.topnav a.active:hover {
background-color: #0a6127;
}
#noCss {
display: none;
}
#page {
margin-left: 50px;
margin-right: 50px;
}
tr,td {
border: 1px solid black;
}
table {
width: 100%;
}
.box {
width: calc(100% - 20px);
padding: 10px;
background-color: white;
}
</style>
<body>
<div class = "topnav">
<a href="/home" class="active"><b>--</b></a>
<a href="/stuff">My Folders</a>
<a href="/search">Search</a>
<a href="/home" id="yellow" style="float:right;">Sign up</a>
<a href="/home" style="float:right;">Log In</a>
</div>
<br><br>
<center>
<div id="noCss" style="background-color: #a7ab35;width: 70%; border: 1px solid black;">
<br>
<h2>⚠️ Uh-oh ⚠️</h2>
It looks like the CSS didn't load. Please reload the page.
<br><br><span style="color:#a7ab35;">.</span>
</div>
</center>
<div id="page">
<h2>--</h2>
<table>
<tr>
<td><div class="box">
hi
</div></td>
</tr>
</table>
</div>
</body>
</html>
或
这里实际上不需要分配
width:100%;
,只要给出 width:100%
因此只需删除 width:100%;
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width,td {
border: 1px solid black;
}
table {
width: 100%;
}
.box {
padding: 10px;
background-color: white;
}
</style>
<body>
<div class = "topnav">
<a href="/home" class="active"><b>--</b></a>
<a href="/stuff">My Folders</a>
<a href="/search">Search</a>
<a href="/home" id="yellow" style="float:right;">Sign up</a>
<a href="/home" style="float:right;">Log In</a>
</div>
<br><br>
<center>
<div id="noCss" style="background-color: #a7ab35;width: 70%; border: 1px solid black;">
<br>
<h2>⚠️ Uh-oh ⚠️</h2>
It looks like the CSS didn't load. Please reload the page.
<br><br><span style="color:#a7ab35;">.</span>
</div>
</center>
<div id="page">
<h2>--</h2>
<table>
<tr>
<td><div class="box">
hi
</div></td>
</tr>
</table>
</div>
</body>
</html>