Allowing images to shrink, but not stretch

fnord12

I have a site with 4,000+ pages and 10 or more jpeg images per page, of varying sizes. I'm trying to make the site more mobile friendly. To that end, i want to make it possible for the images to shrink to fit on smaller screens. I know that i can do something like this to signal that the images can shrink:

img.bodyImg
{
 width: 100%;
 max-width: 357px;
 height: auto;
}

But what if not all images have a width of 357 (or whatever), and i don't want smaller images stretched beyond their true dimensions? And just to make things more fun, what if the images tags don't have height and width attributes specified?

My goal is to find a solution that doesn't require me to adjust tens of thousands of image calls manually, but i can do a search and replace. Images are currently wrapped in a div container and have a class, like so:

<div class="imgDiv"><img class="bodyImg" src="http://www.example.com/image.jpg"></div>

I'm also open to the possibility that i'm going about this in the wrong way entirely.

showdev

Using max-width is simple, effective, and requires no JavaScript.

The CSS below creates responsive images that shrink to fit the container's width but won't expand beyond their native sizes.

img.bodyImg {
    max-width:100%
}

In the demonstration below, both images are 300px X 75px. The first container is 200px wide and the second one is 400px wide. Notice that the first image shrinks to fit in the container, but the second image does not expand beyond its native size. Also note that the proportions of each image remain accurate.

div {
  background-color: #CCC;
  margin:0 0 .5em;
  padding:.25em;
}
div.one {
  width: 200px;
}
div.two {
  width: 400px;
}
img {
  display:block;
  max-width: 100%;
}
<div class="one">
  <img src="http://lorempixel.com/300/75/abstract/4/" />
</div>
<div class="two">
  <img src="http://lorempixel.com/300/75/abstract/4/" />
</div>

Additional Notes

  1. I've included display:block to remove the descender space below the image.
  2. If your images have specific height and width attributes (as they arguably should), you can add height:auto and/or width:auto to override those attributes.
  3. Bootstrap uses this method for responsive images.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Flex transition: Stretch (or shrink) to fit content

Docker images - types. Slim vs slim-stretch vs stretch vs alpine

What is the suffix "-stretch" means in the circleci images?

How can I stretch/shrink any size image to best fit a square, with only CSS/HTML?

React router not allowing images to load

Flexbox Shrink Images within Div for Column Stacked images

How to stretch property not working on images react native?

Flex items do not vertically shrink when child images vertically shrink in IE

VB stretch DataGridViewImageColumn images programmatically

Flutter: shrink images to fit into row while keeping aspect ratio

Swift: Animating stretch & shrink of an image

How can I smoothly stretch and shrink a revolving line arc?

Shrink a row of images to fit container height and maintain aspect ratios

Bootstrap4 -- fullscreen dashboard with images that resize and fill/stretch into their divs?

How do I make images shrink when the screen is reduced?

Space between images grows on browser shrink

ffmpeg modify audio length/size ( stretch or shrink)

how to shrink all images, jpg in a directory to another directory using imagemagick

Stretch images contained in 1 div across screen

Center and stretch images vertically, center horizontally

Why does my HTML table shrink images from left to right?

Image stretch if smaller shrink if bigger and centered

Responsive layout - 4 sprites in a row, stretch/shrink to enclosure

Android Widget Stretch/Shrink

How do I set an element's width while still allowing text to stretch it?

Using flexbox why two images with different proportions stretch differently

Stretch Images to screen size in GridLayout Android

CSS, Bootstrap, HTML - Shrink responsive Images to get equal size in the row

Meme Generator: Event Listener not allowing for multiple images on the page at once

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive