0

I have a responsive image list. Each image is inside a container. I want the image container to be 75% of its first container (unit container in this case)

the image ration is 1:1

I played a little with the image container percentage width but it feels like this is not the solution.

<ul class="list-inline unit_list ">
<li class="unit_list_item col-xs-24 col-sm-12 col-md-8">
    <a href='#' alt="unit">
        <div class="unit_container">
            <div class="icon_container unit_icon">
                <img class="img-responsive unit_image" src="http://placehold.it/60X60" />
            </div>
            <div class="unit_name">FREE</div>
        </div>
    </a>
</li></ul>

Btw, I'm using bootstrap if that's matter.

http://jsfiddle.net/wmu3w3ej/1/

user2587454
  • 903
  • 1
  • 19
  • 44

3 Answers3

2

Thanks to @Mary Melody

transform: scale(0.75);

works like magic

I'm a little afraid to use it since it's so simple.

any thoughts?

user2587454
  • 903
  • 1
  • 19
  • 44
  • This is normally not where you ask questions, but answer them, even for yourself. Transform has good support for modern browsers look at the issues here and which browsers support it: http://caniuse.com/#search=transform – Christina Sep 23 '14 at 14:55
1

Using the logic from here: https://stackoverflow.com/a/20117454/3389737

I have applied it to your situation: http://jsfiddle.net/phwaLmen/1/

#wrapper
{
    position: relative;
    width: 50%;
    overflow: hidden;
}

#wrapper:before
{
    content: "";
    display: block;
    padding-top: 75%;
}

#image
{
    position: absolute;
    top:      0;
    left:     0;
    bottom:   0;
    right:    0;
    height: 100%;
    width: 100%;
}
<div id="wrapper">
    <img src="http://placehold.it/350x350" id="image">
</div>

Add relative positioning to the parent, set its width as you'd like and make sure the overflow is hidden.

Create a :before element for the wrapper with a padding-top of 75%. Since there is no height specified for the #wrapper, this 75% is based on the width of the element :)

Then you have your image, positioned absolutely and then fitted to the container. If you want the image to be cropped instead of resized, remove the height: 100% and width: 100% style rules from it.

Community
  • 1
  • 1
Luke
  • 3,985
  • 1
  • 20
  • 35
  • Thanks i tried this but it doesn't keep the aspect ration 1:1 of the image. – user2587454 Sep 23 '14 at 09:02
  • You asked for 75%, so it will never be 1:1 unless you use an image that is in those proportions? Also if you remove the last two styles to the image, as mentioned it crops it instead of resizing it. – Luke Sep 23 '14 at 09:06
  • It doesn't really make sense – Luke Sep 23 '14 at 11:30
0

You can do it like this (in your html):

<img src="img.jpg" height="75%" />

Good luck!

Wijnand M
  • 372
  • 1
  • 3
  • 12