98

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:

Code

.image-cropper {
    max-width: 100px;
    height: auto;
    position: relative;
    overflow: hidden;
}

.image-cropper img{
    display: block;
    margin: 0 auto;
    height: auto;
    width: 150%; 
    margin: 0 0 0 -20%;
    -webkit-border-radius: 50%;
    -moz-border-radius: 50%;
    -ms-border-radius: 50%;
    -o-border-radius: 50%;
    border-radius: 50%;  
}
<div class="image-cropper">
   <img src="https://sf1.autojournal.fr/wp-content/uploads/autojournal/2012/07/4503003e3c38bc818d635f5a52330d.jpg" class="rounded" />
</div>
MaxiGui
  • 6,190
  • 4
  • 16
  • 33
49volro
  • 1,095
  • 1
  • 9
  • 9

12 Answers12

134

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>
cloned
  • 6,346
  • 4
  • 26
  • 38
Johnny Kutnowski
  • 2,340
  • 1
  • 13
  • 15
84

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="https://picsum.photos/200/300" class="rounded">
HynekS
  • 2,738
  • 1
  • 19
  • 34
Dispenser
  • 1,111
  • 7
  • 9
  • 2
    Brilliant! I love this simple, one-step solution. I has to remove the `img` from the css you provided and add it as a class to the image, but it worked like a charm! Thank you! – jord8on May 29 '18 at 07:03
  • 1
    This is great, thank you! Should be the accepted answer imo – Rolv Apneseth Nov 04 '21 at 11:19
23

If you can live without the <img> tag, I suggest you use the photo as a background image.

.cropcircle{
    width: 250px;
    height: 250px;
    border-radius: 100%;
    background: #eee no-repeat center;
    background-size: cover;
}

#image1{
    background-image: url(http://www.voont.com/files/images/edit/7-ridiculous-ways-boost-self-esteem/happy.jpg);
}
<div id="image1" class="cropcircle"></div>
Tom
  • 648
  • 3
  • 9
13

A simple one liner.

clip-path: circle();
Trevor
  • 2,511
  • 1
  • 14
  • 25
Baabar Ali
  • 171
  • 1
  • 6
10

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.

codingrose
  • 15,563
  • 11
  • 39
  • 58
3

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>
evanjmg
  • 3,465
  • 1
  • 14
  • 12
2

I know many of the solutions mentioned above works, you can as well try flex.

But my image was rectangular and not fitting properly. so this is what i did.

.parentDivClass {
    position: relative;
    height: 100px;
    width: 100px;
    overflow: hidden;
    border-radius: 50%;
    margin: 20px;
    display: flex;
    justify-content: center;
}

and for the image inside, you can use,

child Img {
    display: block;
    margin: 0 auto;
    height: 100%;
    width: auto;
}

This is helpful when you are using bootstrap 4 classes.

Bas van Dijk
  • 816
  • 7
  • 20
Akshay L
  • 508
  • 2
  • 7
  • 19
0

The best way I've been able to do this is with using the new css object-fit (1) property and the padding-bottom (2) hack.

You need a wrapper element around the image. You can use whatever you want, but I like using the new HTML picture tag.

.rounded {
  display: block;
  width: 100%;
  height: 0;
  padding-bottom: 100%;
  border-radius: 50%;
  overflow: hidden;
}

.rounded img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}


/* These classes just used for demo */
.w25 {
  width: 25%;
}

.w50 {
  width: 50%;
}
<div class="w25">
<picture class="rounded">
  <img src="https://i.imgur.com/A8eQsll.jpg">
</picture>
</div>

<!-- example using a div -->
<div class="w50">
<div class="rounded">
  <img src="https://i.imgur.com/A8eQsll.jpg">
</div>
</div>

<picture class="rounded">
  <img src="https://i.imgur.com/A8eQsll.jpg">
</picture>

References

  1. CSS Image size, how to fill, not stretch?

  2. Maintain the aspect ratio of a div with CSS

Community
  • 1
  • 1
stldoug
  • 853
  • 9
  • 14
0

The accepted answer probably works for some situations, but it depends on the ratio of the rectangle and any predetermined styles.

I use this method because it's more compatible than solutions only using object-fit:

