Aditya
Aditya

Reputation: 4463

Get heart-shaped border using CSS?

I have this code that is used to create a colored heart (which has a blue background) and an uncolored heart(white colored with blue border ) using CSS:

#favourite_user {
    position: relative;
    width: 25px;
    height: 22.5px;
    margin: 30px;
    cursor:pointer;
}
#favourite_user.favourited:before,#favourite_user.favourited:after {
    background: #3498db;
}
#favourite_user.unfavourited:before,#favourite_user.unfavourited:after {
    background: #fff !important;
    border: 1px solid #3498db;
}
#favourite_user:before,
#favourite_user:after {
    position: absolute;
    content: "";
    left: 12.5px;
    top: 0;
    width: 12.5px;
    height: 20px;
    -moz-border-radius: 50px 50px 0 0;
    border-radius: 50px 50px 0 0;
    -webkit-transform: rotate(-45deg);
       -moz-transform: rotate(-45deg);
        -ms-transform: rotate(-45deg);
         -o-transform: rotate(-45deg);
            transform: rotate(-45deg);
    -webkit-transform-origin: 0 100%;
       -moz-transform-origin: 0 100%;
        -ms-transform-origin: 0 100%;
         -o-transform-origin: 0 100%;
            transform-origin: 0 100%;
}
#favourite_user:after {
    left: 0;
    border-left:0px !important;
    -webkit-transform: rotate(45deg);
       -moz-transform: rotate(45deg);
        -ms-transform: rotate(45deg);
         -o-transform: rotate(45deg);
            transform: rotate(45deg);
    -webkit-transform-origin: 100% 100%;
       -moz-transform-origin: 100% 100%;
        -ms-transform-origin: 100% 100%;
         -o-transform-origin: 100% 100%;
            transform-origin :100% 100%;
}

On clicking the heart i am toggling classes .favourited and .unfavourited , one which has a bluish background while the other is supposed to have just a blue border with a white background . But i am not able to achieve this , a part of this border is not coming out well using psuedo elements :before and :after.

Here is the Fiddle

Any pointers regarding the solution would be greatly appreciated...Thanks in advance...

Upvotes: 1

Views: 9237

Answers (4)

jbutler483
jbutler483

Reputation: 24529

You could use something like:

$('div').click(function() {
  $(this).toggleClass("active");

});
div {
  height: 200px;
  width: 200px;
  background: tomato;
  position: relative;
  margin: 100px auto;
  border-bottom: 5px solid black;
  border-right: 5px solid black;
  transform: rotate(45deg);
}
div:before,
div:after {
  content: "";
  position: absolute;
  height: 100%;
  width: 100%;
  border-radius: 50%;
  background: tomato;
  border: 5px solid transparent;
  transform: rotate(-45deg);
  z-index: -2;
}
div:before {
  top: -5px;
  left: calc(-50% - 5px);
  border-top-color: black;
  border-left-color: black;
}
div:after {
  top: calc(-50% - 5px);
  left: -5px;
  border-top-color: black;
  border-right-color: black;
}
.active {
  border-color: blue;
  background: transparent;
}
.active:before {
  border-top-color: blue;
  border-left-color: blue;
  background: transparent;
}
.active:after {
  border-top-color: blue;
  border-right-color: blue;
  background: transparent;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div></div>

Upvotes: 2

Andrea Ligios
Andrea Ligios

Reputation: 50193

To remove the lower left artifact, you need to add

#favourite_user:after {
    border-bottom:0px !important; /* ADDED */

Running demo

Also need that !important should be avoided, because it forces a recalculation of all the rules each time the browser engine founds it in a rule;

Instead, you can higher the CSS specificity of your rules to give them the correct importance when applied.

Upvotes: 1

Pradeep Pansari
Pradeep Pansari

Reputation: 1297

You can try this:

You can also use font awesome for that and color change also as a font color

Fontawesome: http://fortawesome.github.io/Font-Awesome/icon/heart/

Demo

#favourite_user:after {
 border:solid 1px #fff;
 position: absolute;
 content: "";
 left: 12.5px;
 top: 0;
 width: 12.5px;
 height: 20px;
 -moz-border-radius: 50px 50px 0 0;
 border-radius: 50px 50px 0 0;
 -webkit-transform: rotate(-45deg);
   -moz-transform: rotate(-45deg);
    -ms-transform: rotate(-45deg);
     -o-transform: rotate(-45deg);
        transform: rotate(-45deg);
 -webkit-transform-origin: 0 100%;
   -moz-transform-origin: 0 100%;
    -ms-transform-origin: 0 100%;
     -o-transform-origin: 0 100%;
        transform-origin: 0 100%;
}

Upvotes: 0

G.L.P
G.L.P

Reputation: 7207

try to use font awesome LINK

You can use "fa-heart-o" class instead of using this much code

HTML:

<i class="fa fa-heart-o"></i>

Upvotes: 4

Related Questions