Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we include common css class in another css class?

I am a CSS newbie. I am just wondering, is that possible to include one common class into another class?

for example,

.center {align: center}; .content { include .center here}; 

I came across css framework - Blueprint. We need to put the position information into HTML, e.g.

<div class="span-4"><div class="span-24 last"> 

As such, we will place the positioning attribute inside html, instead of css. If we change the layout, we need to change html, instead of css.

That's the reason I ask this question. If I can include .span-4 into my own css, i won't have to specify it in my html tag.

like image 265
janetsmith Avatar asked Oct 16 '09 04:10

janetsmith


People also ask

Can you put a class in a class CSS?

You can't declare a CSS class an then extend it with another CSS class. However, you can apply more than a single class to an tag in your markup ... in which case there is a sophisticated set of rules that determine which actual styles will get applied by the browser.

Can one CSS class refer to another?

You can just add a comma-separated list of classes in . radius , like . radius, . another, .

How do you call a CSS class in CSS?

To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class. To do this, start with the element name, then write the period (.)

Can an element have two 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.


2 Answers

Bizarrely, even though CSS talks about inheritance, classes can't "inherit" in this way. The best you can really do is this:

.center, .content { align: center; } .content { /* ... */ } 

Also I'd strongly suggest you not do "naked" class selectors like this. Use ID or tag in addition to class where possible:

div.center, div.content { align: center; } div.content { /* ... */ } 

I say this because if you do your selectors as broad as possible it ends up becoming unmanageable (in my experience) once you get large stylesheets. You end up with unintended selectors interacting with each other to the point where you create a new class (like .center2) because changing the original will affect all sorts of things you don't want.

like image 105
cletus Avatar answered Sep 19 '22 20:09

cletus


In standard CSS, it's not possible to do this, though it would be nice.

For something like that you'd need to use SASS or similar, which "compiles" to CSS.

like image 20
Deeksy Avatar answered Sep 20 '22 20:09

Deeksy