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