Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select classes with spaces

How do I select a class like class="boolean optional" ?

I have tried this:

.boolean optional {CSS}  .boolean_optional {CSS} 
like image 776
Rails beginner Avatar asked Jul 30 '11 18:07

Rails beginner


People also ask

How do you select a class with space in CSS?

They are called multiple class selectors. You basically just need to make sure all the class names are connected (no spaces between them) and separated with a dot. Show activity on this post. Classes will never actually have spaces in their name.

Can classes have spaces in CSS?

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 classes have spaces?

A class name can't have spaces. When you have a space-separated string in your class attribute, you're always giving your element several classes.

Can CSS id have spaces?

You simply can't have spaces. If you use a space it means you're using two different classes.


2 Answers

As Zepplock says, that's actually two classes in a single attribute: boolean and optional. The space is not part of a class name; it acts as the separator.

These three selectors will all match it:

.boolean .optional .boolean.optional 

The last selector only picks up this element as it has both classes.

You never include a space when chaining class selectors, not even like this:

.boolean .optional 

As that selects .optional elements that are contained within separate .boolean elements.

like image 73
BoltClock Avatar answered Sep 19 '22 06:09

BoltClock


Those are not classes with spaces :) They are called multiple class selectors.

You basically just need to make sure all the class names are connected (no spaces between them) and separated with a dot.

.boolean.optional {  } 
like image 26
brezanac Avatar answered Sep 21 '22 06:09

brezanac