Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I convert css clip rect() to clipPath?

I have some older code that uses CSS clip. It still works very well but since clip is deprecated I want to switch to using clipPath. The docs sounded like using inset() with the same top right bottom left properties would work, but it doesn’t produce the same result. Here’s an example:

<img src="https://i.ibb.co/0qG5Bzp/stone-wall.jpg" style="position: absolute; height: 554px; top: 0px; left: 0px; width: 2216px; clip: rect(0px, 50px, 100px, 25px); border: 1px solid red;">

<img src="https://i.ibb.co/0qG5Bzp/stone-wall.jpg" style="position: absolute; height: 554px; top: 125px; left: 0px; width: 2216px; clip-path: inset(0px 50px 100px 25px);  border: 1px solid red;">

https://jsfiddle.net/39742zoh/

I’ve searched Google for some time and while there’s a lot of good information on how to use clipPath, there’s not any information on converting your old clip code to the newer clipPath property.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How would I produce the same clip using clipPath?

Thanks in advance.

>Solution :

"top" and "bottom" for rect() consider the top border as a reference which is different with inset() because for the latter "bottom" will consider the bottom border as a reference. Same logic for "left" and "right"

You need to do the following to get the same result

<img src="https://i.ibb.co/0qG5Bzp/stone-wall.jpg" style="position: absolute; height: 554px; top: 0px; left: 0px; width: 2216px; clip: rect(0px, 50px, 100px, 25px); border: 1px solid red;">

<img src="https://i.ibb.co/0qG5Bzp/stone-wall.jpg" style="position: absolute; height: 554px; top: 125px; left: 0px; width: 2216px; clip-path: inset(0px calc(100% - 50px) calc(100% - 100px) 25px);  border: 1px solid red;">
clip: rect(top, right, bottom, left);
clip-path: inset(top calc(100% - right) calc(100% - bottom) left)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading