curriculum/challenges/english/blocks/top-learn-css-foundations-projects/63ee3ff8381756f9716727f3.md
Understanding how combinators work can become a lot easier when you start playing around with them and see what exactly is affected by them versus what isn't.
The goal of this exercise is to apply styles to elements that are descendants of another element, while leaving elements that aren't descendants of that element unstyled.
yellow background for p elements that are descendants of the div element.red for elements that are descendants of the div element.20px for elements that are descendants of the div element.div element.You should have a background color of yellow on your descendants.
const styleOne = new __helpers.CSSHelp(document).getStyle('.container .text');
const styleTwo = new __helpers.CSSHelp(document).getStyle('div p');
const styleThree = new __helpers.CSSHelp(document).getStyle('div .text');
const styleFour = new __helpers.CSSHelp(document).getStyle('.container p');
const styles = [styleOne, styleTwo, styleThree, styleFour];
function getCorrectStyle(){
for(let i = 0; i < styles.length; i++){
if(styles[i] != undefined){
return styles[i];
}
}
}
assert(getCorrectStyle()?.backgroundColor === 'yellow');
You should have a text color of red on your descendants.
const styleOne = new __helpers.CSSHelp(document).getStyle('.container .text');
const styleTwo = new __helpers.CSSHelp(document).getStyle('div p');
const styleThree = new __helpers.CSSHelp(document).getStyle('div .text');
const styleFour = new __helpers.CSSHelp(document).getStyle('.container p');
const styles = [styleOne, styleTwo, styleThree, styleFour];
function getCorrectStyle(){
for(let i = 0; i < styles.length; i++){
if(styles[i] != undefined){
return styles[i];
}
}
}
assert(getCorrectStyle()?.color === 'red');
You should have a font size of 20px on your descendants.
const styleOne = new __helpers.CSSHelp(document).getStyle('.container .text');
const styleTwo = new __helpers.CSSHelp(document).getStyle('div p');
const styleThree = new __helpers.CSSHelp(document).getStyle('div .text');
const styleFour = new __helpers.CSSHelp(document).getStyle('.container p');
const styles = [styleOne, styleTwo, styleThree, styleFour];
function getCorrectStyle(){
for(let i = 0; i < styles.length; i++){
if(styles[i] != undefined){
return styles[i];
}
}
}
assert(getCorrectStyle()?.fontSize === '20px');
You should center align the text on your descendants.
const styleOne = new __helpers.CSSHelp(document).getStyle('.container .text');
const styleTwo = new __helpers.CSSHelp(document).getStyle('div p');
const styleThree = new __helpers.CSSHelp(document).getStyle('div .text');
const styleFour = new __helpers.CSSHelp(document).getStyle('.container p');
const styles = [styleOne, styleTwo, styleThree, styleFour];
function getCorrectStyle(){
for(let i = 0; i < styles.length; i++){
if(styles[i] != undefined){
return styles[i];
}
}
}
assert(getCorrectStyle()?.textAlign === 'center');
<!DOCTYPE html>
<html lang="en">
<head>
<title>Descendant Combinator</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<p class="text">This should be styled.</p>
</div>
<p class="text">This should be unstyled.</p>
<p class="text">This should be unstyled.</p>
<div class="container">
<p class="text">This should be styled.</p>
<p class="text">This should be styled.</p>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Descendant Combinator</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<p class="text">This should be styled.</p>
</div>
<p class="text">This should be unstyled.</p>
<p class="text">This should be unstyled.</p>
<div class="container">
<p class="text">This should be styled.</p>
<p class="text">This should be styled.</p>
</div>
</body>
</html>
.container .text {
background-color: yellow;
color: red;
font-size: 20px;
text-align: center;
}