How to display just a portion of an image in HTML and CSS?

Spread the love

Sometimes, we want to display just a portion of an image in HTML and CSS.

In this article, we’ll look at how to display just a portion of an image in HTML and CSS.

How to display just a portion of an image in HTML and CSS?

To display just a portion of an image in HTML and CSS, we set the clip-path property.

For instance, we write

<div class="container">
  <img id="control" src="http://lorempixel.com/150/150/people/1" />
</div>
<div class="container">
  <img id="rectangular" src="http://lorempixel.com/150/150/people/1" />
</div>
<div class="container">
  <img id="circle" src="http://lorempixel.com/150/150/people/1" />
</div>
<div class="container">
  <img id="ellipse" src="http://lorempixel.com/150/150/people/1" />
</div>
<div class="container">
  <img id="polygon" src="http://lorempixel.com/150/150/people/1" />
</div>

to add some images.

Then we write

div.container {
  display: inline-block;
}
#rectangular {
  -webkit-clip-path: inset(30px 10px 30px 10px);
  clip-path: inset(30px 10px 30px 10px);
}
#circle {
  -webkit-clip-path: circle(75px at 50% 50%);
  clip-path: circle(75px at 50% 50%);
}
#ellipse {
  -webkit-clip-path: ellipse(75px 50px at 50% 50%);
  clip-path: ellipse(75px 50px at 50% 50%);
}
#polygon {
  -webkit-clip-path: polygon(50% 0, 100% 38%, 81% 100%, 19% 100%, 0 38%);
  clip-path: polygon(50% 0, 100% 38%, 81% 100%, 19% 100%, 0 38%);
}

to set the clip-path for each image.

We can set it to show parts of the image what the shape of the image set by the clip-path value.

Conclusion

To display just a portion of an image in HTML and CSS, we set the clip-path property.

Leave a Reply

Your email address will not be published. Required fields are marked *