When preparing for a CSS interview, especially for a senior position (like Senior UI/UX Developer, Full Stack Engineer etc), you can expect questions that delve into advanced concepts, best practices, and real-world problem-solving. This Interview Series lists some common questions and their answers to help you refresh your understanding or acquire new learning in CSS.
1. What is Box Model in CSS?
The CSS Box Model describes how elements are structured and rendered in a web page. It consists of four parts:
- Content: The actual text or image within the element.
- Padding: Space between the content and the border, inside the element.
- Border: A line surrounding the padding (if any) and content.
- Margin: Space outside the border, separating the element from other elements.
Each of these areas can be controlled using CSS properties (padding, border, margin).
2. What is the difference between the CSS positionings like relative, absolute, fixed, and sticky?
- Relative: The element is positioned relative to its normal position. Setting
top,right,bottom, orleftmoves it from where it would normally be. - Absolute: The element is positioned relative to its nearest positioned ancestor (not static). If no positioned ancestor is found, it is relative to the initial containing block (usually the
<html>element). - Fixed: The element is positioned relative to the viewport and remains in place even when the page is scrolled.
- Sticky: The element toggles between relative and fixed, depending on the user’s scroll position. It is treated as relative until it crosses a specified threshold, after which it behaves as fixed.
Watch this recommended video to understand CSS Positioning with a practical
3. How does the z-index property work?
The z-index property controls the stacking order of positioned elements (those with position set to relative, absolute, fixed, or sticky). Elements with a higher z-index value are stacked on top of those with a lower value. Note that z-index only works on positioned elements.
4. What is Flexbox?
Flexbox is a CSS layout module designed for one-dimensional layouts. It allows for flexible and efficient arrangement of elements within a container. Key CSS properties related to Flex include:
display: flex;This style rule activates Flexbox for any container.flex-directioncontrols the direction of items (row or column).justify-contentaligns items along the main axis.align-itemsaligns items along the cross axis.flexis a shorthand forflex-grow,flex-shrink, andflex-basis.
Understanding how the Flex Layout works is essential for any UI/UX Engineer. If you want to get a good understanding of flex, trying playing around the Flexbox Fun Website. It’s a recommend
5. What are CSS Grid Layouts? How do they differ from Flexbox?
CSS Grid Layouts provide a two-dimensional grid-based layout system, allowing for both rows and columns. Key properties include:
display: grid;on the container to activate Grid Layout.grid-template-rowsandgrid-template-columnsdefine the number and size of rows and columns.grid-areaandgrid-column/grid-rowfor placing items within the grid.
Unlike Flexbox, which is best for one-dimensional layouts (either rows or columns), Grid Layouts are suited for more complex layouts with both rows and columns.
Check out Complete Guide on Grid Layout
6. What is CSS preprocessor?
CSS preprocessor is a scripting language that extends CSS with features like variables, nested rules, and mixins. It compiles down to standard CSS so that browsers can interpret them.
CSS preprocessors make it easy to manage and maintain complex stylesheets, automate repetitive tasks and create reusable code snippets. Some popular CSS preprocessors are:
- Sass (Syntactically Awesome Style Sheets)
- LESS (Leaner Style Sheets)
- Stylus
7. What are the features of a CSS preprocessor?
Features of a CSS preprocessors include:
- Variables: For reusable values like font, colors or sizes.
- Nesting: Allows for hierarchical styling.
- Mixins: Reusable chunks of code for styling (like a function).
- Functions and Operations: For more dynamic styling.
8. How do CSS media queries work?
Media queries apply CSS rules based on the characteristics of the device or viewport. They allow us to create responsive designs that adapt to different screen sizes. For example:
@media (max-width: 600px) {
.container {
flex-direction: column;
}
}In this example, the styles inside the media query will only be applied if the viewport width is 600px or less.
9. Which styles are applied to an element when multiple rules could apply?
CSS specificity determines which styles are applied to an element when multiple rules could apply. Specificity is calculated based on:
- Inline styles: Always highest (e.g.,
style="..."). - IDs:
#id(higher specificity than class). - Classes, attributes, and pseudo-classes:
.class,[attribute],:pseudo-class. - Element and pseudo-elements:
element,::pseudo-element.
The more specific the selector, the higher its priority. Specificity is calculated based on the number of each type of selector in a rule.
10. How can you optimize CSS performance?
- Minimize CSS files: Use tools like CSS Minifiers.
- Combine CSS files: Reduce HTTP requests by combining CSS files.
- Use shorthand properties: Reduce the amount of code.
- Avoid excessive use of
@import: This can cause additional HTTP requests. - Optimize selectors: Use efficient selectors and avoid deep nesting.
- Use critical CSS: Load essential styles first to improve perceived load time.
11. How do you use CSS Variables?
CSS Variables (or custom properties) contain specific values to be reused throughout a document. They are defined with a -- prefix and can be used within a document using the var() function. Example:
:root {
--main-color: #3498db;
}
body {
color: var(--main-color);
}They are useful for theming and maintaining consistency across a site.
12. How does the :nth-child() selector work?
The :nth-child() selector targets elements based on their position in a parent element. It accepts a variety of arguments:
n: Selects every nth element (e.g.,:nth-child(2)selects every 2nd element).oddoreven: Selects odd or even children respectively.An+B: A more complex pattern whereAandBare integers (e.g.,:nth-child(3n+1))
13. What are pseudo-classes and pseudo-elements in CSS?
- Pseudo-classes: Define the state of an element (e.g.,
:hover,:focus,:nth-child). - Pseudo-elements: Style specified parts of an element (e.g.,
::before,::after,::first-line).
Pseudo-classes are used for interactions and dynamic states, while pseudo-elements are used for styling specific parts of elements.
14. Explain the difference between em and rem units in CSS.
em: Relative to the font-size of the element or its closest parent. It is context-dependent.rem: Relative to the root element (<html>) font-size. It is consistent across the document regardless of nesting.
Using rem can help maintain consistent sizing across different elements, while em is useful for relative scaling within a component.
15. What are the benefits and drawbacks of using CSS Grid versus Flexbox?
- CSS Grid:
- Benefits: Ideal for complex layouts with both rows and columns, provides a two-dimensional layout system, and allows precise control over positioning.
- Drawbacks: Can be overkill for simple one-dimensional layouts.
- Flexbox:
- Benefits: Great for one-dimensional layouts (either rows or columns), easier to implement for simple alignments, and provides good support for responsive designs.
- Drawbacks: Less suited for complex layouts that require both rows and columns.
Choosing between them depends on the layout complexity and specific design needs.
