Front-End Interview Questions - CSS

  1. 1. Explain your understanding of the box model and how you would tell the browser in CSS to render your layout in different box models.
  2. 2. Implement the layout below
  3. 3. comparing PNG, SVG, JPG, GIF and WebP
  4. 4. Describe BFC (Block Formatting Context) and how it works.
  5. 5. What is CSS selector specificity and how does it work?
  6. 6. What’s the difference between “resetting” and “normalizing” CSS?
  7. 7. Describe floats and how hey work.
  8. 8. Describe z-index and how stacking context is formed.
  9. 9. How would you approach fixing browser-specific styling issues?
  10. 10. How do you serve your pages for feature-constrained browsers?
  11. 11. What are the different ways to visually hide content (and make it available only for screen readers)?
  12. 12. Do you use CSS preprocessors? What are their advantages and disadvantages?
  13. 13. How would you implement a web design comp that uses non-standard fonts
  14. 14. Explain how a browser determines what elements match a CSS selector.
  15. 15. Can you explain the difference between px, em and rem as they relate to font sizing?

1. Explain your understanding of the box model and how you would tell the browser in CSS to render your layout in different box models.

All HTML elements can be considered as boxes.

In CSS we have two types of boxes - block boxes and inline boxes.

The CSS box model is essentially a box that wraps around every HTML element. It consists of: margins, borders, padding, and the actual content. The image below illustrates the box model.

CSS Box Model

There are two types of box models: the standard box model and the alternative box model.

In the standard box model, if you give a box a width and a height attribute, this defines the size of the content box. Any padding and borders is then added to that width and height to get the total size taken up by the box.

By default, browsers use the standard box model. But we can turn on the alternative model for an element by setting box-sizing: border-box on it. By doing this you are telling the browser to take the border box as the area defined by any size you set.

2. Implement the layout below

Layout

Solution 1. Flexbox

See the Pen NWrBjjR by Kang Chen (@rainyjune) on CodePen.

Solution 2. Grid

See the Pen fe-interview-css-grid-layout by Kang Chen (@rainyjune) on CodePen.

Solution 3. inline-block

See the Pen wvWxdmz by Kang Chen (@rainyjune) on CodePen.

3. comparing PNG, SVG, JPG, GIF and WebP

Lossy Lossless Transparency Animation
PNG Yes Yes
GIF Yes Yes Yes
JPG/JPEG Yes
WebP Yes Yes Yes Yes
SVG Yes Yes Yes
  • PNG: Good for images with sharp lines; Images that has a lot of colors or requires transparency.
  • GIF: Images with few colors and does not require any advanced alpha transparency effect.
  • JPG/JPEG: For anything that is photo quality, though not for text.
  • WebP: A new format from Google, though browser support is limited.
  • SVG: High-quality graphics and animations that does not lose detail as their size changes.

4. Describe BFC (Block Formatting Context) and how it works.

We can think of a BFC as like a mini layout inside a webpage. Once an element creates a BFC, everything is contained inside it.

Some examples of how to enable a BFC:

  • The root element of the document(<html>).
  • Floats (elements where float isn’t none).
  • Aboslutely positioned elements(elements where position is absolute or fixed)
  • inline-blocks (elements with display: inline-block)
  • Table cells (elements with display: table-cell)
  • Flex items (direct children of the element with display: flex or inline-flex) if they are
    neither flex nor grid nor table containers themselves.
  • Grid items (direct children of the element with display: grid or inline-grid) if they are neither flex nor grid nor table containers themselves.
  • Block elements where overflow has a value other than visible and clip.

What can we do with a BFC?

  • The BFC prevents margins collapsing.
  • A BFC stops content wrapping floats.

5. What is CSS selector specificity and how does it work?

Specificity is a weight that is applied to a given CSS declaration. when multiple declarations have equal specificity, the last declaration found in the CSS is applied to the element. Specificity only applies when the same element is targeted by multiple declarations.

