2

Imagine a parent div with a rounded border-radius. Is there a way to fit an img inside the div so that it also conforms to the rounded border?

.parent-div {
  background-color: gray;
  border-radius: 20px;
}

.image {
  width: 100%
}
<div class="parent-div">
  <img class="image" src="/a-cool-image">
  <p>Some text</p>
</div>

This just results in an image with square corners while the div has rounded corners on the bottom. The only workaround I know is to have the image have the same border-radius property but only on the top two corners. I find that to be an annoying workaround.

Dulani Maheshi
  • 1,070
  • 1
  • 10
  • 30
sweenejp
  • 61
  • 1
  • 6

3 Answers3

7

Trying looking into a using overflow https://www.w3schools.com/cssref/pr_pos_overflow.asp

.parent-div {
  background-color: gray;
  border-radius: 20px;

  overflow: hidden;
}

Here is a working example to showcase what I'm suggesting: https://jsfiddle.net/4mbzpfu2/3/

Dylan Anlezark
  • 1,256
  • 2
  • 12
  • 27
  • 1
    Thanks! I had been accidentally adding the overflow property to the child element instead of the parent element. – sweenejp Apr 02 '21 at 02:39
0

You can try this.

.parent-div img {
    -webkit-border-radius: 20px;
    -moz-border-radius: 20px;
    -ms-border-radius: 20px;
    -o-border-radius: 20px;
    border-radius: 20px;
}
.image {
  width: 100%
}
<div class="parent-div">
  <img class="image" src="https://www.w3schools.com/howto/img_avatar.png">
  <p>Some text</p>
</div>
Rakesh Gupta
  • 3,507
  • 3
  • 18
  • 24
0

Another workaround might be using CSS. You can set the radius property for individual corners. This is an example with rounded corners at the top, and square corners at the bottom:

img.yourclass { 
-webkit-border-top-left-radius: 20px;
-webkit-border-top-right-radius: 20px;
-moz-border-radius-topleft: 20px;
-moz-border-radius-topright: 20px;
border-top-left-radius: 20px;
border-top-right-radius: 20px;
}

Check out this website to generate the CSS code: https://border-radius.com/

mosmos
  • 13
  • 1
  • 6