Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a circle avatar from a rectangle image keeping proportions and just using centre of image

I have images that are 480px wide by 640px high.

I want to display them as circles on a webpage 150px wide using CSS. But I want to see the centre of the image.

So take 80px of the top and bottom of the original image produces the square with the image I want to see. I then want to make that into a circle.

Everything I try stretches the image as most examples are to use a square image to start with.

Can any one help

like image 954
user3086854 Avatar asked Oct 31 '14 18:10

user3086854


People also ask

How do you make a round avatar in CSS?

You can set the image as the background of an element, set its background-size to cover , and then use border-radius to round the edges. This works with images of any aspect ratio, and will scale the image to fill the container without stretching/distorting it.


3 Answers

You can set the image as the background of an element, set its background-size to cover, and then use border-radius to round the edges. This works with images of any aspect ratio, and will scale the image to fill the container without stretching/distorting it.

#avatar {
    /* This image is 687 wide by 1024 tall, similar to your aspect ratio */
    background-image: url('http://i.stack.imgur.com/Dj7eP.jpg');
    
    /* make a square container */
    width: 150px;
    height: 150px;

    /* fill the container, preserving aspect ratio, and cropping to fit */
    background-size: cover;

    /* center the image vertically and horizontally */
    background-position: top center;

    /* round the edges to a circle with border radius 1/2 container size */
    border-radius: 50%;
}
<div id="avatar"></div>
like image 83
meagar Avatar answered Oct 10 '22 17:10

meagar


Another solution is to set the sizes for img and use "object-fit: cover;". It will do the same as the "background-size:cover" without messing with background images.

img {
  object-fit: cover;
  border-radius:50%;
  width: 150px;
  height: 150px;
}
<img src="http://i.stack.imgur.com/Dj7eP.jpg" />

I found it on Chris Nager post. - 1

Edit: As @prograhammer mentioned, this doesn't work on IE. Edge supports it only on <img> tags.

Partial support in Edge refers to object-fit only supporting <img> - 2

Edit 2: This post of Primož Cigler shows how to use Modernizr to add a fallback support for IE but, in this case, you will need to add a wrapper to de image.

like image 27
Brosig Avatar answered Oct 10 '22 19:10

Brosig


If the image is required to be in the HTML rather than a background image

.wrapper {
  width:150px;
  height:150px;
  margin: 25px auto;
  overflow: hidden;
  border-radius:50%;
  position: relative;
}

.wrapper img {
  position: absolute;
  top:50%;
  left:50%;
  transform: translate(-50%,-50%)
}
<div class="wrapper">
  <img src="http://lorempixel.com/output/business-q-c-640-480-4.jpg" alt="" />
</div>
like image 23
Paulie_D Avatar answered Oct 10 '22 18:10

Paulie_D