0

I have an image and some text beside it in a table. The HTML looks like this:

  <table>
    <tr>
      <td>
        <img src="b.png" alt="B" class="the-b">
      </td>
      <td>
        <h1>WHY</h1>
        <h2></h2>
        <p>BUT WHY THOUGH</p>
      </td>
    </tr>
  </table>

Without any styling on the image except a yellow background, the text in the table rests right alongside the image, like this: no resizing

However, if I add this CSS to the image, there is a big white space between the image and text:

.the-b {
  width: 10%;
  height: auto;
}

See resized.

Even if I change the padding and margin to 0, this stays the same. It looks as though the image is taking up the same amount of space no matter what size it is, and even though the background color is confined to the visible image.

If I use an image with smaller dimensions, the text rests against it until I resize it, so I know it's not some table default size. But, if I just put text beside the image, the resizing whitespace issue disappears, so it must be a combo of the table with CSS width: x%.

What is creating this whitespace? Is it normal for CSS image resizing to hold the original image size space this way (if that's what's happening)?

Kitty
  • 89
  • 10
  • 1
    you told the image to take only 10% of the width so you will have 90% of white space – Temani Afif Jun 28 '21 at 23:37
  • Ohh, so the image size is the width the percentage is based on, so it keeps that original metric and just resizes the image in relation to the space? That makes sense. But why does it only happen with a table? Also, if I want to use percentage resizing, is there a way to remove the whitespace, or do I have to use px or something instead? – Kitty Jun 28 '21 at 23:39
  • yes you have to use pixel and this doesn't happen only with tables (read the duplicate) – Temani Afif Jun 28 '21 at 23:44
  • Thank you! (I couldn't find the question in a search.) – Kitty Jun 28 '21 at 23:46

0 Answers0