CSS Interview Questions (Quiz) for Front End Interviews
Find the latest version of this page on GreatFrontEnd's CSS Quiz Interview Questions.
Answers to Front-end Job Interview Questions - CSS Questions. Pull requests for suggestions and corrections are welcome!
- What is CSS selector specificity and how does it work?
- What's the difference between "resetting" and "normalizing" CSS? Which would you choose, and why?
- Describe
float
s and how they work. - Describe
z-index
and how stacking context is formed. - Describe Block Formatting Context (BFC) and how it works.
- What are the various clearing techniques and which is appropriate for what context?
- Explain CSS sprites, and how you would implement them on a page or site.
- How would you approach fixing browser-specific styling issues?
- How do you serve your pages for feature-constrained browsers? What techniques/processes do you use?
- What are the different ways to visually hide content (and make it available only for screen readers)?
- Have you ever used a grid system, and if so, what do you prefer?
- Have you used or implemented media queries or mobile-specific layouts/CSS?
- Are you familiar with styling SVG?
- Can you give an example of an @media property other than screen?
- What are some of the "gotchas" for writing efficient CSS?
- What are the advantages/disadvantages of using CSS preprocessors?
- Describe what you like and dislike about the CSS preprocessors you have used.
- How would you implement a web design comp that uses non-standard fonts?
- Explain how a browser determines what elements match a CSS selector.
- Describe pseudo-elements and discuss what they are used for.
- Explain your understanding of the box model and how you would tell the browser in CSS to render your layout in different box models.
- What does
* { box-sizing: border-box; }
do? What are its advantages? - What is the CSS
display
property and can you give a few examples of its use? - What's the difference between
inline
andinline-block
? - What's the difference between a
relative
,fixed
,absolute
andstatic
ally positioned element? - What existing CSS frameworks have you used locally, or in production? How would you change/improve them?
- Have you played around with the new CSS Flexbox or Grid specs?
- Can you explain the difference between coding a website to be responsive versus using a mobile-first strategy?
- How is responsive design different from adaptive design?
- Have you ever worked with retina graphics? If so, when and what techniques did you use?
- Is there any reason you'd want to use
translate()
instead ofabsolute
positioning, or vice-versa? And why? - Other Answers
What is CSS selector specificity and how does it work?
The browser determines what styles to show on an element depending on the specificity of CSS rules. We assume that the browser has already determined the rules that match a particular element. Among the matching rules, the specificity, four comma-separate values, a, b, c, d
are calculated for each rule based on the following:
a
is whether inline styles are being used. If the property declaration is an inline style on the element,a
is 1, else 0.b
is the number of ID selectors.c
is the number of classes, attributes and pseudo-classes selectors.d
is the number of tags and pseudo-elements selectors.
The resulting specificity is not a score, but a matrix of values that can be compared column by column. When comparing selectors to determine which has the highest specificity, look from left to right, and compare the highest value in each column. So a value in column b
will override values in columns c
and d
, no matter what they might be. As such, specificity of 0,1,0,0
would be greater than one of 0,0,10,10
.
In the cases of equal specificity: the latest rule is the one that counts. If you have written the same rule into your stylesheet (regardless of internal or external) twice, then the lower rule in your style sheet is closer to the element to be styled, it is deemed to be more specific and therefore will be applied.
I would write CSS rules with low specificity so that they can be easily overridden if necessary. When writing CSS UI component library code, it is important that they have low specificities so that users of the library can override them without using too complicated CSS rules just for the sake of increasing specificity or resorting to !important
.
References
- https://www.smashingmagazine.com/2007/07/css-specificity-things-you-should-know/
- https://www.sitepoint.com/css-selectors-specificity/
What's the difference between "resetting" and "normalizing" CSS? Which would you choose, and why?
- Resetting - Resetting is meant to strip all default browser styling on elements. For e.g.
margin
s,padding
s,font-size
s of all elements are reset to be the same. You will have to redeclare styling for common typographic elements. - Normalizing - Normalizing preserves useful default styles rather than "unstyling" everything. It also corrects bugs for common browser dependencies.
I would choose resetting when I have a very customized or unconventional site design such that I need to do a lot of my own styling and do not need any default styling to be preserved.