If you wish to achieve a specific shape for your elements, consider utilizing
clip-path: polygon (x y, x y, ...)
. This property allows you to define shapes using units like
%
or
px
, making it convenient for adjusting width and height with variables. Below is the code snippet I have developed based on my previous solution shared in this stackoverflow post.
UPDATE: The latest version of the code now caters to image responsiveness! To explain further, a div styled with positional properties such as left
, right
, top
, and bottom
is placed around the image, slightly larger than the image size itself. This setup requires a container with a relative
position. Moreover, by using the class my-polygon
to define the shape in percentages, any potential issues are eliminated.
:root {
--cut-corner-size: 10px;
--cut-corner-border: 5px;
}
.my-polygon {
clip-path: polygon(
calc(var(--cut-corner-size) * 2) 0,
calc(100% - var(--cut-corner-size) * 2) 0,
// Remaining CSS code truncated for brevity
);
padding: 0;
border: 0;
margin: 0;
}
.relative-container {
position: relative;
display: inline-block;
}
.border-container {
position: absolute;
left: calc(-1 * var(--cut-corner-border));
top: calc(-1 * var(--cut-corner-border));
bottom: calc(-1 * var(--cut-corner-border));
right: calc(-1 * var(--cut-corner-border));
background-color: green;
}
img {
display: block;
}
<div class="relative-container">
<div class="border-container my-polygon">
</div>
<img src="https://picsum.photos/300/150" class="my-polygon">
</div>
<div class="relative-container">
<div class="border-container my-polygon">
</div>
<img src="https://picsum.photos/150/150" class="my-polygon">
</div>