Show navigation

Introduction to CSS

Today we'll look at what CSS is, how it operates, where to put it, how to write CSS rules, and how to validate our CSS.

Grouping & Combinator Selectors

SelectorDescriptionExampleSelected element
,Adds multiple selectors to the same ruleset.my-class, .my-other-class {color: blue;}<p class="my-class"></p><h1 class="my-other-class"></h1>
Combined selectorsSelects one element based on multiple selectorsdiv.my-class.my-other-class{color: blue;}<div class="my-class my-other-class"></div>
[space]Selects descendant elementsdiv .my-class{color: blue;}<div><p><span class="my-class"></span></p></div>
>Selects direct child elements#myId > span {color: blue;}<p id="myId"><span></span></p>
~Selects all sibling elementsp ~ .my-class {color: blue;}<p></p><div></div><span class="my-class"></span>
+Selects immediate siblingh1 + p {color: blue;}<h1></h1><p></p>
To see our CSS selectors in action, check out this demo:

Common CSS properties

PropertyDescription
backgroundDefines the background color or image.
borderDefines the width, style and colour of the element's border.
border-radiusDefines the degree to which an element's corners are rounded.
colorChanges the color of text.
font-familyChanges the text's font.
font-sizeChanges the text's size.
font-weightChanges the text's weight.
marginChanges the space around the outside of an element.
paddingChanges the padded space inside of an element.
text-alignChanges the justification of text.
text-decorationChanges the decoration of text.
heightChanges the height of an element.
widthChanges the width of an element.

Here's today's lab work:

  1. Sign into your CodePen Opens in a new window account.
  2. In CodePen, create a new "Pen".
  3. Create a short write-up about what you learned today about how CSS determines what rules apply to what elements. Your write-up should be styled with at least one instance of each of the following:
    • a class selector
    • an id selector
    • a sibling selector
    • a child selector
    • centred text
    • a hex code
    • a font-size
    • one style that gets overridden by another
  4. When you're done, submit the link to it via Blackboard, just like we did the first week.

Footnotes

1Honestly, this is the best list Opens in a new window I could find. Inherited properties are largely limited to text-based properties like font-size, line-height, and text-align, including list properties like list-style.
↑ Back to reference 1
2Note that combinators like +, >, ~, and [space] don't affect specificity.
↑ Back to reference 2
3I mean, most of the time. Not always. Ugh.
↑ Back to reference 3
Introduction to CSSToday we'll look at what CSS is, how it operates, where to put it, how to write CSS rules, and how to validate our CSS.