Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to maintain dimension of non-square image in a round image container

Tags:

css

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

enter image description here

However, I was not able to constraint an image which is not a square image for example for this non-square image

enter image description here

I will get this result

enter image description here

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

like image 855
Zhen Avatar asked Jan 05 '13 00:01

Zhen


People also ask

How do I stretch an image to fit in a div?

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.

How do I put an image in a circle in HTML?

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.

How do you make an image a circle in CSS?

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.


2 Answers

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).

HTML

<div class="mask">
    <img src="http://i.stack.imgur.com/MFao1.png" />
</div>​

CSS

img{
    width: 200px;
    height: 200px;
    object-fit: cover;
    border-radius: 50%;
}
like image 86
M. Kloz Avatar answered Sep 16 '22 15:09

M. Kloz


Dispite @alexvance already gave that useful link, I will add the entire code to ensure it will still remain after link becomes broken…

Example

enter image description here

HTML

<div class="thumbnail">
  <img src="landscape-img.jpg" alt="Image" />
</div>
<div class="thumbnail">
  <img src="portrait-img.jpg" class="portrait" alt="Image" />
</div>

CSS

.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;
}
like image 25
Beat Avatar answered Sep 16 '22 15:09

Beat