Make multiple images in one row resize proportionally instead of wrapping them to the next line

My phone's screen is small and I want the three images to remain in the same row. In case they don't fit, they should shrink proportionately to maintain alignment (refer to image at the bottom).

Consider the code snippet below:

<div id="example">
    <span id="A"><img src="blabla1.png" /></span>
    <span id="B"><img src="blabla2.png" /></span>
    <span id="C"><img src="blabla3.png" /></span>
</div>

I have tried a simple solution like this before:

#example {
    min-width: 200px;
    height: auto;
}
img {
   height: auto; 
   width: auto; 
}

Check out the Fiddle here: http://jsfiddle.net/fdce0x7k/1/

Image description:

https://i.sstatic.net/qavOu.png

Answer №1

Consider utilizing the flex display along with the view width properties in your CSS:

Check out this example on JSFiddle: https://jsfiddle.net/8k7dtny3/

HTML Code

<div id="sample">
    <span id="X"><img src="http://placehold.it/350x150" /></span>
    <span id="Y"><img src="http://placehold.it/350x150" /></span>
    <span id="Z"><img src="http://placehold.it/350x150" /></span>
</div>

CSS Styles

#sample{
    display:flex;
    flex-wrap:nowrap;
}

#sample span img
{
    width:33vw;
}

Answer №2

A different approach to using flex is by employing CSS tables.

You can set display: table for the parent element .example, followed by setting display: table-cell for the child elements like spans.

The key here is to specify the width of the image as 100%, which will then expand the table size to accommodate all three images effectively. This results in each cell scaling the contained image proportionally.

An advantage of this method is that CSS tables are more universally supported compared to flex currently.

.example {
  border: 1px dashed black;
  display: table;
  width: 100%;
}
.example span {
  display: table-cell;
  width: auto;
  border: 1px solid blue;
  padding: 5px;
}
.example span img {
  vertical-align: top;
  width: 100%;
}
<div class="example">
    <span id="X"><img src="http://placehold.it/500x50" /></span>
    <span id="Y"><img src="http://placehold.it/300x50" /></span>
    <span id="Z"><img src="http://placehold.it/150x50" /></span>
</div>

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

What are the steps to personalize Twitter Bootstrap with Less for changing the height of the Navbar?

I recently stumbled upon some interesting articles that explain how to customize various elements of Twitter Bootstrap using LESS. You can check out one of the articles here and find more information about Twitter Bootstrap here. However, I am still unsure ...

Encountering issues with the hyperlink tag '<a>' in an HTML document

I've encountered an issue with the code on my HTML page: <body> <div align="center"> <a href="../index.html"> <img border="0" src="banner1.jpg" width="800" height="120" alt="Ecommerce Knowledge Base"> &l ...

Assign the callback function to execute when the select element loses focus

Is there a way to trigger a function when the user clicks out of a select menu without selecting an option, even though I know about the onChange and onFocus event listeners associated with the select HTML element? ...

Dynamic row additions causing vanishing rows in the table due to JS/jQuery implementation

I am currently facing an issue while trying to create a table where I can add rows dynamically. Despite looking at various solutions on this topic, none of them seem to resolve the problem I am encountering. The newly added rows disappear immediately after ...

having trouble with changing the button's background color via toggle

I've been experimenting with toggling the background color of a button, similar to how changing the margin works. For some reason, the margin toggles correctly but the button's color doesn't. <script> let myBtn = document.querySele ...

highlight the selected option in the ng-repeat list of items

Looking for some assistance with a coding problem I'm having. I keep running into an error when trying to make a selected item in an ng-repeat list highlight. The CSS style is only applied on the second click, not the first. Additionally, I need to en ...

AngularJS - Create Alert Pop-Up for Missing Input Fields

I've been struggling to set up my app so that it displays an alert when the user attempts to submit a form with an empty input box. Despite having a confirmation popup working in another part of the application, I can't seem to figure out why thi ...

Is it possible to implement a code that will organize a dropdown list alphabetically?

Could someone assist me in sorting the location items alphabetically in this search form at: I would like the locations to be displayed in alphabetical order. Any help with updating the code below would be greatly appreciated. Here is the current code sn ...

JQuery restricts input to only allow numbers with a set number of digits, ensuring uniqueness

Can someone assist me in creating a jQuery script that only allows unique numbers with exactly 4 digits in a numeric field? Here is the HTML code: <input type="text" id="registration_number" class="input-sm" name="registration_number" maxlength="6" re ...

H1 and span elements experiencing abnormal size discrepancies

Check out this code snippet: <h1><a href="">Windows App Store<br /><span class="smallSubText">applications</span></a></h1> and here's the CSS styling: #windowsStoreApplications { float: right; width ...

Struggling to update a scope variable when utilizing Firebase's Facebook authentication with AngularJS

Hey there... I'm currently exploring AngularJS and attempting to implement Facebook's connect feature using Firebird. Almost everything is running smoothly - the connection to my Facebook account is successful, and the information is retrieved w ...

Unable to use href attribute as intended

HTML: <a id="Switch">Click here to switch</a> <a href="image1_large.png" class="mainA blade"> <img id="mainImage" src="image1.png"/></a> Javascript: <script> $(function() { $('#Switch').click(functio ...

Attempting to align input fields and spans side by side

Is there a way to properly align multiple inputs next to each other with dots in between, resembling an IPv4 address? Currently, the span appears to float in the center. How can this be resolved? It's worth noting that all elements in the image have b ...

What is the best way to implement a CSS transition in React when the state changes, the component remounts, or it re-rend

Imagine having a React functional component with some basic state: import React, { useState } from 'react' import { makeStyles } from "@material-ui/core" export default function Cart() { const [itemNumber, setItemNumber] = useState ...

Customizing the CSS for individual posts in WordPress based on their post

I am interested in establishing a unique look for each individual post on my wordpress.org blog. Is there a way to customize the CSS code of individual posts without changing the design of other posts? I have tried using categories to create a new PHP fi ...

Ways to consistently return to the main home URL/directory within an HTML document

Greetings to all! Currently facing a challenge with HTML pathing. I want to ensure that every time I log out from Dynamics CRM Portals, I am redirected back to the "SignIn" page. The issue arises when I am in various URLs. For example, logging out from d ...

Edit: "Submit a binary file with just JavaScript"

I am currently developing a Chrome application that utilizes the HTML5 Filesystem API to enable users to import and synchronize files. A challenge I'm facing is when attempting to sync image files, as they appear to become corrupted during the upload ...

Integrate a Google Maps API Javascript into a Flex application

I'm trying to incorporate a Google Maps API JavaScript into my Flex Application, but I have been unsuccessful so far despite looking at various examples. If anyone has any tips or guidance on how to achieve this, I would be extremely grateful for you ...

Custom component not rendering expected CSS style

I have successfully developed a custom web component without using any framework. I then proceeded to populate it with content from a template tag. Although I was able to manipulate the content using JavaScript, I encountered difficulties when trying to m ...

Text positioned beneath a toggle switch in html that overlaps

Is there a way to fix the issue of the label on the left side overlapping below the selector switch on my website? I want to ensure that both labels are perfectly distributed on each side of the switch. Here is the code snippet: <div class="ro ...