0

The images can be wider than the width of their container or smaller than it. To control the width of larger images, I can use max-width: 100%. Similarly, I can also set width:100% for smaller images.

Is there any way to set image width to 100% only if the original image is at least 50% of its container width? I will prefer a CSS based solution if it exists because the images will load dynamically.

SanJeet Singh
  • 1,291
  • 2
  • 15
  • 28

2 Answers2

0

Since the images will load dynamically, I think your best shot is with javascript. You'll need to get the width of the image (Determine original size of image cross browser?), and then you can compare that to the parent of the image, and determine if it's above or below 50%.

UPDATE:

Considering the question didn't provide any sample code, I didn't want to just guess. But, the pseudo code would look something like this:

// after ajax completes
var image = jQuery('.image'),
parent = image.parent();
if (image.width() * 2 >= parent.width()) {
   image.css('width','100%');
}
Community
  • 1
  • 1
James Collier
  • 157
  • 10
0
var containerDiv = $(".container");
var image = $(".img");
image.width(image.width()>containerDiv.width()?containerDiv.width():image.width());
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116