Search code examples
htmlcsspaddingbackground-color

Background Color in CSS Going Over in Table


In my HTML project, I set the background color of a table tr to white, but it goes over the edge of the table with 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>

Notice when you run it the white background sticks out.

Image of background going over edge

How can I fix this?


Solution

  • try this

    You given here width:100%;padding:10px; makes

    width = 100% + 20px (both left and right)

    so, one method to using calc() in CSS to minus 20px padding

    .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>

    Or

    no need to assign width:100%; actually in here one having given width:100%

    so just remove the width:100%;

    <!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 {
        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>