Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change image size via parent div

I tried to do so but it didn't change the size:

<div style="height:42px;width:42px">
   <img src="http://someimage.jpg">
</div>

What will re-size it (I can't edit/access the img element itself)?

Thanks in advance.

like image 504
funerr Avatar asked Mar 04 '12 16:03

funerr


People also ask

How do I resize an image using CSS?

We can resize the image by specifying the width and height of an image. A common solution is to use the max-width: 100%; and height: auto; so that large images do not exceed the width of their container. The max-width and max-height properties of CSS works better, but they are not supported in many browsers.

How do I make an image not overflow in a div?

Give the container a fixed height and then for the img tag inside it, set width and max-height . The difference is that you set the width to be 100%, not the max-width .


3 Answers

I'm not sure about what you mean by "I have no access to image" But if you have access to parent div you can do the following:

Firs give id or class to your div:

<div class="parent">
   <img src="http://someimage.jpg">
</div>

Than add this to your css:

.parent {
   width: 42px; /* I took the width from your post and placed it in css */
   height: 42px;
}

/* This will style any <img> element in .parent div */
.parent img {
   height: 100%;
   width: 100%;
}
like image 189
Ilja Avatar answered Oct 22 '22 08:10

Ilja


Apply 100% width and height to your image:

<div style="height:42px;width:42px">
  <img src="http://someimage.jpg" style="width:100%; height:100%">
</div>

This way it will same size of its parent.

like image 8
Sarfraz Avatar answered Oct 22 '22 09:10

Sarfraz


Actually using 100% will not make the image bigger if the image is smaller than the div size you specified. You need to set one of the dimensions, height or width in order to have all images fill the space. In my experience it's better to have the height set so each row is the same size, then all items wrap to next line properly. This will produce an output similar to fotolia.com (stock image website)

with css:

parent {
   width: 42px; /* I took the width from your post and placed it in css */
   height: 42px;
}

/* This will style any <img> element in .parent div */
.parent img {
   height: 42px;
}

without:

<div style="height:42px;width:42px">
    <img style="height:42px" src="http://someimage.jpg">
</div>
like image 1
JJ_Coder4Hire Avatar answered Oct 22 '22 09:10

JJ_Coder4Hire