Using the css property border-radius
you can round the edges of an element. If you use border-radius: 50%
you will end up with a circular image.
So, if you had the following HTML:
<img src="some.url" class="circle">
and the following css:
.circle{ border-radius: 50%;}
You will end up with a rounded image. In order to have a circular image, the dimensions of the element that border-radius
is being applied to must be square. So, you will need to set a height
and width
property in the css as well.
.circle{
border-radius: 50%;
height: 150px;
width: 150px;
}
You should be aware if you are not using a square image and are applying the dimensions directly to the image, you could end up stretching or smushing the image.
To add the border to the image, you need to include the border
property in your css:
.circle{
border-radius: 50%;
height: 150px;
width: 150px;
border: red solid 2px;
}
Alternatively, you could just create a button
element and add the image as a background image to that element like this:
//html
<button class="circle"></button>
//css
.circle{
background-image:url("http://upload.wikimedia.org/wikipedia/commons/7/71/St._Bernard_puppy.jpg");
background-size: cover;
background-position: center center;
border-radius: 50%;
border: red solid 2px;
height: 150px;
width: 150px;
}
This will create a button
element with a background image from the url specified. The background-size
property will ensure that the image is always large enough to cover the button
. The background-position
will center the background image inside the button
so that the portion of the image that is shown on the button will be from the center of the background image.
This might be a better option for you since you can change the background-position
property to position a background image and keep the focus of that image in the center of the new circular element you have created.
In order to use the round image as a button you have a few ways you can go. The best would be to use pure javascript or use jQuery to select the rounded element and add a click
event handler.
You could also wrap the rounded element in an <a>
element and simply turn the rounded element into a link. Like this:
<a href="#"><button class="circle"></button></a>
In this case, you could remove the button if you wanted:
<a href="#" class="circle"></a>
However, make sure you then add display: block;
to your css for the .circle
class:
.circle{
display: block;
background-image:url("http://upload.wikimedia.org/wikipedia/commons/7/71/St._Bernard_puppy.jpg");
background-size: cover;
background-position: center center;
border-radius:50%;
width: 150px;
height: 150px;
border: red solid 2px;
}