Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define global CSS classes using JavaScript or jQuery?

Is there a way to set the CSS of global classes using JavaScript or jQuery? That is, append .my_class { foo:bar } to the <style/> tag of the page?

like image 743
MaiaVictor Avatar asked Mar 19 '13 07:03

MaiaVictor


People also ask

What is global class in CSS?

Classes are names that you can assign to a set of styles and apply them to elements. This helps when you want to apply the same style rules to multiple elements. You can search and add a class from the Styling tab of any element.

What is the use of CSS () method in jQuery?

jQuery css() Method The css() method sets or returns one or more style properties for the selected elements. When used to return properties: This method returns the specified CSS property value of the FIRST matched element.

Can we use CSS class in JavaScript?

The class name can be used by JavaScript to manipulate the specified element while CSS uses that class name to style that element. Hence, in this post, we will go through how to modify CSS classes in JavaScript but first let us set the environment by initializing elements in HTML and then styling that element in CSS.

What is a global CSS file?

Traditionally, websites are styled using global CSS files. Globally-scoped CSS rules are declared in external . css stylesheets, and CSS specificity and the Cascade determine how styles are applied.


1 Answers

Pure javascript -

var style=document.createElement('style');
style.type='text/css';
if(style.styleSheet){
    style.styleSheet.cssText='your css styles';
}else{
    style.appendChild(document.createTextNode('your css styles'));
}
document.getElementsByTagName('head')[0].appendChild(style);
like image 199
spaceman12 Avatar answered Oct 20 '22 00:10

spaceman12