Round image mask with CSS

Asked

Viewed 20,457 times

5

Efeito

imagem original original image

I would like to know how to put the gray background and cut the outside of the player to stay just inside the circle.

  • As long as the image has a transparent background, just use the background-color and the border-radius. The Radius border roundes the edges, so just change until a circle as you want, and the gray background is just do with the background color. You can use the rgba to define transparency if the ash is to be transparent.

3 answers

9

you can create an img inside a div with "overflow:Hidden;"

.circle {
  background-color: #aaa;
  border-radius: 50%;
  width: 150px;
  height: 150px;
  overflow: hidden;
  position: relative;
}

.circle img {
  position: absolute;
  bottom: 0;
  width: 100%;
}
<div class="circle">
  <img src="https://i.stack.imgur.com/atUuf.png">
</div>

  • Tomás, you can improve the result by adding transform: scale(1.4) in .circle img.

  • It depends on the image

7

I’d do it this way:

img{
  background-color: #ddd;
  border-radius: 100%;
  height: 200px;
  object-fit: cover;
  width: 200px;  
}
<img src="https://i.stack.imgur.com/atUuf.png" alt="" />

This for testing, in development would put a class or ID, I find it simpler so.

2

CSS - just apply a class to a button with the image as the background, but it is advisable to apply an edge.

.tim{
    border: 2px solid #AD235E;
    border-radius: 150px;
    width: 200px;
    height: 190px; background-image: url(https://i.stack.imgur.com/atUuf.png);
    background-position: center;
    }
<button class="tim">
    </button>

It can be a div too, just put the background color in the CSS

.tim{
    //border: 2px solid #AD235E;
    border-radius: 150px;
    width: 200px;
    height: 190px; background-image: url(https://i.stack.imgur.com/atUuf.png);
    background-position: center;
    background-color: #C0C0C0;
    }
<div class="tim">
    </div>

  • The background turned gray from using button? A care that should be taken when using the button is in accessibility because, by default, a button has tabindex = 0 and is added to the navigation list by Tab. Being just an image maybe this is not interesting.

  • Can be a div too, just put the background color

Browser other questions tagged

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