35

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.

user1212
  • 861
  • 4
  • 13
  • 22
  • 1
    possible duplicate of [handling class name with spaces in it html](http://stackoverflow.com/questions/9284313/handling-class-name-with-spaces-in-it-html) by the same user. – Andrew Marshall Feb 14 '12 at 22:58

6 Answers6

60

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".

Rono
  • 601
  • 1
  • 6
  • 2
30

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.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
18
class="paragraph one" 

actually represents two different classes

id="para one"

won't work, but you'll probably end up having an actual id of para

Mikey G
  • 3,473
  • 1
  • 22
  • 27
  • 4
    If you have no control over these white spaced IDs, you can still target them like this: `#para\ one` or by doing a substring match `[id*='para']` – carlo Jun 07 '19 at 09:43
14

You can't have spaces in id values or class names. When you have spaces in the value of the class attribute it specifies multiple classes that apply to that element:

<p class="paragraph one"> <!--Has both "paragraph" and "one" class-->

As for id values, the rules (HTML4) state the following:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

As you can see, spaces are not valid. The HTML5 spec is more leniant, but spaces are still not allowed (emphasis added):

The id attribute specifies its element's unique identifier (ID). The value must be unique amongst all the IDs in the element's home subtree and must contain at least one character. The value must not contain any space characters.

James Allardice
  • 164,175
  • 21
  • 332
  • 312
8

Modern browsers actually support id with spaces. So on Chrome 54 this works:

p#para\ one {
}

And modern browsers do not support the [id="..."] syntax, so

p[id='para one'] {
}

does not work in Chrome 54.

Adi Fairbank
  • 369
  • 3
  • 2
1

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.

elclanrs
  • 92,861
  • 21
  • 134
  • 171