.image-cropper {
   width: 150px;
   height: 150px;
   position: relative;
   overflow: hidden;
   border-radius: 50%;
   border:2px solid #f00;
}

/* Common img styles in web dev environments */
img {
   height: auto;
   max-width: 100%;
}

/* Center image inside of parent */
img.center {
   position: absolute;
   top: 50%;
   left: 50%;
   transform: translate(-50%, -50%);
}

/* For horizontal rectangles */
img.horizontal {
   height: 100%;
   width: auto;
   max-width: 9999px; /* max-content fall back */
   max-width: max-content;
}
<div class="image-cropper">
  <img src="https://via.placeholder.com/300x600" class="center" />
</div>

<div class="image-cropper">
  <img src="https://via.placeholder.com/600x300" class="horizontal center" />
</div>

If you run the snippet you can see, for horizontal rectangles we add another class .horizontal.

We override max-width to allow the img to go larger than 100% of the width. This preserves the aspect ratio, preventing the image from stretching.

However, the image will not be centered and that's where the .centered class comes in. It uses a great centering trick to absolute position the image in the center both vertically and horizontally.

More information on the centering at CSS Tricks

More than likely you won't always know what ratio the image will be, so this is why I'd suggest using javascript to target the img and add the .horizontal class if needed.

Here is a stack overflow answer that would work

Jared Rice
  • 413
  • 3
  • 9
0
.image-cropper {
    width: 100px;
    height: 100px;
    overflow: hidden;
    border-radius: 50%;
    background: #ffffff;
    box-shadow: 0px 8px 18px 2px rgba(8, 49, 150, 0.05);
}
.image-cropper img {
    width: 50%;
}
Kevin M. Mansour
  • 2,915
  • 6
  • 18
  • 35
surbhi241
  • 81
  • 1
  • 2
-2

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;    
}
Dejan.S
  • 18,571
  • 22
  • 69
  • 112
  • 1
    Definitely don't need jQuery. See accepted answer or @Tom's – abettermap Feb 19 '17 at 03:05
  • @abettermap those solution don't have dynamic height, if you read the question, "Usually it's a rectangle form." meaning not always, therefor I get the image height with javascript. Seo value is lost with Toms solution. So read and think best pracitce also before downvoting. – Dejan.S Feb 20 '17 at 11:14
  • The answers using `background-image` have dynamic height in the sense that the image will fill the circular space regardless of what the height is, which I think was the OP's goal. Re: best practices, I think some might argue that not using JS for styling would be best practice. You have a point with SEO, however, so I will edit your response. Feel free to edit further. – abettermap Feb 26 '17 at 18:32
  • This "answer" should be deleted. – reformed Jun 15 '23 at 21:48
  • @reformed the answer was made in 2014 and css has evolved in the past 9 years, also care to motivate the "constructive feedback" as to why it should be deleted? – Dejan.S Jun 21 '23 at 22:18
  • @Dejan.S 1) OP specifically requested CSS, not jQuery. 2) "You need to use jQuery to do this" is not true. 3) You admit your demo is flawed, and the -4 negative impact likely renders it not worth your time to "fix"/"finish" it. – reformed Jun 23 '23 at 13:47
  • @reformed Sure fair enough with the CSS, but, what all the answers fail to provide is the dynamic part as the OP clearly state that the dimensions are not known and usually a rectangle as in "not always". Read the comments to my answer here where I also bring up the SEO. Also keep in mind that my answer was made in 2014. As far as the "flaw part", many times giving a OP a hint the solution might come by itself. To be fair the OP never mention "has to be CSS", the lack of a tag does not mean the answer is gone be what someone thinks as they come here for answers to what they don't know right. – Dejan.S Jun 28 '23 at 22:01
  • @Dejan.S Just FYI I didn't downvote you. I guess I would ask, if you, today, were faced with the same problem as the OP, would you go with the solution presented by this answer here, to solve it? – reformed Jun 29 '23 at 18:32
-2

insert the image and then backhand all you need is:

<style>
img {
  border-radius: 50%;
}
</style>

** the image code will be here automatically**

  • The OP wanted a "circle", your example is only rounding the edges, which if the image is rectangular, the result would be an oval. – stldoug Dec 02 '19 at 20:19