Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css: fit rounded (circle) image (do not stretch it)

Tags:

For example I have such images:

enter image description here enter image description here

and css:

.company-header-avatar{     width: 60px;     height: 60px;     -webkit-border-radius: 60px;     -webkit-background-clip: padding-box;     -moz-border-radius: 60px;     -moz-background-clip: padding;     border-radius: 60px;     background-clip: padding-box;     margin: 7px 0 0 5px;     float: left; } 

As the result I get:

enter image description here

But they are stretched. Are there any ways to make them rounded, but without stretching? (for example, get part from middle, etc)

Fiddle:

http://jsfiddle.net/73h7try9/

like image 734
brabertaser19 Avatar asked Sep 09 '15 10:09

brabertaser19


People also ask

How do I fit a div image without stretching it?

Those images on the site you linked to are actual size, so the simple answer is just to resize the image. You can't really do this without "stretching" the image if the image happens to be less than 300px wide or tall, but you can do it without changing the ratio, which is what I think you mean.

What CSS styles if any can you use to display the image with gently rounded corners?

The CSS property border-radius adds rounded corners on images. You can round all of the image's corners or just select corners, vary the radius on different corners or display an image in the shape of an oval or a circle.


1 Answers

I recommend applying the images with background-image to a div and then applying background-size: cover to ensure the ratio stays correct regardless of the image's original size/ratio:

JS Fiddle

HTML

<div class="company-header-avatar" style="background-image: url(https://dl.dropboxusercontent.com/u/59666091/6E06C3D.jpeg)"></div>  <div class="company-header-avatar" style="background-image: url(https://dl.dropboxusercontent.com/u/59666091/8WluhcUlWl8.jpg)"></div> 

CSS

.company-header-avatar{     width: 60px;     height: 60px;     -webkit-border-radius: 60px;     -webkit-background-clip: padding-box;     -moz-border-radius: 60px;     -moz-background-clip: padding;     border-radius: 60px;     background-clip: padding-box;     margin: 7px 0 0 5px;     float: left;     background-size: cover;     background-position: center center; } 
like image 67
Derek Story Avatar answered Oct 24 '22 11:10

Derek Story