The following list of selector types increases by specificity:

  1. Type selectors (e.g., h1) and pseudo-elements (e.g., ::before).
  2. Class selectors (e.g., .example), attributes selectors (e.g., [type="radio]) and pseudo-classes (e.g., :hover).
  3. ID selectors (e.g., #example);

Universal selector (*), combinators (+, >, ~ ' ', ||) and negation pseudo-class (:not()) have no effect on specificity.

Inline styles added to an element (e.g., style="font-weight: bold;") always overwrite any styles in external stylesheets, and can be thought of as having the highest specificity.

6. What’s the difference between “resetting” and “normalizing” CSS?

  • Resetting: Aim to remove all built-in browser styling. You’re then supposed to add all decoration yourself.
  • normalizing: Aim to make built-in browser styling consistent across browsers. You’re then supposed to add only the difference in decoration your design needs.

I would use normalize.css in most cases, as it preserves useful defaults, will make CSS smaller and faster to write.

7. Describe floats and how hey work.

There are left, right and none for float. Each value indicates how an element should float. When float is set, each element will get out of its normal flow and will be shifted to the specified direction, until it gets its container or another floated element.

8. Describe z-index and how stacking context is formed.

The z-index is a property that allows developers to stack elements in the CSS. It's basically a 3D property so it allows the developer to choose how close the element appears. This is how stacking context is formed.

Some examples:

  • Root element of the document (<html>).
  • Element with a position value absolute or relative and z-index value other than auto.
  • Element with a position value fixed or sticky.
  • Element with a opacity value less than 1.

9. How would you approach fixing browser-specific styling issues?

  1. Use normalize.css to create a consistant default style
  2. Use a separate stylesheet that only onloads when that specific browser is being used.

10. How do you serve your pages for feature-constrained browsers?

Graceful degradation - The practice of building an application for modern browsers while ensuring it remains functional in older browsers.

Progressive enhancement - The practice of building an application for a base level of user experience, but adding functional enhancements when a browser supports it.

11. What are the different ways to visually hide content (and make it available only for screen readers)?

  • display:none or visibility:hidden - Hide content from all users, and is ignored by screen readers.
  • hidden attribute - it functions the same as CSS display:none.
  • width:0px, height:0px - Remove from the flow of the page, so most screen readers will not read it.
  • text-indent: -10000px - Off the visible screen, screen readers will still read text.
  • Absolutely positioning content off-screen - Visually hiding content that will be read by a screen reader
    1
    2
    3
    4
    5
    6
    7
    8
    .sr-only {
    position:absolute;
    left:-10000px;
    top:auto;
    width:1px;
    height:1px;
    overflow:hidden;
    }
  • CSS clip - This faily modern technique will hide or clip content that does not fit into a 1-pixel visible area. Reable by modern screen readers.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    {
    clip: rect(1px, 1px, 1px, 1px);
    clip-path: inset(50%);
    height: 1px;
    width: 1px;
    margin: -1px;
    overflow: hidden;
    padding: 0;
    position: absolute;
    }

12. Do you use CSS preprocessors? What are their advantages and disadvantages?

The three most popular and stable CSS preprocessors are SaSS, LESS and Stylus.

Advantages:

  • Option to add variables, mixins and functions for code reusability
  • Hierarchy
  • Easy to join multiple files

Disadvantages:

  • Compilation takes longer time
  • They can produce very large CSS files
  • Extra Tooling and developer inconvenience

13. How would you implement a web design comp that uses non-standard fonts

  • Web fonts with @font-face
    1
    @font-face { font-family: Delicious; src: url('Delicious-Roman.otf'); }
    Then call it using font-family:
    1
    h3 { font-family: Delicious, sans-serif; }
  • Using font service link
    1
    <link href='http://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'>

14. Explain how a browser determines what elements match a CSS selector.

Browsers match selectors from rightmost to left. Browsers filter out elements in the DOM according to the key selector, and traverse up its parent elements to determine matches. The shorter the length of the selector chain, the faster the browser can determine if that element matches the selector.

For example with this selector p span, browsers firstly find all the <span> element, and traverse up its parent all the way up to the root to find the <p> element. For a particular <span>, as soon as it finds a <p>, it knows that the <span> matches and can stop its matching.

15. Can you explain the difference between px, em and rem as they relate to font sizing?

  • px - The easiest measurement to use. It is not good for font-size, as it doesn’t respect user’s font preferences.
  • em - An em is equal to the computed font-size of that element’s parent. Best for layouts and media queries.
  • rem - rem values are relative to the root html element, not to the parent element. Best for font sizing and spacing (margin, padding, etc).

https://www.digitalocean.com/community/tutorials/css-rem-vs-em-units