r/HTML • u/calendareclipse • 20h ago
How to Make Images Actually Fill Table Cell

Hi, I'm trying to get a image to actually fill a table cell, and while I've mostly accomplished that, there's still a tiny border around the image of the background that I cannot figure out how to remove.
HTML
<div class="table-container">
<table>
<caption>Products</caption>
<tr>
<th scope="col">Name</th>
<th scope="col">Image</th>
<th scope="col">Price</th>
</tr>
<tr>
<th scope="row">1</th>
<td class="image"><img src="images/lovemug.jpg" alt=""></img></td>
<td>1.</td>
</tr>
</table>
</div><div class="table-container">
<table>
<caption>Products</caption>
<tr>
<th scope="col">Name</th>
<th scope="col">Image</th>
<th scope="col">Price</th>
</tr>
<tr>
<th scope="row">1</th>
<td class="image"><img src="images/lovemug.jpg" alt=""></img></td>
<td>1.</td>
</tr>
</table>
</div>
CSS
.table-container {
overflow-x: auto;
margin: 0.2rem 1rem 1.5rem;
}
table {
border: 0.2rem solid;
border-spacing: 0px;
}
table, tr {
border-collapse: collapse;
background-color: #fff597;
}
caption {
border: 0.2rem solid;
border-bottom: none;
font-size: 20px;
font-weight: bold;
background-color: #ffe552
}
tr:hover, tr:focus {
background-color: #fcef7f
}
th {
border: 0.16rem solid #000000;
}
td {
border: 0.16rem solid #000000;
white-space: pre-line;
}
td img {
border: none;
width: 100%;
display: block;
object-fit: fill;
}
td p, th p {
text-align: left;
} .table-container {
overflow-x: auto;
margin: 0.2rem 1rem 1.5rem;
}
table {
border: 0.2rem solid;
border-spacing: 0px;
}
table, tr {
border-collapse: collapse;
background-color: #fff597;
}
caption {
border: 0.2rem solid;
border-bottom: none;
font-size: 20px;
font-weight: bold;
background-color: #ffe552
}
tr:hover, tr:focus {
background-color: #fcef7f
}
th {
border: 0.16rem solid #000000;
}
td {
border: 0.16rem solid #000000;
white-space: pre-line;
}
td img {
border: none;
width: 100%;
display: block;
object-fit: fill;
}
td p, th p {
text-align: left;
}
4
u/jcunews 14h ago
<td>by default, have paddings. So add CSS propertypadding: 0totd.1
u/on-works 12h ago
There's no border on your image. The border is on the cell.
i.e. In your css...
I note in your html that you have given the cell a class name of "image". You can target that selector for both the cell (td.image) and for the image (td.image img).
i.e.
A couple of additional pointers...
So you only need <img src="path"> not <img src="path"></img>
Depending on what you're wanting to achieve you can use "outline" as well as "border".
Paste this at the end of your css to see how each behaves.
td.image img { border:3px solid orange; outline:3px solid green; }
Hope that helps