Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scaling div like an image

Tags:

html

css

scaling

I'm doing a list of items, but it has some challenges:

  • Responsive;
  • The "title" may have more than one line;
  • Sometimes a I need to show a icon with a color in the background instead of full image.

This is the image of what I'd expect: Final result expected

And what I've got: http://codepen.io/caio/pen/ygkfm/ What I have

As you can see, I can't set the same scaling to an "image" div when it has a icon. Is there any solution for my problem?

like image 538
Caio Tarifa Avatar asked Mar 27 '14 20:03

Caio Tarifa


People also ask

How do I make a div fit an image?

To auto-resize an image or a video to fit in a div container use object-fit property. It is used to specify how an image or video fits in the container. object-fit property: This property is used to specify how an image or video resize and fit the container.

How do I resize a div proportionally?

For proportional resizing purposes, it makes matters extremely simple: Define the width of an element as a percentage (eg: 100%) of the parent's width, then define the element's padding-top (or -bottom) as a percentage so that the height is the aspect ratio you need. And that's it!

How do I fit a div image without stretching it?

How to fit image without stretching and maintain aspect ratio? Answer: If you want to use the image as a CSS background, there is an elegant solution. Simply use cover or contain in the background-size CSS3 property. contain will give you a scaled-down image.

How do I make an image fit in a div responsive?

Answer: Use the CSS max-width Property You can simply use the CSS max-width property to auto-resize a large image so that it can fit into a smaller width <div> container while maintaining its aspect ratio.


1 Answers

I am assuming your images (exept icons) all have the same aspect ratio as in your example.

In this case, you can use padding bottom to keep the height of the image container. As padding-bottom is calculated according to the width of the container, it will keep it's aspect ratio whatever the content (you will have to position the content with position:absolute; so it doesn't change the dimesions of the container).

Here is a demo Showing what you can do.sorry I'm not into codePen

I also added an other container to center the icons horizontaly.

* {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}
.items {
    margin: 50px auto 0;
    width: 90%;
    *zoom: 1;
}
.items:before, .items:after {
    content:" ";
    display: table;
}
.items:after {
    clear: both;
}
.items .item {
    border: 1px solid #ddd;
    float: left;
    width: 32%;
}
.items .item:nth-child(3n+2) {
    margin: 0 2%;
}
.items .item .image {
    background: #eee;
    padding-bottom:50%;
    position:relative;
}
.items .item .image .img_in{
    position:absolute;
    width:100%;
    height:100%;
    text-align:center;
}
.items .item .image img {
    display: block;
    width: 100%;
    height:100%;
}
.items .item .image img.icon {
    height: 80%;
    margin:0 auto;
    position: relative;
    top: 10%;
    width: auto;
}
.items .item .title {
    margin: 0;
    padding: 20px;
}
like image 179
web-tiki Avatar answered Nov 15 '22 21:11

web-tiki