I am creating a round avatar in the user's profile and intend to set the user's image in the round avatar container.
If the image is a square there will not be an issue
However, I was not able to constraint an image which is not a square image for example for this non-square image
I will get this result
This is the CSS code I am using
.avatar_container {
display: inline-block;
width: 25%;
max-width: 110px;
overflow: hidden;
}
.avatar_container img {
border-radius: 50%;
}
What can I do to always maintain a round avatar? And that the image in it won't be distorted? overflow should be hidden
Answer: Use the CSS max-width Property You can simply use the CSS max-width property to auto-resize a large image so that it can fit into a smaller width <div> container while maintaining its aspect ratio.
Complete HTML/CSS Course 2022 To display an image inside SVG circle, use the <circle> element and set the clipping path. The <clipPath> element is used to define a clipping path. Image in SVG is set using the <image> element.
The main CSS property responsible for the circular shape is the border-radius property. Setting the radius of the corners to 50% of the width/height results in a circle.
This post is a bit old but another option to achieve a circular image from a portrait or landscape image is to use object-fit (which is now supported, at least for img tags in all browsers but IE).
<div class="mask">
<img src="http://i.stack.imgur.com/MFao1.png" />
</div>
img{
width: 200px;
height: 200px;
object-fit: cover;
border-radius: 50%;
}
Dispite @alexvance already gave that useful link, I will add the entire code to ensure it will still remain after link becomes broken…
<div class="thumbnail">
<img src="landscape-img.jpg" alt="Image" />
</div>
<div class="thumbnail">
<img src="portrait-img.jpg" class="portrait" alt="Image" />
</div>
.thumbnail {
position: relative;
width: 200px;
height: 200px;
overflow: hidden;
}
.thumbnail img {
position: absolute;
left: 50%;
top: 50%;
height: 100%;
width: auto;
-webkit-transform: translate(-50%,-50%);
-ms-transform: translate(-50%,-50%);
transform: translate(-50%,-50%);
}
.thumbnail img.portrait {
width: 100%;
height: auto;
}
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