Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between CSS classes .foo.bar (without space) and .foo .bar (with space) [duplicate]

Tags:

css

People also ask

What does a space between CSS selectors mean?

A space in a CSS selector has very special meaning. It means that the part of the selector that occurs right of the space is within (a child of) the part of the selector to the left. This doesn't apply to your second example, because it has no space.

What is the difference between and space in CSS?

vs. Space. Here, the (space) selects all the all the <span> elements inside <div> element even if they are nested inside more than one element. The > selects all the children of <div> element but if they are not inside another element.

Can CSS classes have spaces?

The class name can't contain a space, but it can contain hyphens or underscores. Any tag can have multiple space-separated class names.

Can CSS ids have spaces?

You simply can't have spaces. If you use a space it means you're using two different classes. One is para and the other one is one .


.element .symbol

means .symbol inside .element

.element.symbol

means .element that has the class symbol as well.

So,

.element.large .symbol

means .symbol inside .element that has the class large as well.


I think you got a slight misunderstanding what the first one means.

.element .symbol {}

Means that those CSS settings are applied to any HTML element with the class .symbol that is inside an element with the class .element.

<div class="element">
    <div class="symbol" />
</div>

In this example your first CSS entry would affect the <div> tag in the middle.

Your second example means that the first class requires two classes to be affected. Other than that it's equal to the first one.

<div class="element large">
    <div class="symbol" />
</div>

So if the HTML looks like this, the CSS values will be applied to the inner <div> tag as well.

If you want to set CSS tags that apply for multiple classes separately then you need to split them up using a comma. So it looks like this:

.element, .symbol {}

Edit: By request the link to the documentation of the CSS selectors.


Using

.element.large

refers to an element with both classes:

<div class="element large"></div>

rather than a descendant of an element:

.element .large

meaning that in:

<div class="element">
    <div class="large"></div>
</div>

only

<div class="large"></div>

is 'receiving' the styles.

Basically, being separated by a space implies two elements with a descendant relationship.


You would use .element .symbol this where you have an element inside of another element. For example:

<div class="element">
    <i class="symbol"></i>
</div>

If down the road you wanted to differentiate some divs, you could add an additional class to target only those that differ, and target it with .element.large .symbol. So, for example:

<div class="element large">
    <i class="symbol"></i>
</div>

In your second example, the first part of the selector is simply an element with two classes, as in <span class="element large"> or <span class="large element">.

In general, each part of a selector applies to one HTML element.

table[border].clname means a table with a border attribute and a class of clname, while table [border] .clname means an element with class clname, in an element with a border attribute, in a table.

(Edit: well, I say "one HTML element", but of course you can have more than one table that this applies to. You understand.)