I want to make a centered circular image from rectangle photo. The photo\'s dimensions is unknown. Usually it\'s a rectangle form. I\'ve tried a lot of methods:
CSS:
The object-fit property provides a non-hackish way for doing this (with image centered). It has been supported in major browsers for a few years now (Chrome/Safari since 2013, Firefox since 2015, and Edge since 2015) with the exception of Internet Explorer.
img.rounded {
object-fit: cover;
border-radius: 50%;
height: 100px;
width: 100px;
}
<img src="http://www.electricvelocity.com.au/Upload/Blogs/smart-e-bike-side_2.jpg" class="rounded">
insert the image and then backhand all you need is:
<style>
img {
border-radius: 50%;
}
</style>
** the image code will be here automatically**
You need to use jQuery to do this. This approach gives you the abbility to have dynamic images and do them round no matter the size.
My demo has one flaw right now I don't center the image in the container, but ill return to it in a minute (need to finish a script I'm working on).
DEMO
<div class="container">
<img src="" class="image" alt="lambo" />
</div>
//script
var container = $('.container'),
image = container.find('img');
container.width(image.height());
//css
.container {
height: auto;
overflow: hidden;
border-radius: 50%;
}
.image {
height: 100%;
display: block;
}
Try this:
img {
height: auto;
width: 100%;
-webkit-border-radius: 50%;
-moz-border-radius: 50%;
-ms-border-radius: 50%;
-o-border-radius: 50%;
border-radius: 50%;
}
DEMO here.
OR:
.rounded {
height: 100px;
width: 100px;
-webkit-border-radius: 50%;
-moz-border-radius: 50%;
-ms-border-radius: 50%;
-o-border-radius: 50%;
border-radius: 50%;
background:url("http://www.electricvelocity.com.au/Upload/Blogs/smart-e-bike-side_2.jpg") center no-repeat;
background-size:cover;
}
DEMO here.
Johnny's solution is good. I found that adding min-width:100%, really helps images fill the entire circle. You could do this with a combination of JavaScript to get optimal results or use ImageMagick - http://www.imagemagick.org/script/index.php if you're really serious about getting it right.
.image-cropper {
width: 35px;
height: 35px;
position: relative;
overflow: hidden;
border-radius: 50%;
}
.image-cropper__image {
display: inline;
margin: 0 auto;
height: 100%;
min-width: 100%;
}
<div class="image-cropper">
<img src="#" class="image-cropper__image">
</div>
The approach is wrong, you need to apply the border-radius
to the container div
instead of the actual image.
This would work:
.image-cropper {
width: 100px;
height: 100px;
position: relative;
overflow: hidden;
border-radius: 50%;
}
img {
display: inline;
margin: 0 auto;
height: 100%;
width: auto;
}
<div class="image-cropper">
<img src="https://via.placeholder.com/150" class="rounded" />
</div>