Resizing Images in a Table with Proportional Scaling

I am working on a HTML table that has a width set to 100%. This table consists of two rows, each with two cells. Inside each cell, there is an image with a width of 100% and height set to auto, allowing the image to scale proportionally within the cell.

Here is the HTML code snippet:

<table cellspacing="0" cellpadding="0">
    <tr>
        <td>
            <img src="http://idevicegazette.com/wp-content/uploads/2013/03/htc-one-sample-002-200x200.jpg" />
            <div class="info"></div>
        </td>
        <td>
            <img src="http://idevicegazette.com/wp-content/uploads/2013/03/htc-one-sample-002-200x200.jpg" />
            <div class="info"></div>
        </td>
    </tr>
    <tr>
        <td>
            <img src="http://idevicegazette.com/wp-content/uploads/2013/03/htc-one-sample-002-200x200.jpg" />
            <div class="info"></div>
        </td>
        <td>
            <img src="http://idevicegazette.com/wp-content/uploads/2013/03/htc-one-sample-002-200x200.jpg" />
            <div class="info"></div>
        </td>
    </tr>
</table>

And here is the CSS style applied to it:

table
{
    width: 100%;
}

table td
{
    width: 50%;
    position relative;
}

img
{
    width: 100%;
    height: auto;
}

To address a specific issue, I have created a JSFiddle (recommended use in Chrome):

http://jsfiddle.net/Fk8vQ/3/

If you resize the window in the result area, you may notice that the images in the right column can be 1 pixel smaller than those in the left column. My goal is to ensure all images maintain the exact same size, regardless of the column they are in.

I understand that this problem arises when the table width is an odd number of pixels, leading to slight discrepancies in image sizes. I am seeking a workaround to address this issue.

Answer №1

If you want to ensure that the table width is always an even number, you can use a simple JavaScript snippet like this:

let screenWidth = window.innerWidth;
if (screenWidth % 2 !== 0) {
    screenWidth -= 1;
}

document.getElementById("tableId").width = screenWidth;

Keep in mind that there may be some slight asymmetry if the window width is an odd number.

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

Tips on duplicating objects in TypeScript with type annotations

My goal is to inherit properties from another object: interface IAlice { foo: string; bar: string; }; interface IBob extends IAlice { aFunction(): number; anotherValue: number; }; let alice: IAlice = { foo: 'hi', bar: 'bye&apo ...

What purpose does sending null to XMLHttpRequest.send serve?

Have you ever wondered why send is often called like this? xhr.send(null) instead of just xhr.send() ? W3, MDN, and MSDN all mention that the argument is optional. Additionally, the ActiveX control seems to work without it: hr=pIXMLHTTPRequest.Create ...

Spin the SVG image upon clicking the Bootstrap 4 collapse feature

I am attempting to create a toggle effect for an SVG icon, rotating it from the up position to the down position when clicked. While a pure CSS solution would be ideal, I am struggling to implement it for an SVG icon. Unfortunately, I cannot use a font ic ...

Navigating HTML and clicking on links

Currently, I am working on a code that is designed to open a URL, locate the 3rd link, and repeat this process three times with the new URL. However, I am facing a challenge where the code seems to restart with the original URL every time. Could someone p ...

Tips for aligning numbers in a monospaced ordered list

Is there a way to align the numbers in a numbered list within a table using monospace font so that they are at the same level as bullets on a bullet list? Simplest example: ol { font-family: monospace; } ul, ol { margin-top: 10px; margin-bottom: ...

Looking to create a pop-up using javascript, css, or jQuery?

When visiting digg.com and clicking the login button, a sleek in-screen popup appears to input user data. I'm curious about the best way to achieve this on my own site. It is built with RoR and includes some Javascript elements. Searching for "javasc ...

Creating a 3-column Bootstrap layout with a top title div

I'm working on coding divs using Bootstrap along with HTML and CSS to match an image. The image I'm referencing can be seen here: https://i.sstatic.net/SvdFA.png My goal is to have 3 of these images displayed side by side using Bootstrap. Howev ...

Ways to create a table with columns from various fields obtained through an API call

Looking to preprocess data received from an API, the raw data is structured as follows: https://i.sstatic.net/a9Q2Z.png Desiring to dynamically generate a table with columns based on the fields task_name and saved_answers. It's important to note tha ...

Differences between Javascript object constructor and object literal

Similar Questions: Creating Objects - New Object or Object Literal Notation? Literal Notation VS. Constructor to Create Objects in JavaScript As I embark on my initial Javascript tutorial journey, I have come across two distinct methods of creatin ...

Transfer Data from Ajax Response to an Array

I am struggling to properly assign a value from an ajax call to an array and then pass that array. It seems like the variable 'options' is not being recognized as valid. This is the code I have: $.ajax({ url: url, success: function(data){ ...

What method can I use to modify the object's keys based on certain conditions in ES6/React?

How can I dynamically change the value of an object's keys based on specific conditions? What is the most effective way to structure and implement this logic? Data const newData = { id: 111, name: "ewfwef", description: "Hello&qu ...

Seeking assistance with HTML coding to incorporate an image into a website

I am currently customizing a pre-written code to finalize my project. The project involves a game where a snake eats an apple and grows in size. One of the tasks I need to complete is replacing the red blob, representing the apple in the game, with an imag ...

Encountering problems with Node Sass versions while trying to import custom scss files into app.js

I've encountered several articles on stack that didn't quite solve my issues. The problem arose when I tried to import a custom SCSS file into my React app.js to override bootstrap theme colors. Here are the style imports in my App.js: import &q ...

Utilize regular expressions to substitute a specific string of text with an HTML tag

My task is to transform this text: let text = "#Jim, Start editing to see some magic happen!"; into this format: text = "<span style="color:red">#Jim</span>, Start editing to see some magic happen!"; Here is my ...

Navigating to the bottom of a specific element by scrolling

I am currently working on enhancing a module within the application I'm developing. The goal is to automatically scroll the browser window to the bottom of an element when said element's height exceeds the height of the window. The functionality ...

Encountering difficulties with implementing and utilizing bootstrap in Symfony 5.3 with Encore plugin

While I am currently dealing with an older version of Symfony, I decided to create a new 5.3 Symfony application from scratch. However, I am facing difficulties when trying to integrate bootstrap into it. After consulting some documentation, I proceeded to ...

Page loaded with activated Flash functionality

Curious if there's a way, To have my flash map on the site automatically activated upon page load, as opposed to requiring a click to interact with it. I've noticed that some websites already have their flash content active when the page loads, ...

What is the best way to pass the anchor's Id as a parameter to a function?

Update: Apologies folks, I found the issue - Misspelled class name :( Hey there, I have a question that I need some help with. I have a series of anchor tags, each linking to a user's profile by using their username as the link id. All these links ...

The alignment of the Nivo Slider is off-center

Visit to see that the Nivo Slider is not properly centered on the background. I am unsure why this is happening. (Please disregard the 2nd and 3rd pictures as they are not the correct size, but focus on the 1st one). ...

Retrieval and sorting of documents by their unique identifiers

There are approximately 20 documents stored in the Firebase database, each containing fields with unique "id" values. How can we filter the database query to only retrieve documents with a specific "id" and exclude all others? ...