Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class names concatenated or separated by a space

Tags:

css

When do you separate style classes with a space? So for example: what is the difference between the following two blocks of css?

Block 1:

div {     color: brown; }  div.special {     font-size: 18px; } 

Block 2:

div {     color: brown; }  div .special {     font-size: 18px; } 

This is the HTML:

<div class="special">The quick brown fox jumps over the lazy dog.</div> 

I tried both versions. Only with block 1 the text wil be in font size 18.

like image 504
Bart Weber Avatar asked May 31 '13 13:05

Bart Weber


People also ask

Can you have spaces in class names?

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 Java class names have spaces?

A class name is an identifier—a series of characters consisting of letters, digits, underscores ( _ ) and dollar signs ( $ ) that does not begin with a digit and does not contain spaces.

How do you separate multiple classes in HTML?

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.


1 Answers

You separate classes by a space when you want to refer to descendant element and you concatenate them when you want to refer to a single element with multiple classes.

For example, to refer to a div with two classes, e.g. <div class="foo bar"> you could use:

div.foo.bar {...} 

To refer to the child span element <div class="foo"><span class="bar">stuff</span></div> you could use:

div.foo .bar {...} 
like image 134
j08691 Avatar answered Oct 06 '22 00:10

j08691