I know how to give same styling to multiple classes , we do use a comma between the class names . but what i can't understand what this below code is.
.navbar-inverse .navbar-toggle { background-color: #333; }
I know how to give same styling to multiple classes , we do use a comma between the class names . but what i can't understand what this below code is.
.navbar-inverse .navbar-toggle { background-color: #333; }
Its hierarchy this will impact to .navbar-toggle
class under navbar-inverse
This will give background color to the element with class name "navbar-toggle" which is inside an element with class name "navbar-inverse"
The space between parts of a CSS selector is called descendant combinator
:
ancestorSelector descendantSelector { rules }
A CSS selector utilizing this is called descendant selector. Please note that any amount of whitespace works just like a single space.
whereas >
indicates a parent-child relationship and is called child combinator
.
parentSelector > childSelector { rules }
A CSS selector utilizing this is called child selector.
Look at this example:
/*
this will affect any element whose classlist contains "b"
inside any element whose classlist contains "a",
no matter how deeply nested it is
*/
.a .b {
color: blue;
}
/*
this will affect only elements whose classlist contains "b"
and which are direct children of any element whose classlist contains "a"
*/
.a > .b {
color: red;
}
<div class="a">
<div class="b">
This is the direct child of <code><div class="a"></code>.
<div class="b">
This is <strong>not a direct child</strong> of <code><div class="a"></code>.
</div>
</div>
<div class="c">
<div class="b">
<hr />
Unaffected by <code>.a > .b</code>, but <code>.a .b</code> matches.
</div>
</div>
</div>
navbar-inverse
is the parent and .navbar-toggle
is the child.
When you want to make a hierarchy of parent and child classes, you follow this approach. In the below code, see how the first .navbar-toggle
has background while the second one does not.
.navbar-inverse {
background-color: #ccc;
padding: 1em;
}
.navbar-inverse .navbar-toggle {
background-color: #333;
color: white;
}
.navbar {
background-color: #ccc;
padding: 1em;
}
<div class="navbar-inverse">
<div class="navbar-toggle">
This is the child
</div>
</div>
<hr>
<div class="navbar">
<div class="navbar-toggle">
This wont be affected!
</div>
</div>
.navbar-inverse > .navbar-toggle { /This style applied only for first level child/
}
.navbar-inverse .navbar-toggle { /This style applied for all the child/
}
It shows that we have two classes
.navbar-inverse
and .navbar-toggle
.navbar-toggle
is child of .navbar-inverse
and the css applied will be workable for only and only where .navbar-toggle
as child of .navbar-inverse
will be written in html.