1

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...

jbutler483
  • 24,074
  • 9
  • 92
  • 145
Aditya
  • 4,453
  • 3
  • 28
  • 39
  • it seems that this is just a small adjustment issue, for example, you can fix the bottom left part by changing `left: 12.5px;` to `left: 13.5px;` – Banana Jul 01 '14 at 12:31
  • 3
    Look at [CSS3shapes](http://www.css3shapes.com/#heart). – Vucko Jul 01 '14 at 12:33
  • also, keep in mind that if you make a 1px border, you need to take it into consideration when you adjust the width as you will need the heart to be 1px wider – Banana Jul 01 '14 at 12:37
  • and of course, even the filled heart needs a border, so the size wont appear different. heres a slightly adjusted version of your code: http://jsfiddle.net/TheBanana/M9cHA/3/ – Banana Jul 01 '14 at 12:40

4 Answers4

4

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>
G.L.P
  • 7,119
  • 5
  • 25
  • 41
1

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.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Thanks, you are right `!important` should be avoided , but there is some issue on the curved part(top-left) of the heart , do you know how that can be dealt with ? – Aditya Jul 01 '14 at 15:30
1

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>
jbutler483
  • 24,074
  • 9
  • 92
  • 145
0

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%;
}
Pradeep Pansari
  • 1,287
  • 6
  • 10