Ensure the HTML table's <td> cell width automatically adjusts to accommodate the image, regardless of its dimensions

Has anyone encountered a strange issue while building an HTML table for an HTML email? Whenever I insert an image into a <td>, it causes the cell to expand to its maximum width, affecting all the columns to the right by resizing them to their minimum width. Surprisingly, the Google Chrome Inspector tool reveals that the image width is much wider than its actual size. I have tried resizing the image to 100x136px, but the <td> still expands to 1078px. Even after testing with different images, the same problem persists. What could possibly be causing this? Below is the code snippet I am working on:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <style>
            table {
                width: 100%;
                margin: 20px auto;
                border-collapse: collapse;
            }
            td {
                border: 1px solid black;
            }

            .header {
                height: 150px;
            }
            .bar {
                height: 40px;
            }
            .greeting {
                height: 300px;
            }
            .itemRow {
                height: 250px;
            }
            .footer {
                height: 75px;
            }
            .measure {
                height: 25px;
            }

            img {
            }
        </style>
    </head>
    <body>
        <table>
            <tr class="header">
                <td colspan="2" class="crest"><img src="crest.jpg" alt="" /></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr class="bar">
                <td colspan="14"></td>
            </tr>
            <tr class="greeting">
                <td colspan="14"></td>
            </tr>
            <tr class="itemRow">
                <td colspan="2"></td>
                <td colspan="2"></td>
                <td colspan="2"></td>
                <td colspan="2"></td>
                <td colspan="2"></td>
                <td colspan="2"></td>
                <td colspan="2"></td>
            </tr>
            <tr class="measure">
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr class="itemRow">
                <td colspan="2"></td>
                <td colspan="2"></td>
                <td colspan="2"></td>
                <td colspan="2"></td>
                <td colspan="2"></td>
                <td colspan="2"></td>
                <td colspan="2"></td>
            </tr>
            <tr class="measure">
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr class="itemRow">
                <td colspan="2"></td>
                <td colspan="2"></td>
                <td colspan="2"></td>
                <td colspan="2"></td>
                <td colspan="2"></td>
                <td colspan="2"></td>
                <td colspan="2"></td>
            </tr>

            <tr class="footer">
                <td colspan="12"></td>
                <td colspan="1"></td>
                <td colspan="1"></td>
            </tr>
        </table>
    </body>
</html>

Answer №1

Automatic table layout causes cells to expand or contract based on content size. To prevent this, add table-layout: fixed; to the table tag and width: 100%; height: auto; to the image within the cell:

table {
  width: 100%;
  margin: 20px auto;
  border-collapse: collapse;
  table-layout: fixed;
}

td {
  border: 1px solid black;
}

.header {
  height: 150px;
}

.bar {
  height: 40px;
}

.greeting {
  height: 300px;
}

.itemRow {
  height: 250px;
}

.footer {
  height: 75px;
}

.measure {
  height: 25px;
}

img {
  width: 100%;
  height: auto;
}
<table>
  <tr class="header">
    <td colspan="2" class="crest"><img src="https://placehold.it/500x300/ea8" alt="" /></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr class="bar">
    <td colspan="14"></td>
  </tr>
  <tr class="greeting">
    <td colspan="14"></td>
  </tr>
  <tr class="itemRow">
    <td colspan="2"></td>
    <td colspan="2"></td>
    <td colspan="2"></td>
    <td colspan="2"></td>
    <td colspan="2"></td>
    <td colspan="2"></td>
    <td colspan="2"></td>
  </tr>
  <tr class="measure">
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr class="itemRow">
    <td colspan="2"></td>
    <td colspan="2"></td>
    <td colspan="2"></td>
    <td colspan="2"></td>
    <td colspan="2"></td>
    <td colspan="2"></td>
    <td colspan="2"></td>
  </tr>

  <tr class="footer">
    <td colspan="12"></td>
    <td colspan="1"></td>
    <td colspan="1"></td>
  </tr>
</table>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

reduce opacity of specified div background

Objective / Purpose I am working with multiple div elements and I want to assign two distinct classes to each of them, each serving a different purpose within my application. One class should determine the level, influencing the color of the div. The oth ...

What steps should I take to ensure my three.js project is compatible with iPhone testing?

I recently developed a new AR project that allows users to interact with AR content through their phone camera on a website. However, I am facing an issue where I cannot easily test my website on an iPhone whenever I make changes to the code. Currently, ...

What is the best way to gradually transform a continuously shifting text color into a single, consistent hue?

Hey there, wonderful people of StackOverflow! I am absolutely stumped and cannot figure out how to smoothly transition this color-changing text from its current state into a different solid color when the submit button is clicked. Is there a skilled indiv ...

