Foundational Exercises
These exercises are designed to help you practice the concepts you have learned so far, and to introduce you to a few new concepts. Complete as many as you can, and feel free to reference the previous content as needed.
You may also need to reference the CSS Documentation to complete these exercises, or other resources you find online. Learning how to read documentation and find answers is a crucial skill for any developer. Never expect to know everything, and be prepared to do some research!
Exercise 1: Integrating CSS
In this exercise, you are going to practice adding CSS to an HTML file using all three methods: external CSS, internal CSS, and inline CSS. You should only be using type selectors for this exercise when adding styles via the external and internal methods. You should also use keywords for colors (e.g. “blue”) instead of using RGB or HEX values (if you are familiar with them).
Goals
- You should see a
<div>element with some text.- It should have a red background, white text, a font size of 32px, text center aligned and bold.
- The CSS for the
<div>element should be added externally, and using a type selector.
- You should see a
<p>element with some text.- It should have a green background, white text, and a font size of 18px.
- The CSS for the
<p>element should be added internally, and using a type selector.
- You should see a
<button>element with some text.- The
<button>element should have an orange background and a font size of 18px. - The CSS for the
<button>element should be added using inline styles.
- The
Exercise 2: Classes and IDs
There are several elements in the HTML file provided, which you will have to add either class or id attributes to. You will then have to add rules in the CSS file provided using the correct selector syntax.
Goals
- You should see a
yellowbackground for all odd numbered elements in the list. - You should have a
classselector used for all odd numbered elements in the list. - You should see that the second element in the list has
bluetext and afont-sizeof36px. - The
font-sizeand text color on the second element should be set by using anidattribute. - You should see that the third element in the list has a
font-sizeof24px. - The
font-sizeon the third element should be set by using aclassattribute. - You should see that the fourth element in the list has a
redbackground, afont-sizeof24px, and afont-weightofbold. - The
font-sizeof the fourth element should be set with aclassattribute. Thefont-weightand thecolorshould be set with anidattribute.
Exercise 3: More Styles
Let’s build a little off the previous exercise, in which you added multiple classes to a single element in order to apply two different rules to it.
Goals
- You should see a black background and white text on the first
buttonelement. - You should see a yellow background on the second
buttonelement. - You should set a font size of
28pxusing a selector list. - You should have a list of fonts containing
HelveticaandTimes New Romanwithsans-serifas a fallback in the selector list. - You should see a unique class name on each element.
- You should have a selector list for styles that both elements share.
Exercise 4: Selector Chaining
With this exercise, we’ve provided you a completed HTML file, so you will only have to edit the CSS file. For this exercise, it’s more important to understand how chaining different selectors works than how to actually add the attributes.
Goals
- You should see a width of
300pxon theavatarandproportionedclass. - You should give it a
heightso that it retains its original square proportions (don’t hardcode in a pixel value for the height!). - You should give the elements with both the
avataranddistortedclasses awidthof200px. - You should give it a
heighttwice as big as it’s width.
Exercise 5: Combinators
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.
Goals
- You should see a
yellowbackground forpelements that are descendants of thedivelement. - You should see a text color of
redfor elements that are descendants of thedivelement. - You should see a font size of
20pxfor elements that are descendants of thedivelement. - You should center align text for elements that are descendants of the
divelement.