diff --git a/client/i18n/locales/english/intro.json b/client/i18n/locales/english/intro.json index ad43f584d55..8f26c287a54 100644 --- a/client/i18n/locales/english/intro.json +++ b/client/i18n/locales/english/intro.json @@ -958,6 +958,10 @@ "top-learn-css-foundations-projects": { "title": "Learn CSS Foundations Projects", "intro": ["A description is to be determined"] + }, + "top-learn-css-specificity": { + "title": "Learn CSS Specificity", + "intro": ["A description is to be determined"] } } }, diff --git a/client/src/pages/learn/the-odin-project/top-learn-css-specificity/index.md b/client/src/pages/learn/the-odin-project/top-learn-css-specificity/index.md new file mode 100644 index 00000000000..39bc4dc70b8 --- /dev/null +++ b/client/src/pages/learn/the-odin-project/top-learn-css-specificity/index.md @@ -0,0 +1,9 @@ +--- +title: The Odin Project +superBlock: the-odin-project +certification: the-odin-project +--- + +## The Odin project + +Description is to be determined diff --git a/curriculum/challenges/_meta/top-learn-css-specificity/meta.json b/curriculum/challenges/_meta/top-learn-css-specificity/meta.json new file mode 100644 index 00000000000..f93d4d75794 --- /dev/null +++ b/curriculum/challenges/_meta/top-learn-css-specificity/meta.json @@ -0,0 +1,45 @@ +{ + "name": "TOP Learn CSS Specificity", + "dashedName": "top-learn-css-specificity", + "isUpcomingChange": true, + "helpCategory": "HTML-CSS", + "order": 4, + "time": "", + "template": "", + "required": [], + "superBlock": "the-odin-project", + "challengeOrder": [ + [ + "6489bcd7b7600c818dc297ea", + "The Cascade of CSS Question A" + ], + [ + "6489c96782cf2e4f86f03ae2", + "The Cascade of CSS Question B" + ], + [ + "6489cb0b82cf2e4f86f03ae3", + "The Cascade of CSS Question C" + ], + [ + "6489cf5882cf2e4f86f03ae5", + "The Cascade of CSS Question D" + ], + [ + "6489cf6282cf2e4f86f03ae6", + "The Cascade of CSS Question E" + ], + [ + "6489cf6782cf2e4f86f03ae7", + "The Cascade of CSS Question F" + ], + [ + "6489cf7682cf2e4f86f03ae8", + "The Cascade of CSS Question G" + ], + [ + "648acb0745e79f79650fa2ac", + "The Cascade of CSS Question H" + ] + ] +} \ No newline at end of file diff --git a/curriculum/challenges/english/16-the-odin-project/top-learn-css-specificity/the-cascade-of-css-question-a.md b/curriculum/challenges/english/16-the-odin-project/top-learn-css-specificity/the-cascade-of-css-question-a.md new file mode 100644 index 00000000000..8d884db8734 --- /dev/null +++ b/curriculum/challenges/english/16-the-odin-project/top-learn-css-specificity/the-cascade-of-css-question-a.md @@ -0,0 +1,39 @@ +--- +id: 6489bcd7b7600c818dc297ea +title: The Cascade of CSS Question A +challengeType: 15 +dashedName: the-cascade-of-css-question-a +--- + +# --description-- + +Sometimes we may have rules that conflict with one another, and we end up with some unexpected results. “But I wanted these paragraphs to be blue, why are they red like these other paragraphs?!” As frustrating as this can be, it’s important to understand that CSS doesn’t just do things against your wishes. CSS only does what we tell it to do. One exception to this is the default styles that are provided by a browser. These default styles vary from browser to browser, and they are why some elements create a large “gap” between themselves and other elements, or why buttons look the way they do, despite us not writing any CSS rules to style them that way. + +So if you end up with some unexpected behavior like this it’s either because of these default styles, not understanding how a property works, or not understanding this little thing called the cascade. + +The cascade is what determines which rules actually get applied to your HTML. There are different factors that the cascade uses to determine this. We will examine three of these factors, which will hopefully help you avoid those frustrating “I hate CSS” moments. + +# --question-- +## --text-- + +Why might you encounter unexpected behavior in CSS? + +## --answers-- + +Due to conflicting rules and unexpected results + +--- + +Due to the browser's default styles + +--- + +Due to a lack of understanding of CSS properties + +--- + +All of the above + +## --video-solution-- + +4 diff --git a/curriculum/challenges/english/16-the-odin-project/top-learn-css-specificity/the-cascade-of-css-question-b.md b/curriculum/challenges/english/16-the-odin-project/top-learn-css-specificity/the-cascade-of-css-question-b.md new file mode 100644 index 00000000000..c3f77d8e47e --- /dev/null +++ b/curriculum/challenges/english/16-the-odin-project/top-learn-css-specificity/the-cascade-of-css-question-b.md @@ -0,0 +1,42 @@ +--- +id: 6489c96782cf2e4f86f03ae2 +title: The Cascade of CSS Question B +challengeType: 15 +dashedName: the-cascade-of-css-question-b +--- + +# --description-- + +A CSS declaration that is more specific will take precedence over less specific ones. Inline styles, which you went over in a previous lesson, have the highest specificity compared to selectors, while each type of selector has its own specificity level that contributes to how specific a declaration is. Other selectors contribute to specificity, but you're focusing only on the ones you’ve gone over so far: + +1. ID selectors (most specific selector) +2. Class selectors +3. Type selectors + +Specificity will only be taken into account when an element has multiple, conflicting declarations targeting it, sort of like a tie-breaker. An ID selector will always beat any number of class selectors, a class selector will always beat any number of type selectors, and a type selector will always beat any number of anything less specific than it. When no declaration has a selector with a higher specificity, a larger amount of a single selector will beat a smaller amount of that same selector. + +# --question-- + +## --text-- + +Which of the following is the correct order of specificity for CSS selectors, from most specific to least specific? + +## --answers-- + +Type selectors, class selectors, ID selectors + +--- + +ID selectors, class selectors, type selectors + +--- + +Class selectors, type selectors, ID selectors + +--- + +ID selectors, type selectors, class selectors + +## --video-solution-- + +2 diff --git a/curriculum/challenges/english/16-the-odin-project/top-learn-css-specificity/the-cascade-of-css-question-c.md b/curriculum/challenges/english/16-the-odin-project/top-learn-css-specificity/the-cascade-of-css-question-c.md new file mode 100644 index 00000000000..59b5175e93a --- /dev/null +++ b/curriculum/challenges/english/16-the-odin-project/top-learn-css-specificity/the-cascade-of-css-question-c.md @@ -0,0 +1,58 @@ +--- +id: 6489cb0b82cf2e4f86f03ae3 +title: The Cascade of CSS Question C +challengeType: 15 +dashedName: the-cascade-of-css-question-c +--- + +# --description-- + +Let’s take a look at a few quick examples to visualize how specificity works. Consider the following HTML and CSS code: + +```html + + +
+
+
+``` + +```css +/* rule 1 */ +.subsection { + color: blue; +} + +/* rule 2 */ +.main .list { + color: red; +} +``` + +In the example above, both rules are using only class selectors, but rule 2 is more specific because it is using more class selectors. + +# --question-- + +## --text-- + +Based on the given HTML and CSS code, which color would the `
` element be rendered as? + +## --answers-- + +blue + +--- + +red + +--- + +purple + +--- + +black + +## --video-solution-- + +2 diff --git a/curriculum/challenges/english/16-the-odin-project/top-learn-css-specificity/the-cascade-of-css-question-d.md b/curriculum/challenges/english/16-the-odin-project/top-learn-css-specificity/the-cascade-of-css-question-d.md new file mode 100644 index 00000000000..1b4794c1be9 --- /dev/null +++ b/curriculum/challenges/english/16-the-odin-project/top-learn-css-specificity/the-cascade-of-css-question-d.md @@ -0,0 +1,58 @@ +--- +id: 6489cf5882cf2e4f86f03ae5 +title: The Cascade of CSS Question D +challengeType: 15 +dashedName: the-cascade-of-css-question-d +--- + +# --description-- + +Now, let’s change things a little bit: + +```html + + +
+
+
+``` + +```css +/* rule 1 */ +#subsection { + color: blue; +} + +/* rule 2 */ +.main .list { + color: red; +} +``` + +In the example above, despite rule 2 having more class selectors than ID selectors, rule 1 is more specific because ID beats class. + +# --question-- + +## --text-- + +Based on the modified HTML and CSS code, which color would the `
` element be rendered as? + +## --answers-- + +orange + +--- + +red + +--- + +blue + +--- + +cyan + +## --video-solution-- + +3 diff --git a/curriculum/challenges/english/16-the-odin-project/top-learn-css-specificity/the-cascade-of-css-question-e.md b/curriculum/challenges/english/16-the-odin-project/top-learn-css-specificity/the-cascade-of-css-question-e.md new file mode 100644 index 00000000000..40bbba045f2 --- /dev/null +++ b/curriculum/challenges/english/16-the-odin-project/top-learn-css-specificity/the-cascade-of-css-question-e.md @@ -0,0 +1,51 @@ +--- +id: 6489cf6282cf2e4f86f03ae6 +title: The Cascade of CSS Question E +challengeType: 15 +dashedName: the-cascade-of-css-question-e +--- + +# --description-- + +```css +/* rule 1 */ +#subsection .list { + background-color: yellow; + color: blue; +} + +/* rule 2 */ +#subsection .main .list { + color: red; +} +``` + +In this final example, both rules are using ID and class selectors, so neither rule is using a more specific selector than the other. The cascade then checks the amounts of each selector type. Both rules only have one ID selector, but rule 2 has more class selectors, so rule 2 has a higher specificity! + +While the `color: red` declaration would take precedence, the `background-color: yellow` declaration would still be applied since there’s no conflicting declaration for it. + +# --question-- + +## --text-- + +Considering the updated CSS code, which of the following describes the rendering of the `
` element? + +## --answers-- + +Text color: Red, Background color: Transparent + +--- + +Text color: Red, Background color: Yellow + +--- + +Text color: Blue, Background color: Transparent + +--- + +Text color: Blue, Background color: Yellow + +## --video-solution-- + +2 diff --git a/curriculum/challenges/english/16-the-odin-project/top-learn-css-specificity/the-cascade-of-css-question-f.md b/curriculum/challenges/english/16-the-odin-project/top-learn-css-specificity/the-cascade-of-css-question-f.md new file mode 100644 index 00000000000..b8a8f09e257 --- /dev/null +++ b/curriculum/challenges/english/16-the-odin-project/top-learn-css-specificity/the-cascade-of-css-question-f.md @@ -0,0 +1,64 @@ +--- +id: 6489cf6782cf2e4f86f03ae7 +title: The Cascade of CSS Question F +challengeType: 15 +dashedName: the-cascade-of-css-question-f +--- + +# --description-- + +```css +/* rule 1 */ +.class.second-class { + font-size: 12px; +} + +/* rule 2 */ +.class .second-class { + font-size: 24px; +} +``` + +Here both rule 1 and rule 2 have the same specificity. Rule 1 uses a chaining selector (no space) and rule 2 uses a descendant combinator (the empty space). But both rules have two classes and the combinator symbol itself does not add to the specificity. + +```css +/* rule 1 */ +.class.second-class { + font-size: 12px; +} +/* rule 2 */ +.class > .second-class { + font-size: 24px; +} +``` + +This example shows the same thing. Even though rule 2 is using a child combinator (`>`), this does not change the specificity value. Both rules still have two classes so they have the same specificity values. + +*Note:* Not everything adds to specificity +When comparing selectors, you may come across special symbols for the universal selector (`*`) as well as combinators (`+`, `~`, `>`, and an empty space). These symbols do not add any specificity in and of themselves. + +# --question-- + +## --text-- + +Based on the given CSS code, which font size would be applied to elements with the class "class second-class"? + +## --answers-- + +12px + +--- + +36px + +--- + +24px + +--- + +48px + +## --video-solution-- + +3 diff --git a/curriculum/challenges/english/16-the-odin-project/top-learn-css-specificity/the-cascade-of-css-question-g.md b/curriculum/challenges/english/16-the-odin-project/top-learn-css-specificity/the-cascade-of-css-question-g.md new file mode 100644 index 00000000000..f95f59c45e6 --- /dev/null +++ b/curriculum/challenges/english/16-the-odin-project/top-learn-css-specificity/the-cascade-of-css-question-g.md @@ -0,0 +1,60 @@ +--- +id: 6489cf7682cf2e4f86f03ae8 +title: The Cascade of CSS Question G +challengeType: 15 +dashedName: the-cascade-of-css-question-g +--- + +# --description-- + +Inheritance refers to certain CSS properties that, when applied to an element, are inherited by that element’s descendants, even if we don’t explicitly write a rule for those descendants. Typography based properties (`color`, `font-size`, `font-family`, etc.) are usually inherited, while most other properties aren’t. + +The exception to this is when directly targeting an element, as this always beats inheritance: + +```html + + +
+
+
+``` + +```css +/* styles.css */ + +#parent { + color: red; +} + +.child { + color: blue; +} +``` + +Despite the `parent` element having a higher specificity with an ID, the `child` element would have the `color: blue` style applied since that declaration directly targets it, while `color: red` from the parent is only inherited. + +# --question-- + +## --text-- + +Based on the given HTML and CSS code, what would be the color of the `
` element? + +## --answers-- + +Red + +--- + +Blue + +--- + +Inherited from the parent element + +--- + +Transparent + +## --video-solution-- + +2 diff --git a/curriculum/challenges/english/16-the-odin-project/top-learn-css-specificity/the-cascade-of-css-question-h.md b/curriculum/challenges/english/16-the-odin-project/top-learn-css-specificity/the-cascade-of-css-question-h.md new file mode 100644 index 00000000000..96f4c93c570 --- /dev/null +++ b/curriculum/challenges/english/16-the-odin-project/top-learn-css-specificity/the-cascade-of-css-question-h.md @@ -0,0 +1,51 @@ +--- +id: 648acb0745e79f79650fa2ac +title: The Cascade of CSS Question H +challengeType: 15 +dashedName: the-cascade-of-css-question-h +--- + +# --description-- + +The final factor, the end of the line, the tie-breaker of the tie-breaker. Let’s say that after every other factor has been taken into account, there are still multiple conflicting rules targeting an element. How does the cascade determine which rule to apply? + +Really simply, actually. Whichever rule was the last defined is the winner. + +```css +/* styles.css */ + +.alert { + color: red; +} + +.warning { + color: yellow; +} +``` + +For an element that has both the `alert` and `warning` classes, the cascade would run through every other factor, including inheritance (none here) and specificity (neither rule is more specific than the other). Since the `.warning` rule was the last one defined, and no other factor was able to determine which rule to apply, it’s the one that gets applied to the element. + +# --question-- +## --text-- + +What determines which CSS rule is applied when there are conflicting rules targeting the same element? + +## --answers-- + +The specificity of the CSS rule. + +--- + +The inheritance hierarchy of the element. + +--- + +The presence of classes or IDs on the element. + +--- + +The order in which the rules were defined. + +## --video-solution-- + +4