CSS descendant combinator
Created By: chatGPT
Descendant combinators in CSS are used to select elements that are nested within other elements. This allows you to create styles that apply to elements based on their hierarchy in the document's structure. The syntax for a descendant combinator involves using a space between two selectors.
div p {
color: blue;
}
In the example above, this style will apply to all
<p>
elements that are within a <div>
element. Both elements can be customized by utilizing the descendant combinator.ul li {
list-style-type: square;
}
Here, all
<li>
elements inside a <ul>
will have a square list-style. If you want to make further customizations, you can chain multiple descendant selectors together.div .classname h2 {
font-size: 20px;
}
In this case, the CSS will target any
<h2>
element that is a descendant of any element with the class classname
, and that is also within a <div>
. Cascading styles are useful for maintaining structure and avoiding repetitive code.section article p {
line-height: 1.5;
}
This style applies a line height of 1.5 to any
<p>
elements that are nested within any <article>
elements, which in turn are nested within a <section>
element. By effectively utilizing descendant combinators, you can create well-structured and organized web layouts.footer .link a {
color: red;
}