Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the colour of pseudo-elements in CSS through JQuery?

Tags:

jquery

css

I have made a heart-shaped button using CSS. Here is the JSFiddle link of my code.

#heart {
  height: 50px;
  width: 50px;
  position: relative;
}
#heart .outline:before,
#heart .outline:after {
  position: absolute;
  content: "";
  left: 28px;
  top: 1px;
  width: 15px;
  height: 27px;
  background: #d53423;
  -moz-border-radius: 50px 50px 0 0;
  border-radius: 85px 60px 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%;
}
#heart .outline:after {
  left: 13px;
  -webkit-transform: rotate(45deg);
  border-radius: 45px 60px 0 0;
  -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%;
}
$("#heart").on('submit',
function(e) {
  console.log('click heart support');
  e.preventDefault();
  $('#heart .outline:before').css('background', 'grey');
}
);
<form id="heart">
  <button id="like_btn" class="outline" type="submit"></button>
</form>

When I click the form button, I want this heart-shaped button to change its colour. However, this heart-shaped button is made from CSS pseudo-elements and hence, I can't easily change its colour.

Does anyone have a clue as to how can I manipulate CSS pseudo-elements (e.g. :before and :after) using JQuery?

like image 440
user824624 Avatar asked Apr 29 '13 13:04

user824624


People also ask

How do I change a specific color in CSS?

Simply add the appropriate CSS selector and define the color property with the value you want. For example, say you want to change the color of all paragraphs on your site to navy. Then you'd add p {color: #000080; } to the head section of your HTML file.

How do you style pseudo elements in JavaScript?

In general, if we want to change anything in pseudo elements through JavaScript, we do it in the following way: Create CSS classes on element, which will change pseudo elements' UI. Get the element using querySelector. Modify the classes using classList.

Can you select pseudo elements with JavaScript?

Since they're not part of the DOM, CSS pseudo-elements can't be selected and edited with JavaScript the same way as traditional elements on a page.

Can we specify inline styles for pseudo elements?

You can't specify inline styles for pseudo-elements. This is because pseudo-elements, like pseudo-classes (see my answer to this other question), are defined in CSS using selectors as abstractions of the document tree that can't be expressed in HTML.


2 Answers

I set :before :after background color inherit, then change the background color of their parent. http://jsfiddle.net/npMyy/3/

.outline {
    background: red;
}
#heart .outline:before, #heart .outline:after {
    background: inherit;
}
like image 117
Peiwen Avatar answered Oct 11 '22 20:10

Peiwen


This seems to work for me (Firefox 20)... not tested in anything else..

#heart .outline:active:before,
#heart .outline:active:after {
    background: #000;
}

Here, :active is a pseudo-class, and :before and :after are pseudo-elements. So this is perfectly acceptable according to the standard.

Pseudo-classes are allowed anywhere in selectors while pseudo-elements may only be appended after the last simple selector of the selector.

like image 23
naththedeveloper Avatar answered Oct 11 '22 19:10

naththedeveloper