31

I'd like to create a round image from a rectangular image using radius-border.

Is there simple way to achieve this with CSS without distorting the image AND ensuring a circle is perfectly round.

See failed attempts here:

http://jsfiddle.net/v8g3y0na/

.rounded-corners-2{
    border-radius: 100px;
    height: 150px;
    width: 150px;

Can this be done in only CSS.....?

1
  • 1
    Noting from your fiddle that you are trying to fit a rectangular image into a circle. Which bit of the image do you want to lose? Commented Aug 12, 2014 at 15:17

5 Answers 5

43

You do that by adding a parent div to your img and the code flows as follows

figure{
    width:150px;
    height:150px;
    border-radius:50%;
    overflow:hidden;
}

Updated Demo

Sign up to request clarification or add additional context in comments.

5 Comments

the issue here is that the image isn't centered in the circle.
Yep it works like that it actually crops the img to by inheriting the size of your parent div.
@web-tiki you can position your img follow this link jsfiddle.net/benjaminthomas/v8g3y0na/7
The technique you are using to center the image in the container won't work for all images. If they have different widths, it will fail.
Updated the url @mbomb007
30

Round image using CSS object-fit and border radius:

img{
  width:80px;
  height:80px;
  border-radius: 50%;
  object-fit: cover;
}
<img src="https://picsum.photos/id/1011/800/400">

img with background image

For older browsers, using the <img> tag

<img alt="My image" 
 src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
     style="background: url(https://picsum.photos/id/1011/300/180) 50% / cover; 
            border-radius: 50%;
            width:150px;">

The trick is to set a transparent px for the src (to prevent broken image icon) and do the best CSS3 and background-size has to offer (cover).

3 Comments

I've read through all the answers on this page and this is by far the smartest! Its also the most compact. And it uses no javascript! And it uses no css! Thanks @Roko C. Buljan! What image does it contain here? With which tool do you convert the graphic into base64 code? Thanks +1!
Thanks! Well, it actually uses CSS as you can see style=" which is ofc best put into a separate stylesheet :) To convert an image to base64 you can see here: stackoverflow.com/a/17711190/383904 .
@mbomb007 yep, it exists (perhaps yesterday the server was down) - Anyways, the code matters here - one can always use it's own image, but thanks for reporting - now you can delete your comment...
13

Is there simple way to achieve this with CSS without distorting the image AND ensuring a circle is perfectly round.

Yes, and you can also avoid using parent elements by just setting the image as the background. You can also position the image as you wish by using the background-position attribute.

Updated to address concerns about size, roundness, skewing and dynamically loaded content.

setTimeout(function() {
	$("#image").css("background-image", "url(https://placeholdit.imgix.net/~text?txtsize=33&txt=150%C3%97350&w=150&h=350)");
}, 3000);
#image {
    display: block;
    background-image: url("https://placeholdit.imgix.net/~text?txtsize=33&txt=350%C3%97150&w=350&h=150");
    border-radius: 200px;
    width: 200px;
    height: 200px;
    background-size: cover;
    background-position: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img id="image" />

9 Comments

yes this works if the width and height are same unless it will skew
@Benjamin How else is one supposed to make a circle if the width and height aren't the same? It's the same as your example but without using a parent div.
dude we can make a img a circle when we sizes a non-linear for instance if the image in 300X200 when we are abt to size it looks like skewed. I think you know wat im speaking abt. No offense here
The image is in the background, it doesn't matter what are the dimensions of the image, it's never distorted.
That's why I suggested putting the image in the background and not in the src attribute of the img element.
|
5

http://jsfiddle.net/o8fwpug5/37/

This is a slight update of a previous answer. I liked the other answer, but this is a bit more streamlined and gives a pixel based width for the wrapper. This way it is easier to see and change the dimensions for your own purposes.

HTML:

<div><img src="http://www.jpl.nasa.gov/spaceimages/images/mediumsize/PIA17011_ip.jpg" /></div>

CSS:

div{
    height:200px;
    width:200px;
    position:relative;    
    border-radius:100%;
    overflow:hidden;
}
img{
    position:absolute;
    left:-50%; right:-50%; top:0;
    margin:auto;
    height:100%; width:auto;
}

Comments

1

Put a DIV frame around the image: DEMO

<div class="rounded-corners">
   <img src="http://welovekaleycuoco.com/wp-content/uploads/2013/11/Kaley-Cuoco-Wallpapers-81.jpg" width="200"> 
</div>

div.rounded-corners {     
    height: 150px;
    width: 150px; 
    border-radius: 50%;
    overflow: hidden;
}

note: you don't need your img.rounded-corners style anymore

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.