The SVG syntax underwent alterations following its transmission via email

Can someone please help me fix this code? My visual studio code interpreter seems to be having trouble recognizing the style code within it. <?xml version="1.0" encoding="utf-8"?> <!-- (c) ammap.com | SVG weather icons --> <svg vers ...

How can you ensure that this intricate text on the page is verified using Selenium with Java?

I need to confirm if the following HTML code is present on the page, which contains // characters, etc. I am using selenium and Java for this task. <div class="powatag" data-endpoint="https://api-sb2.powatag.com" data-key="b3JvYmlhbmNvdGVzdDErYXBpOjEyM ...

Step-by-step guide on crafting your own twig with customizable color options using a colorpicker

I attempted to implement a color picker for my HTML form in Symfony, and it functioned flawlessly. However, when I tried using Twig for my form, I encountered difficulties meeting my expectations. I explored the GenemuFormBundle at https://github.com/gen ...

Highlighted top and bottom borders accenting a vertical list

I am working on a list that contains four items. Each item should have top and bottom borders set to 1px solid grey by default. There is a JavaScript rotator in place that adds a 'highlighted' class to the selected list element, which should have ...

Simple method to toggle between elements using jQuery

After creating a script using jQuery to show/hide content when tabs are clicked (which also changes the color of the clicked tab), everything is functioning smoothly. However, I believe there may be a more efficient way to switch between content that allow ...

Tips for fixing alignment problems using CSS

I am experiencing a problem where I am unable to figure out how to align the content in one column with text positioned next to it in a right-aligned format. /* Personal Details */ .personal-info { width: 90%; padding: 25px; border-bottom: 1px so ...

What is the best way to generate two <div> elements, with one of them being centrally fixed?

When it comes to CSS, I must admit that it's not my strong suit. My current challenge is to create two boxes or divs where one remains fixed in the center of the page, while the other adapts and positions itself right next to the first one. Essentiall ...

Hero Sticky Transition with 100vhDivElement

My code is giving me some trouble. I want the div with text to stay at the bottom of the hero section with a transparent background. When that div reaches the top, it should stick with a black background. The issue is that I have set it in the javascript v ...

Having difficulty retrieving data from an HTML file using HTMLEditorKit with Java

Within my HTML code, I have tags structured like this: <div class="author"><a href="/user/1" title="View user profile.">Apple</a> - October 22, 2009 - 01:07</div> I am aiming to retrieve the date, "October 22, 2009 - 01:07" from e ...

Nested datalist Enhanced for Table Design

When using the codes below, I expect a result like picture 1 but instead, I am getting a result like picture 2. How can I resolve this issue? ASPX code: <asp:datalist id="dtlUrun" runat="server" RepeatDirection="Horizontal"> <ItemTem ...

Retrieve SQL data and store it in a JavaScript variable

Need assistance with fetching data from SQL and storing it in a JavaScript variable. I have already connected PHPMyAdmin to my website. I am attempting to retrieve two variables (date) from my SQL table. JAVASCRIPT: var countdown_48 = new Date; countdow ...

The class is failing to be applied to the parent div that holds an id starting with the letter x

I have been attempting to assign a class to the parent container when the child element has the 'hidden' class. If not, then a different class should be added. function tagMissions() { if ($('span[id^="mission_participant_new"]').h ...

Creating a scrolling effect similar to the Nest Thermostat

After researching countless references, I am determined to achieve a scrolling effect similar to the Nest Thermostat. I came across this solution on this JSFiddle, but unfortunately, the parent element has a fixed position that cannot be utilized within my ...

Load a form using ajax and submit it using jQuery

For a while now, I have been facing challenges in figuring out what's going wrong with my code. The issue arises when I try to submit a form using jQuery that was loaded through ajax. The form loads perfectly fine within a div after invoking the ajax ...

The alert dialog does not appear when clicking on "a"

Attempting to retrieve the ID of the clicked element using jQuery has proven unsuccessful for me. Below is the jQuery code I am working with: <script> $(function(){ $("a.step").click(function(){ var id = $(this).attr('id'); ...

showing information from a table column

Utilizing the jQuery DataTables plugin with a JSF <h:dataTable>. The page contains 86 records. +++++++++++++++++++++++++++++++++++++ + SN. + Name + Email + +++++++++++++++++++++++++++++++++++++ + 1 + Name 1 + Email 1 + + ...

It is impossible to change the text color to white

I am working on some JSX code which looks like the following: <div style={Object.assign({}, styles.occupyAllScreen, styles.backgroundContainer)}> <div styles={styles.overimposingText}> <h2 styles={{color: 'white !important& ...