Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning classes to elements through CSS

Tags:

html

css

People also ask

Can I add a class to an element using CSS?

With CSS Hero you can easily add classes to elements without needing to touch any of your theme files or adding weird code to your contents. Plus you can control all your classes right from the CSS Hero interface.

Can you assign a class in CSS?

No, you can't. You can however use Javascript (jQuery recommended) to achieve this effect.

How do I add a CSS class to an element in CSS?

If you want to use a class, use a full stop (.) followed by the class name in a style block. Next, use a bracket called a declaration block that contains the property to stylize the element, such as text color or text size.

How do you assign a class to an element?

HTML Classes and IDs Giving an element a classUse the class attribute to assign a class to an element. To assign multiple classes to an element, separate the class names with spaces.


No, that isn't possible with pure CSS.

Only with JavaScript:

// jQuery
$("h5").addClass("ui-corners-all");

// Pure JavaScript
var elements = document.getElementsByTagName("h5");
for (var i=0; i<elements.length; i++)
{
  var el = elements[i];      
  el.setAttribute( "class", el.getAttribute("class") + " ui-corners-all" );
}

There is no way to assign this value to those elements in pure CSS.

You would need to do:

#sidebar h5
{

}

Then copy all styles from ui-corners-all class into this.

Or alternatively, change your ui-corners-all CSS to:

.ui-corners-all, #sidebar h5
{    

}

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!