Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Image size, how to fill, but not stretch?

Tags:

css

image

I have an image, and I want to set it a specific width and height (in pixels)

But If I set width and height using css (width:150px; height:100px), image will be stretched, and It may be ugly.

How to Fill images to a specific size using CSS, and not stretching it?

Example of fill and stretching image:

Original Image:

Original

Stretched Image:

Stretched

Filled Image:

Filled

Please note that in the Filled image example above: first, image is resized to 150x255 (maintained aspect ratio), and then, it cropped to 150x100.

like image 795
Mahdi Ghiasi Avatar asked Aug 01 '12 10:08

Mahdi Ghiasi


People also ask

How do I make an image fit without stretching CSS?

You can use the css property object-fit . ("sets how the content of a replaced element, such as an <img> or <video> , should be resized to fit its container.") Related: object-position (specifies the alignment of an element's contents within its box.)

How do you fill a box with an image without distorting it?

Using object-fit The image should completely fill the box, retaining aspect ratio, and cropping any excess on the side that is too big to fit. The image should fit inside the box, with the background showing through as bars on the too-small side.

How do I make an image fit my CSS size?

Using CSS, you can set the background-size property for the image to fit the screen (viewport). The background-size property has a value of cover . It instructs browsers to automatically scale the width and height of a responsive background image to be the same or bigger than the viewport.


2 Answers

You can use the css property object-fit. ("sets how the content of a replaced element, such as an <img> or <video>, should be resized to fit its container.")

.cover {   object-fit: cover;   width: 50px;   height: 100px; }
<img src="http://i.stack.imgur.com/2OrtT.jpg" class="cover" width="242" height="363" />

See example here

There's a polyfill for IE: https://github.com/anselmh/object-fit

Related: object-position (specifies the alignment of an element's contents within the it's box.)

like image 122
afonsoduarte Avatar answered Sep 18 '22 12:09

afonsoduarte


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.

.container {    width: 150px;    height: 100px;    background-image: url("http://i.stack.imgur.com/2OrtT.jpg");    background-size: cover;    background-repeat: no-repeat;    background-position: 50% 50%;  }
<div class="container"></div>​

While cover will give you a scaled up image, contain will give you a scaled down image. Both will preserve the pixel aspect ratio.

http://jsfiddle.net/uTHqs/ (using cover)

http://jsfiddle.net/HZ2FT/ (using contain)

This approach has the advantage of being friendly to Retina displays as per Thomas Fuchs' quick guide.

It's worth mentioning that browser support for both attributes excludes IE6-8.

like image 23
Marcelo De Polli Avatar answered Sep 18 '22 12:09

Marcelo De Polli