Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

handling css id and classes with spaces

Tags:

css

I have a paragraph as

<p id="para one" class="paragraph one">Content</p> 

How does one represent the id and class with spaces in the css

When I use

#para#one{ }  .paragraph.one{ } 

It does not work with the css above.

like image 909
user1212 Avatar asked Feb 14 '12 22:02

user1212


People also ask

Can CSS id have spaces?

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

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.

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 we use ID and class together in CSS?

Yes you can. You just need to understand what they are for, the class is more general and can be used several times, the id (is like your id's) you can use it only once.


2 Answers

Just came across this one myself (styling an existing page with no way to change the id).

This is what I used to style it by id:

p[id='para one']{ } 

And, as said previously, .paragraph.one selects two classes - this means it will also select elements with class=" one paragraph" and class="not a paragraph this one".

like image 99
Rono Avatar answered Sep 18 '22 16:09

Rono


Your class CSS is correct. You don't have a class with a space in it, you have two classes, "paragraph" and "one". Your CSS properly selects elements that have both of those classes:

.paragraph.one { color: red; } 

This is a useful technique for splitting out facets of the element into separate classes that can be combined individually. Note also that <p class="one paragraph"> will match the same selector.

like image 43
Ned Batchelder Avatar answered Sep 19 '22 16:09

Ned Batchelder