Exercises (2/4)
Info
The difficulty of these exercises will be for you to get the right selector. I will give you the declarations each time.
Exercise 4
- In
style.css, use a type selector to change the color of all<h1>to maroon.
- To see how the universal selector targets all elements on a page, copy the rule below and paste it on the first line of
style.css.
- In
index.htmlthere is an<h1>element with the classtitle. Create a ruleset that selects that HTML element using its class and add the declaration:
Help
To select an element using its class, you have to add a dot . in front of the name of the class.
For example, if the class of a paragraph is intro, the selector would be:
- In
index.html, in the<h1>element with a class oftitle, add a second class nameduppercaseto this<h1>. Next, instyle.css, and add a ruleset using the.uppercaseclass selector. Then, add the declaration below inside of the curly braces.
Help
To add a second class in the opening tag of an element, you just need to seperate them with a space. For example:
is an h1 element with one classhello
is an h1 element with two classes hello and world.
- In
index.html, inside the same h1 opening tag and after the class attribute, add an id with the valuearticle-title. Next, instyle.cssadd a ruleset using the ID selector to target thearticle-title ID.
Inside of its curly braces, write the declaration:
Help
To select an element using its id, you have to add a hashtag # in front of the name of the id.
For example, if the id of a paragraph is intro, the selector would be:
- Use the attribute selector to select the
<a>element with anhrefattribute value containingflorence.
Inside of its curly braces, write the declaration:
Help
Example: to select the img element with a src attribute value containing winter, you have to write: img[src*='winter'].
- Add a ruleset at the end of
style.cssthat selects the<a>elements on the page. Add a:hoverpseudo-class to theaselector you just created. Then, set the text color to dark orange by adding the following CSS declaration inside the declaration block:
Info
Now when you hover the mouse over the “Learn More” text, the font color changes to dark orange!
Exercise 5
-
To demonstrate using classes on multiple elements, let’s give a few elements the same style. In index.html, there are four
<h2>elements. Give each of them a class calledheading-background. -
Now, let’s give a unique style to a single element using ID. In
index.html, there’s an<h6>element that displays the time the article on the page was published. Add an id attribute to the<h6>, and give it a value ofpublish-time. -
Then, in
style.css, create a ruleset targeting the newheading-backgroundclass, and give it a declaration of:
After you refresh your page, notice how all of the <h2> elements now have the same style.
- Finally, in
style.css, create another ruleset using thepublish-timeID selector. Add the declaration:
Since ID’s are single-use, this element now has a unique ID that can’t be used again in this document!