π¨ Ultimate Guide: CSS Interview Questions and Answers (2025 Edition) - Part 1
Whether you're a front-end developer preparing for interviews or just sharpening your CSS skills, this guide will help you master the most commonly asked CSS interview questions with detailed answers. π
Let's dive in! π
π’ Basic CSS Interview Questions
1. What is CSS?
CSS (Cascading Style Sheets) is used to style and layout HTML documents. It controls the color, font, spacing, positioning, and responsiveness of web pages.
2. What are the different types of CSS?
-
Inline CSS (applied directly within an element using the
style
attribute) -
Internal CSS (written inside a
<style>
tag in the HTML<head>
) -
External CSS (linked via an external
.css
file using<link>
)
3. What is the difference between relative, absolute, fixed, and sticky positioning?
Positioning Type | Description |
---|---|
relative |
Positioned relative to its normal position |
absolute |
Positioned relative to the nearest positioned ancestor |
fixed |
Positioned relative to the viewport (does not scroll) |
sticky |
Behaves like relative until a scroll threshold is met, then behaves like fixed
|
4. What is the difference between em
, rem
, %
, vh
, and vw
?
-
em
: Relative to the font size of the parent element. -
rem
: Relative to the root elementβs font size. -
%
: Relative to the parent elementβs dimensions. -
vh
: 1% of the viewport height. -
vw
: 1% of the viewport width.
5. How do you create a CSS grid layout?
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}
π‘ Intermediate CSS Interview Questions
6. What is the difference between flexbox
and grid
?
- Flexbox: One-dimensional layout (row or column).
- Grid: Two-dimensional layout (rows and columns).
7. What are pseudo-classes and pseudo-elements?
-
Pseudo-classes: Define special states of an element (e.g.,
:hover
,:focus
). -
Pseudo-elements: Style specific parts of an element (e.g.,
::before
,::after
).
8. What is the z-index
property?
Defines the stack order of elements. Higher z-index
values appear in front.
9. How can you implement dark mode in CSS?
@media (prefers-color-scheme: dark) {
body {
background-color: black;
color: white;
}
}
10. What is the difference between min-width
, max-width
, and width
?
-
width
: Fixed width. -
min-width
: Minimum width the element can have. -
max-width
: Maximum width the element can have.
π΄ Advanced CSS Interview Questions
11. What is the difference between transform
, transition
, and animation
?
Property | Description |
---|---|
transform |
Modifies element shape, size, and position (e.g., rotate , scale ) |
transition |
Smoothly animates property changes |
animation |
Defines a keyframe-based animation sequence |
12. What is the difference between visibility: hidden
and display: none
?
-
visibility: hidden
: The element is hidden but still takes up space. -
display: none
: The element is removed from the document flow.
13. What are CSS variables?
Reusable custom properties defined using --variable-name
syntax.
:root {
--primary-color: #3498db;
}
button {
background-color: var(--primary-color);
}
14. What are the best practices for writing efficient CSS?
- Use external stylesheets for maintainability.
- Prefer classes over IDs for reusability.
- Minimize deep selectors to improve performance.
- Use CSS variables for consistency.
15. How can you create a responsive design without media queries?
Using flexbox, grid, and relative units like em
, rem
, %
, vh
, and vw
.
π― Final Thoughts
Mastering these CSS interview questions will give you a competitive edge in front-end development interviews. Keep experimenting with new CSS features and build modern, responsive layouts! π
π¬ Got more questions? Drop them in the comments! π
Top comments (0)