Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS - Multiple vs Single class on an element

Tags:

Is there any performance gain in using single css class vs multiples on an element?

Ex:-

<div class="single-class"></div>

vs

<div class="no-padding no-margin some-class some-other-class"></div>
like image 898
Akhil Uddemarri Avatar asked Apr 06 '11 15:04

Akhil Uddemarri


People also ask

How do I use multiple CSS classes on a single element?

To specify multiple classes, separate the class names with a space, e.g. <span class="left important">. This allows you to combine several CSS classes for one HTML element.

Can an element have multiple CSS classes?

An element is usually only assigned one class. The corresponding CSS for that particular class defines the appearance properties for that class. However, we can also assign multiple classes to the same element in CSS.

Can you have multiple classes on an element?

HTML elements can be assigned multiple classes by listing the classes in the class attribute, with a blank space to separate them.

Can you have multiple classes in one div?

Absolutely, divs can have more than one class and with some Bootstrap components you'll often need to have multiple classes for them to function as you want them to. Applying multiple classes of course is possible outside of bootstrap as well. All you have to do is separate each class with a space.

What is one or more classes in CSS?

One or More Classes in CSS? In most cases, you assign a single class attribute to an element, but you actually aren't limited to just one class they way you are with IDs.

How to add multiple classes to the same element in CSS?

Reference it in css like so: To add multiple class in the same element you can use the following format: Remember that you can apply multiple classes to an element by separating each class with a space within its class attribute. For example: If you have 2 classes i.e. .indent and .font, class="indent font" works.

What is the advantage of multiple classes in HTML?

Layering several classes can make it easier to add special effects to elements without having to create a whole new style for that element. For example, to float elements to the left or right, you might write two classes: in them. Then, whenever you had an element you need to float left, you would simply add the class "left" to its class list.

How to call two classes from one class in HTML?

If you have 2 classes i.e. .indent and .font, class="indent font" works. You dont have to have a .indent.font{} in css. You can have the classes separate in css and still call both just using the class="class1 class2" in the html. You just need a space between one or more class names.


2 Answers

If you have an even moderately complex stylesheet and know what you're doing, using multiple classes has the benefit of taking full advantage of the cascade, which in the end most likely means that your css files will be smaller as you won't be duplicating code. So, in this sense, it actually has a positive impact on performance (dependent on the complexity of your css).

I'm trying to imagine what the jQuery UI stylesheet would look like if it used a one-class-per-element approach. I imagine it would double in size.

Of course like everyone said, the impact is so small you won't notice. Continue your use of multiple classes and embrace the cascade!


FWIW, I really don't like class names like no-padding, float-left and ui-corner-all. Though they make sense to the css author, a semantic approach combined with multiple elements per declaration (to avoid duplicating rules) is almost always preferred. Using the first method, your css will make no sense if you decide that no-padding actually needs 1px padding, and that a.classname.float-left actually needs to float right.

like image 75
Wesley Murch Avatar answered Sep 19 '22 16:09

Wesley Murch


Technically, yes fewer classes mean less data transfer for HTML. As far as CSS is concerned, each element needs to be checked against each rule, so fewer classes may mean less matching depending on the rules you have written.

The difference is so small that it's not easily noticeable. You may notice the performance difference if you're populating a page with tens of thousands of elements, but that would be a poor design choice anyway.

One concern I have is with the example you've provided. Your classes no-padding, no-margin are descriptive of styles which are a bad idea. If all your some-class elements also have the no-margin class, then margin: 0 should be part of the some-class rule.

Don't add classes for styles, add styles for classes.

like image 20
zzzzBov Avatar answered Sep 20 '22 16:09

zzzzBov