Creating a heart shape is not a problem. A few examples can be found here:
How to create a heart shape using CSS?
The problem with these solutions is if you want the heart shape to be clickable, the whole HTML element is clickable (a DIV tag or an IMG tag) and not only the heart shape.
I found a solution to create a clickable heart here: https://codepen.io/bailey_jones/pen/jONaONK
Code snippet:
#heart {
position: relative;
width: 100px;
height: 90px;
}
#heart:before,
#heart:after {
position: absolute;
content: "";
left: 50px;
top: 0;
width: 50px;
height: 80px;
background: red;
border-radius: 50px 50px 0 0;
transform: rotate(-45deg);
transform-origin: 0 100%;
}
#heart:after {
left: 0;
transform: rotate(45deg);
transform-origin: 100% 100%;
}
<a id="heart" href="#"></a>
Here position: absolute is used. I want to use position: relative and the size of the heart to be relative to the parent container and I want it to be centered in the parent container. which should not be difficult, but my heart shape is malformed.
Code snippet:
body {
width: 90vw;
height: 90vh;
}
body,
#container {
display: flex;
align-items: center;
justify-content: center;
}
#container {
width: 400px;
height: 400px;
background: yellow;
}
#heart {
position: relative;
--width: 100%; /* was 100px, is 400px */
--height: 90%; /* was 90px, is 360px */
--x: calc(.5 * var(--width)); /* was 50px, is 200px */
--y: calc(8/9 * var(--height)); /* was 80px, is 320px */
width: var(--width); /* was 100px, is 400px */
height: var(--height); /* was 90px, is 360px */
border: 1px dotted blue;
}
#heart:before,
#heart:after {
position: absolute;
content: "";
left: var(--x);
top: calc(var(--height) - var(--y)); /* Does this make sense? */
width: var(--x);
height: var(--y);
background: red;
border-radius: var(--x) var(--x) 0 0;
transform: rotate(-45deg);
transform-origin: 0 100%;
}
#heart:after {
left: 0;
transform: rotate(45deg);
transform-origin: 100% 100%;
}
<div id="container">
<a id="heart" href="#"></a>
</div>
The worst of all, now not only the heart shape is clickable. Why and how to fix this?
2
Answers
My heart shape works well with some adjustment. There is a little space at the top that is clickable but I don’t think it’s a big deal.
A more complex clip-path can eliminate that space:
Use an SVG which contains the link you want.