2018-09-30 23:01:58 +01:00
---
id: bad87fee1348bd9aedf08805
title: Use CSS Selectors to Style Elements
challengeType: 0
2020-05-21 17:31:25 +02:00
isHidden: false
2018-09-30 23:01:58 +01:00
videoUrl: 'https://scrimba.com/c/cJKMBT2'
2019-07-31 11:32:23 -07:00
forumTopicId: 18349
2018-09-30 23:01:58 +01:00
---
## Description
< section id = 'description' >
2019-10-27 15:45:37 -01:00
With CSS, there are hundreds of CSS properties that you can use to change the way an element looks on your page.
When you entered < code > & #60 ; h2 style="color: red;"& #62 ; CatPhotoApp& #60 ; /h2& #62 ; </ code > , you were styling that individual < code > h2</ code > element with inline CSS, which stands for Cascading Style Sheets.
That's one way to specify the style of an element, but there's a better way to apply CSS.
2018-09-30 23:01:58 +01:00
At the top of your code, create a < code > style< / code > block like this:
2019-05-14 01:11:58 -07:00
```html
< style >
< / style >
```
2019-10-27 15:45:37 -01:00
Inside that style block, you can create a < dfn > CSS selector< / dfn > for all < code > h2< / code > elements. For example, if you wanted all < code > h2< / code > elements to be red, you would add a style rule that looks like this:
2019-05-14 01:11:58 -07:00
```html
< style >
h2 {
color: red;
}
< / style >
```
2018-09-30 23:01:58 +01:00
Note that it's important to have both opening and closing curly braces (< code > {< / code > and < code > }< / code > ) around each element's style rule(s). You also need to make sure that your element's style definition is between the opening and closing style tags. Finally, be sure to add a semicolon to the end of each of your element's style rules.
< / section >
## Instructions
< section id = 'instructions' >
Delete your < code > h2< / code > element's style attribute, and instead create a CSS < code > style< / code > block. Add the necessary CSS to turn all < code > h2< / code > elements blue.
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
2019-11-20 06:45:19 -08:00
- text: The style attribute should be removed from your < code > h2</ code > element.
2019-07-24 02:50:51 -07:00
testString: assert(!$("h2").attr("style"));
2019-11-20 06:45:19 -08:00
- text: You should create a < code > style</ code > element.
2019-07-24 02:50:51 -07:00
testString: assert($("style") & & $("style").length >= 1);
2018-10-04 14:37:37 +01:00
- text: Your < code > h2</ code > element should be blue.
2019-07-24 02:50:51 -07:00
testString: assert($("h2").css("color") === "rgb(0, 0, 255)");
2019-11-20 06:45:19 -08:00
- text: Your stylesheet < code > h2</ code > declaration should be valid with a semicolon and closing brace.
2019-07-24 02:50:51 -07:00
testString: assert(code.match(/h2\s*\{\s*color\s*:.*;\s*\}/g));
2019-11-20 06:45:19 -08:00
- text: All your < code > style</ code > elements should be valid and have closing tags.
2019-07-24 02:50:51 -07:00
testString: assert(code.match(/< \/style > /g) && code.match(/< \/style>/g).length === (code.match(/<style(( \s)*((type|media|scoped|title|disabled)="[^"]*")?( \s)*)*>/g) || []).length);
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'html-seed' >
```html
2019-03-13 12:38:25 -07:00
< h2 style = "color: red;" > CatPhotoApp< / h2 >
2018-09-30 23:01:58 +01:00
< main >
< p > Click here to view more < a href = "#" > cat photos< / a > .< / p >
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
< a href = "#" > < img src = "https://bit.ly/fcc-relaxing-cat" alt = "A cute orange cat lying on its back." > < / a >
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
< div >
< p > Things cats love:< / p >
< ul >
< li > cat nip< / li >
< li > laser pointers< / li >
< li > lasagna< / li >
< / ul >
< p > Top 3 things cats hate:< / p >
< ol >
< li > flea treatment< / li >
< li > thunder< / li >
< li > other cats< / li >
< / ol >
< / div >
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
< form action = "/submit-cat-photo" >
< label > < input type = "radio" name = "indoor-outdoor" checked > Indoor< / label >
< label > < input type = "radio" name = "indoor-outdoor" > Outdoor< / label > < br >
< label > < input type = "checkbox" name = "personality" checked > Loving< / label >
< label > < input type = "checkbox" name = "personality" > Lazy< / label >
< label > < input type = "checkbox" name = "personality" > Energetic< / label > < br >
< input type = "text" placeholder = "cat photo URL" required >
< button type = "submit" > Submit< / button >
< / form >
< / main >
```
< / div >
< / section >
## Solution
< section id = 'solution' >
2019-03-27 05:01:22 +01:00
```html
< style >
h2 {
color: blue;
}
< / style >
< h2 > CatPhotoApp< / h2 >
< main >
< p > Click here to view more < a href = "#" > cat photos< / a > .< / p >
< a href = "#" > < img src = "https://bit.ly/fcc-relaxing-cat" alt = "A cute orange cat lying on its back." > < / a >
< div >
< p > Things cats love:< / p >
< ul >
< li > cat nip< / li >
< li > laser pointers< / li >
< li > lasagna< / li >
< / ul >
< p > Top 3 things cats hate:< / p >
< ol >
< li > flea treatment< / li >
< li > thunder< / li >
< li > other cats< / li >
< / ol >
< / div >
< form action = "/submit-cat-photo" >
< label > < input type = "radio" name = "indoor-outdoor" checked > Indoor< / label >
< label > < input type = "radio" name = "indoor-outdoor" > Outdoor< / label > < br >
< label > < input type = "checkbox" name = "personality" checked > Loving< / label >
< label > < input type = "checkbox" name = "personality" > Lazy< / label >
< label > < input type = "checkbox" name = "personality" > Energetic< / label > < br >
< input type = "text" placeholder = "cat photo URL" required >
< button type = "submit" > Submit< / button >
< / form >
< / main >
2018-09-30 23:01:58 +01:00
```
2019-07-18 08:24:12 -07:00
2018-09-30 23:01:58 +01:00
< / section >