r/HTML • • 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;
  }
3 Upvotes

3 comments sorted by

4

u/jcunews 14h ago

<td> by default, have paddings. So add CSS property padding: 0 to td.

1

u/on-works 12h ago

There's no border on your image. The border is on the cell.

i.e. In your css...

/* This creates a border around all cells */

td { 
    border: 0.16rem solid #000000;
    white-space: pre-line;
  } 


/* This specifies no border around images inside a cell. But it does not remove the border around the cell itself */

td img {
    border: none;
    width: 100%;
    display: block;
    object-fit: fill;
  }

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.

td.image,
td.image img {
    border: none;
  }

A couple of additional pointers...

  1. The html "img" property is self closing.
    So you only need <img src="path"> not <img src="path"></img>
  2. 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