Throughout the years, I’ve trained several people (mostly students); however, like many other situations I often teach them HOW to do things not WHY do things. So as I was starting to train new students, I decided it was a good idea to put together some quizzes that ask why in addition to how. It also served as a pretty good refresher (or introduction) to other members of our team.
Without further explanation or citations (I’ll most likely add these later, in addition to making it an actual quiz format) here are the first few quizzes I put together. Due to time constraints, the answers are already marked (makes it a pretty easy quiz, no?)
CSS 101
CSS Selectors
CSS Specificity Pt. 1
Specificity, besides being the hardest word to say, is how the browser determines which matching CSS rule is used. A selector receives a point in the corresponding column for each matching element (ID, class and pseduo-class, element and pseudo-element).
Specificity
Style attribute:
ID
Class, pseudo-class
Element, pseudo-element
0
0
0
0
Here specificity is shown highest from left to right. Since ul > li > a { color: red; } contains three elements, it results with:
Specificity
Style attribute:
ID
Class, pseudo-class
Element, pseudo-element
0
0
0
3
For simplicity, this can be read as 0, 0, 0, 3.
However, matters become more complicated because it’s not a 10 base system so the values aren’t simply added; depending on the browser it’s at least a 256 base.
CSS Specificity Pt. 2
CSS Shorthand
CSS shorthand is a quicker, more concise way to write several related property value pairs. Shorthand can be used for properties such as:
background
border
color
list
font
margins, outline, padding
Shorthand for margins, outline, and padding go in clockwise order.
Because you can use three values depending on the properties you want to target, it could have also been written as:
p { margin: 1em 2em 1em; }
However, this makes less sense when the property value pairs are the same.
Color hex values can be written as shorthand if both hex values are the same for each color channel.
For instance the hex value of black is #000000. Since each channel (red, green, blue) each has a hex value pair of 00, it can be written in shorthand as #000.
The font shorthand is very useful when incorporating several property value pairs.
body { font: bold italic small-caps 100%/1 Arial, sans-serif; }
Be sure to note how font-size and line-height are written. Font-size and font-family are required; any omitted property will have the initial value of normal.