Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply style to multiple html elements of the same class with css or sass

Tags:

css

sass

I want to apply a style to all <p> and <input> elements of the numeric class using css.

Is it possible to consolidate this so that I only write "numeric" once?

p.numeric,input.numeric {
    float: right;
}

I'm also using sass, so if it's not possible in CSS is it possible with the sass additions?

like image 626
spike Avatar asked Nov 16 '11 16:11

spike


People also ask

Can you use the same CSS class on multiple elements?

When you group CSS selectors, you apply the same styles to several different elements without repeating the styles in your stylesheet. Instead of having two, three, or more CSS rules that do the same thing (set the color of something to red, for example), you use a single CSS rule that accomplishes the same thing.

Can you apply the same class attribute to multiple HTML elements?

The HTML class attribute is used to specify a class for an HTML element. Multiple HTML elements can share the same class.

Which selector is used to apply a style to multiple elements?

The CSS grouping selector is used to select multiple elements and style them together. This reduces the code and extra effort to declare common styles for each element. To group selectors, each selector is separated by a space.

How do I apply multiple CSS classes?

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.


2 Answers

yes it is possible:

p, input {
    &.numeric {
        float: right;
    }
}

The '&' is necceassry to connect with p/input. Without the result will be p .numeric {...}

like image 125
Rito Avatar answered Oct 05 '22 23:10

Rito


You could simply do .numeric, but then it would apply to everything with a class of numeric. If you only want it to apply to paragraphs and inputs then what you're doing is the correct approach.

like image 23
GordonM Avatar answered Oct 05 '22 22:10

GordonM