Front-end quizzes

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

1. What does CSS stand for?
2. Which of the following properly describes CSS syntax?
3. The property: value; of CSS syntax is called what?
4. What color text would paragraph end up having?
    p { color: blue; }
    p { color: red; }
5.What are the three ways to insert CSS? (3 pts.)

CSS Selectors

1. Which CSS rule correctly identifies a class selector?
2. How many id selectors with the same attribute (e.g. id#foo) may technically be used in a single HTML document?
3. What type of selector is foo > bar?
4. Which of the following rule would make the second paragraph text red?
    <p class="one" id="red">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse felis augue, sollicitudin vel dapibus sit amet, mollis condimentum est. Etiam et suscipit massa. Praesent at lacus elit, vitae lobortis ligula. In ac convallis diam. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse eros sapien, interdum sit amet sollicitudin nec, consectetur ornare tellus. Nam fermentum mollis aliquam. Nullam ut sem in lacus feugiat commodo. Maecenas fermentum, felis luctus vulputate vehicula, magna ligula sollicitudin justo, at ornare est nulla vitae quam. Mauris nec diam metus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Morbi tincidunt purus ac ipsum porttitor adipiscing.</p>

    <p class="two" id="blue">Aliquam laoreet eleifend semper. Nulla faucibus orci sit amet tellus feugiat id elementum libero mollis. Proin diam mauris, iaculis in sodales id, vestibulum accumsan enim. Sed eu leo est, sit amet sodales nulla. Ut facilisis massa non turpis ornare luctus. Sed vestibulum adipiscing pellentesque. Pellentesque porttitor placerat mattis. Donec felis leo, semper elementum mollis a, volutpat in dolor. Sed nec ante ac arcu vulputate pretium. Nunc egestas leo non neque fringilla non scelerisque lacus sodales. Aenean sit amet laoreet velit. Donec elementum, libero quis eleifend consequat, erat arcu interdum augue, vitae dapibus lacus est imperdiet mi.</p>
5. There are both pseudo-class and pseudo-element 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.

1. In simplistic terms, what would the specificity of a#foo > li.bar > a be?
2. In simplistic terms, what would the specificity of ul#foo + #bar > li:first-child a.foo::after be?
3. What color would the link text be?
    p#foo a#bar { color: red; }
        
    <p id="foo">Lorem ipsum dolor <a href="foo.html" id="bar" style="color: blue;">sit amet</a>, consectetur adipiscing elit. Quisque egestas venenatis dui, consectetur convallis dui venenatis ac. Nullam non lacus eu libero tristique pretium</p>

Using the !important declaration basically adds another level higher of specificity.

.foo { color: black !important; } would be equal to 1, 0, 0, 1, 0.

4. What would the nuclear (the heaviest-handed) option to override specificity be?
5. What level of specificity should each selector contain? (2 pts.)

CSS Specificity Pt. 2

    .red { color: red; }
    p { color: blue; }
    
    <p class="red">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer fringilla odio et ante vehicula ac posuere neque venenatis. Maecenas tortor quam, mollis viverra blandit non.</p>
1. What color of text would the paragraph have?
2. Why might ul#foo be considered an overqualified selector? (3 pts.)
    ul#foo { color: red; }
    ul {color: blue; }
    
    <ul id="foo">
        <li>Item</li>
        <li>Item</li>
    </ul>
    
    <ul>
        <li>Item</li>
        <li>Item</li>
    </ul>
3. All of the following selectors will render the first link color red, but which selector is a more efficient in regards to browser render time?
    <ul>
        <li><a href="foo.html" id="foo" class="foo">Foo link</a></li>
        <li><a href="bar.html">Bar link</a></li>
    </ul>
4. All of the following selectors will render the first link color red, but which selector is a more efficient in regards to specificity and reusability?
    <ul>
        <li><a href="foo.html" id="foo" class="foo">Foo link</a></li>
        <li><a href="bar.html">Bar link</a></li>
    </ul>
5. Without using !important, at least how many class selectors would it take to override the properties of one id selector?

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
1. Why might you not want to always use shorthand? (3 pts.)

Shorthand for margins, outline, and padding go in clockwise order.

p { 
    margin-top: 1em;
    margin-right: 2em;
    margin-bottom:  1.5em;
    margin-left: 1.75em;
}

is equivalent to

p { margin: 1em 2em  1.5em  1.75em; }
2. What would this be equivalent to? (2 pts.) p { margin-top: 1em; margin-right: 2em; margin-bottom: 1em; margin-left: 2em; }
p { margin-top: 1em; margin-right: 2em; margin-bottom: 1em; margin-left: 2em; }

Is equivalent to:

p { margin: 1em 2em 1em 2em; }

That can be shortened further to:

p { margin: 1em 2em; }

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.

3. p { margin: 1.5em auto 1em; } is equivalent to what long-hand rule?

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.

4. The hex value for red #FF0000 can be written in shorthand as?

The font shorthand is very useful when incorporating several property value pairs.

body {
    font-weight: bold;
    font-style: italic;
    font-variant: small-caps;
    font-size: 100%;
    line-height: 1;
    font-family: Arial, sans-serif;  
}

is equivalent to:

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.

5. What would the font shorthand be for the below rule? (2 pts.)
body {
        font-size: 1em;
        line-height: 1.25;
        font-family: Georgia, serif;
    }