Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there anyway of grouping css classes together?

Quick question which is mostly explained by the code.

Is this possible in CSS or do I have to just implement all the classes in the html?

.class1{
    color:red;
}
.class2{
    text-decoration:underline;
}
.class3{

    class1;
    class2;
    border:1px solid green;
}
like image 798
Tristanisginger Avatar asked Jun 04 '14 11:06

Tristanisginger


People also ask

Can you group classes in CSS?

You can write the CSS styles to a specific element or group elements together with a specific style. Grouping CSS styles saves page space and allows you to apply particular styles to multiple tags.

Can CSS classes be nested?

Direct Nesting. A style rule can be directly nested within another style rule if its selector is nest-prefixed. To be nest-prefixed , a nesting selector must be the first simple selector in the first compound selector of the selector.

Can something have multiple classes CSS?

Attribute Values Specifies one or more class names for an 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.

How do I use grouping in CSS?

The CSS Grouping Selector The grouping selector selects all the HTML elements with the same style definitions. It will be better to group the selectors, to minimize the code. To group selectors, separate each selector with a comma.


2 Answers

You can use a CSS pre-processor like LESS or SASS for CSS "class inheritance".

Otherwise, your code sample is not possible with pure CSS. You can, however, use a comma to add similar styles to multiple elements:

.class1, .class2 {
    /* your styles */
}
like image 50
Josh Beam Avatar answered Nov 15 '22 12:11

Josh Beam


It is not possible to do it as such. If an element needs to have multiple classes, just specify it in the HTML :

<span class="class1 class2">Test</span>

Note that it is possible to define a selector for elements having multiple classes. .class1.class2 selector would target the element in the example here above, while not targetting elements that don't have both classes specified in HTML. This is usefull for example if you wish to override one of the properties of 1 of the classes only when they're used in combination.

like image 28
Laurent S. Avatar answered Nov 15 '22 10:11

Laurent S.