表中要遍历的CSS中的背景色

问题描述

在我的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>

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...