How to turn a rectangular image into a circle (without distorting it)

Asked

Viewed 4,799 times

5

As shown in the image below, I can make the picture rectangular turn round, but it gets distorted:

inserir a descrição da imagem aqui

.posts .posts-item img {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 0 auto;
  margin-bottom: 0.5rem;
  background-position: center center;
}
.posts.round .round-container img {
  width: auto;
  height: 100%;
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
}
figure img {
  border-radius: 100%;
  width: 12rem;
  height: 12rem;
}
<a href="" class="posts-item">
  <div class="title">Conheça a lista dos melhores cafés</div>
  <figure>
    <div class="round-container"><img src="http://vidafit.com.br/wp-content/uploads/2014/11/cafe-coracao-776x517.jpg" alt=""></div>
    <figcaption>Lorem ipsum dolor sit amet, onsectetur dispiscing</figcaption>
  </figure>
</a>

Does anyone know how to solve this for any image I put?

  • See if it helps you: http://codepen.io/anon/pen/jrBqAp

  • It would be like this, but the image would have to be of a specific size and for me it doesn’t fit yet (as this is big, just appeared the spoon in the circle).

4 answers

3

There are several ways to do it, like virtually everything that involves html and css.

The problem in your case is that your image does not have the same height and width value, but you force it to have when defining the properties height and width. Consequently the image will be "stretched". Three possible solutions are:

  • edit the image so that the height and width have the same value.
  • create an element wrapper containing the properties that shape the element (height, width, border) and make the internal element (an image) 100% of the parent element’s height.

    .img-wrapper {
      height: 12rem;
      width : 12rem;
      
      border-radius: 50%;
      overflow: hidden
    }
    
    .img-wrapper > img {
      height: 100%
    }
    <div class='img-wrapper'>
      <img src='http://vidafit.com.br/wp-content/uploads/2014/11/cafe-coracao-776x517.jpg'>
    </div>

  • set the image url in the property background and use background-size: cover to make the background image "cover" the whole space of the element.

    .image {
      height: 12rem;
      width : 12rem;
      
      border-radius: 50%;
      background-image: url(http://vidafit.com.br/wp-content/uploads/2014/11/cafe-coracao-776x517.jpg);
      background-size: cover
    }
    <div class='image'></div>

  • It was very close to what I wanted, but the image was not centered :/ I will post the code that worked for me, but thanks anyway

2

The way I found was to center the image inside a rectangular div, place it inside a square div, pull half the leftover left, and finally hide the overflow and round the father div of all:

.posts .posts-item img {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 0 auto;
  margin-bottom: 0.5rem;
  background-position: center center;
}
figure img {
  width: auto;
  height: 12rem;
}
.round-container {
  overflow: hidden;
  width: 12rem;
  height: 12rem;
  border-radius: 50%;
}
.c {
  position: relative;
  width: 24rem;
  left: -6rem;
  text-align: center;
}
<a href="" class="posts-item">
  <div class="title">Conheça a lista dos melhores cafés</div>
  <figure>
    <div class="round-container">
      <div class="c">
        <img src="http://vidafit.com.br/wp-content/uploads/2014/11/cafe-coracao-776x517.jpg" alt="">
      </div>
    </div>
    <figcaption>Lorem ipsum dolor sit amet, onsectetur dispiscing</figcaption>
  </figure>
</a>

  • Perfect! Hahaha This is exactly how I needed it! I’m going to post my code here, which got a little more optimized (16 lines). I used Transform, which practically gave the same result. Thank you ;)

2


I placed the image inside a container, adding a fixed size and the border-radius. Then I adjusted the image itself inside that container with the transform.

.round-container {
    width: 17rem;
    height: 17rem;
    align-self: center;
    border-radius: 100%;
    margin: 2.3rem auto 2.5rem auto;
    overflow: hidden;
    position: relative;
}
.round-container img {
    width: initial;
    height: inherit;
    max-width: none;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
}
<h1><span>ice dream</span></h1>

<figure>
  <div class="round-container">
  <img src="http://static.tumblr.com/bdf78d98740c3a8962814afcaa5ea1c6/3zeuwsd/PnYmw7l78/tumblr_static_icecream.jpg">
  </div>
  <figcaption>
    Lorem ipsum dolor sit amet, onsectetur dispiscing
  </figcaption>
</figure>

The same code on codepen.

  • Maiz Thanks for the code, gave certi :). But I can not leave the image responsive and needed, Would know how? Thanks

  • Well, for me the image is already "responsive" haha How exactly would be responsive for you?

  • thank you for the answer! It would be in keeping with the screen size, pc, cell phone, tablet, it fit to 100% on all devices. I tried to trade widht and height for % but it didn’t work!

1

You can use the property clip-path, but see that she has a support not so good.

img {
  width: 350px;
  height: 300px;
}

.circle {
  clip-path: circle(150px at center);
  -webkit-clip-path: circle(150px at center);
}
<img src="http://vidafit.com.br/wp-content/uploads/2014/11/cafe-coracao-776x517.jpg" alt="" class="circle">

Note that I’m prefixing the property, which is another sign that she’s not that adopted. You should obviously adjust the dimensions of the image and the clipping for your needs.

Here has a generator of clips well cool, with several formats.

  • 1

    The negative colleague could explain exactly what is wrong with my answer. Who knows so I can improve it to the liking of all.

  • 1

    I was looking for several clips, since my Transform was no longer working, but I managed to fix it now! I will post the reply soon. Thank you ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.