0

Can anyone explain why the following CSS style works:

input.foo {
    background-color: red;
}

But the following one doesn't:

input .foo {
    background-color: red;
}

All I did was add a space after input.

bodacydo
  • 75,521
  • 93
  • 229
  • 319
  • possible duplicate of [When combining selectors does space means the same as no space?](http://stackoverflow.com/questions/16175379/when-combining-selectors-does-space-means-the-same-as-no-space) – Felix Kling Sep 19 '14 at 18:41
  • Because these two selectors mean different things. Basic CSS 101: https://developer.mozilla.org/en-US/docs/Web/CSS/Descendant_selectors – deceze Sep 19 '14 at 18:41

2 Answers2

3

input.foo means an input with a class of foo.

input .foo means elements with a class of foo contained in an input element.

These are fundamentally different.

E.g.

label.foo targets this label element:

<label class="foo"></label>

But label .foo will target the span in this:

<label>
    <span class="foo">
</label>

Read more on CSS Selectors:
CSS 2.1: http://www.w3.org/TR/CSS2/selector.html
CSS 3: http://www.w3.org/TR/css3-selectors/

Arbel
  • 30,599
  • 2
  • 28
  • 29
0

You're trying to select an input tag with a class of .foo. The second one selects an element within input with a class of .foo.

The first one means selecting this type of element:

<input class="foo" />

The second means:

<input>
    <div class="foo" />
</input>

Which would be invalid in HTML, but that's beside the point.

CassOnMars
  • 6,153
  • 2
  • 32
  • 47