Is it possible to use CSS to crop an image so it has a rounded border?
How do I do this in CSS?
Set your image as a background-image on a div and use this technique. I've used a solid red colour in my example. Here i used pseudo elements to create the curves on the top and bottom. Notice the top and bottom offset is half of that of the height of each pseudo element and the border radius is set to 50%.
Rounded borders in CSS are achieved through a property called border-radius
which you can think about like an actual circle or a quarter of a circle for each corner that has some radius and crops the selected element sharp edges to match the quarter circle curve. This is what happens when you use it with pixel values or fixed values if used with %
values such as border-radius: 50%;
it's totally a different story.
here is a good resource to learn more about border-radius
W3schools
or mozilla
The thing you want to achieve can be done like this:
CSS:
img.foo {
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
}
HTML:
<img class="foo" src="./foo.jpg" />
See border-radius.com for a generator.
Add a border radius border-radius:4px;
That crop image in css, use it as a `background.
Html:
<div class="cropped-image"></div>
CSS:
.cropped-image {
width: 100px; // crop by width
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
background: url("your image url") no-repeat center; // show image center
}
OR use clip
- http://www.w3.org/wiki/CSS/Properties/clip
img{
clip: rect(0px, 50px, 50px, 0px);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With