1

How to draw a heart using only the css properties and html?
I have came across few codes to draw it but couldn't understand why do they use the before and after properties

css code

.heart {  
     background-color: red;  
      height: 30px;  
      transform: rotate(-45deg);  
      width: 30px;  
}  
.heart:before,  
.heart:after {    
      content: "";  
      background-color: red;  
      border-radius: 50%;  
      height: 30px;  
      position: absolute;  
      width: 30px;  
}  
.heart:before {    
      top: -15px;  
      left: 0;  
}

How the before and after properties work?
Would be helpful if someone could come up with alternate approach :)

Edits:- I just wanted to know why do we use the before and after properties and how it helps in creating this heart shape.
Also I'm looking for some easy and simple code(using only css) to obtain this shape.

3251_ali
  • 147
  • 1
  • 10

3 Answers3

4

You can do this with pseudo elements

To learn more about pseudo elements

body{
  padding-left:50px;
}

div {
  position: relative;
  width: 100px;
  height: 175px;
  background-color: green;
  -webkit-border-radius: 50px 50px 0 0;
  -moz-border-radius: 50px 50px 0 0;
  border-radius: 50px 50px 0 0;
  -webkit-transform: rotate(315deg);
  -moz-transform: rotate(315deg);
  -ms-transform: rotate(315deg);
  -o-transform: rotate(315deg);
  transform: rotate(315deg);
}

div:before {
  position: absolute;
  width: 175px;
  height: 100px;
  left: 0;
  bottom: 0;
  content: "";
  background-color: green;
  -webkit-border-radius: 50px 50px 0 0;
  -moz-border-radius: 50px 50px 0 0;
  border-radius: 0 50px 50px 0;
}
<div></div>

Hope this helps :)

vaishali kapadia
  • 934
  • 11
  • 22
2

Here is an idea with only background and less of code:

.heart {
  width:200px;
  height:200px;
  background:
   radial-gradient(circle at 50% 83%, red 29%, transparent 30%) -40px -100px/100% 100%,
   radial-gradient(circle at 50% 83%, red 29%, transparent 30%) 40px -100px/100% 100%,
   linear-gradient(to bottom left,red 43%,transparent 43%) bottom left/50% 50%,
   linear-gradient(to bottom right,red 43%,transparent 43%) bottom right/50% 50%;
  background-repeat:no-repeat;
}
<div class="heart">
</div>

You can integrate CSS variable to control the dimensions:

.heart {
  --d:200px;
  width:var(--d);
  height:var(--d);
  background:
   radial-gradient(circle at 50% 83%, red 29%, transparent 30%) calc(-1 * var(--d)/5) calc(-1 * var(--d)/2)/100% 100%,
   radial-gradient(circle at 50% 83%, red 29%, transparent 30%) calc(var(--d)/5) calc(-1 * var(--d)/2)/100% 100%,
   linear-gradient(to bottom left,red 43%,transparent 43%) bottom left/50% 50%,
   linear-gradient(to bottom right,red 43%,transparent 43%) bottom right/50% 50%;
  background-repeat:no-repeat;
  display:inline-block;
}
<div class="heart">
</div>
<div class="heart" style="--d:100px;">
</div>
<div class="heart" style="--d:50px;">
</div>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
0

The before and after tags are so called pseudo-elements. Here's a helpful resource with more information on them. Basically it creates an element, that isn't there in the actual DOM (the HTML structure) and allows you to apply CSS styling to it.

In this case the before and after are two circles.

Here's how a single pseudo element looks like in your case:

body {
  padding: 15px
}

.heart {
  background: green;
  width: 50px;
  height: 50px;
}

.heart:after {
  content: "";
  background-color: red;
  border-radius: 50%;
  height: 30px;
  position: absolute;
  width: 30px;
}

.heart:after {
  left: 15px;
  top: 0px;
}
<div class="heart"></div>

And here's the complete heart itself:

body {
  padding: 15px;
}

.heart {
  background-color: green;
  height: 30px;
  transform: rotate(-45deg);
  width: 30px;
}

.heart:before,
.heart:after {
  content: "";
  background-color: red;
  border-radius: 50%;
  height: 30px;
  position: absolute;
  width: 30px;
}

.heart:before {
  left: 0;
  top: -15px;
}

.heart:after {
  left: 15px;
  top: 0;
}
<div class="heart"></div>

If you rotate the main block you get a rotated square. The two pseudo elements being circles create a heart with the div itself, if the position of each element is fitting. The reason your heart wasn't working properly is because the pseudo elements weren't placed correctly. Pushing the after element to the left fixes this problem.

Maharkus
  • 2,841
  • 21
  • 35