All CSS Interview Questions & Answers
All CSS interview questions with simple answers. Covers what CSS is, selectors, specificity, !important, the box model, margin and padding, display and position, z-index, Flexbox vs Grid, media queries, units like px, em and rem, and transitions.
What are media queries in CSS?
A media query applies CSS only when a condition is true, such as a maximum or minimum screen width. They make a page responsive.
@media (max-width: 600px) {
.sidebar { display: none; }
.column { width: 100%; }
}
You can also test orientation, print mode and dark mode. See CSS Media Queries.
What are pseudo-classes and pseudo-elements in CSS?
A pseudo-class (one colon) selects an element in a special state or position. A pseudo-element (two colons) selects a part of an element, or adds content.
a:hover { color: red; } /* pseudo-class */
li:nth-child(odd) { background: #eee; }
p::first-letter { font-size: 2em; } /* pseudo-element */
.price::before { content: "$"; }
What is the difference between px, em, rem and % in CSS?
px is absolute. The others are relative.
| Unit | Relative to |
|---|---|
px | Nothing, a fixed pixel size |
em | The font size of the element (of the parent for font-size) |
rem | The font size of the root html element |
% | The same property of the parent (like width of the parent) |
html { font-size: 16px; }
h1 { font-size: 2rem; } /* 32px */
.box { width: 50%; } /* half of the parent */
Use rem for font sizes. See CSS Units.
What is box-sizing: border-box in CSS?
By default (content-box) the width is only for the content, and padding and border are added on top. With box-sizing: border-box the width includes padding and border.
* { box-sizing: border-box; }
.box { width: 200px; padding: 20px; border: 5px solid #333; } /* total width 200px */
Most developers add this rule to every stylesheet. See CSS Box Model.
What is the mobile first approach in CSS?
In the mobile first approach you write the base CSS for phones, and then use min-width media queries to add changes for bigger screens.
.cards { display: grid; grid-template-columns: 1fr; } /* phone */
@media (min-width: 768px) {
.cards { grid-template-columns: repeat(2, 1fr); } /* tablet */
}
@media (min-width: 1100px) {
.cards { grid-template-columns: repeat(3, 1fr); } /* desktop */
}
Phones load simpler CSS, and the code stays easier to manage. See CSS Media Queries.
What is the difference between CSS transition and animation?
A transition needs a trigger (like :hover) and goes from one state to another. An animation uses @keyframes, can have many steps, can repeat, and can start by itself.
| Point | Transition | Animation |
|---|---|---|
| Needs trigger | Yes | No |
| Steps | Two (start and end) | Many (keyframes) |
| Repeat | No | Yes, with infinite |
What is clearfix in CSS?
A parent element does not grow around its floated children, so it can collapse to zero height. The clearfix is a small class that fixes this.
.clearfix::after {
content: "";
display: table;
clear: both;
}
Add the class to the parent. The modern ways are overflow: auto or display: flow-root on the parent, or using Flexbox instead of float. See CSS Float and Clear.
How to hide an element in CSS?
You can hide an element in three main ways.
.a { display: none; } /* hidden, no space */
.b { visibility: hidden; } /* hidden, space stays */
.c { opacity: 0; } /* invisible, space stays, still clickable */
Use display: none to remove it completely. Note that screen readers also ignore an element with display: none. See display none vs visibility hidden.
What is inheritance in CSS?
Some CSS properties are inherited: a child element gets the value of its parent if you do not set one. Text properties like color, font-family, font-size and line-height are inherited. Box properties like margin, padding and border are not.
body { color: #333; font-family: Arial, sans-serif; } /* all text inside inherits it */
.box { border: 1px solid red; } /* the children do not get the border */
.child { border: inherit; } /* force inheritance */
See CSS Specificity.
How to add a background image in CSS?
Use background-image with url(), and control it with the other background properties.
.hero {
background-image: url("images/banner.jpg");
background-repeat: no-repeat;
background-position: center;
background-size: cover;
height: 300px;
}
cover fills the whole box and may cut some part. Add a background-color as a backup. See CSS Background.
What is the difference between opacity and rgba in CSS?
opacity makes the whole element transparent, including text and children. rgba() makes only the colour transparent.
.a { opacity: 0.5; } /* text also fades */
.b { background: rgba(0, 115, 170, 0.5); } /* text stays solid */
See CSS Opacity.
What is the difference between link and @import in CSS?
Both load an external CSS file, but in different ways.
| Point | <link> | @import |
|---|---|---|
| Where | In the HTML head | At the top of a CSS file |
| Loading | In parallel with the page | After the CSS file that has it is read, so slower |
| Use | Best choice | Avoid for performance |
<link rel="stylesheet" href="style.css">
@import url("other.css");
See CSS Inclusion.
What are CSS variables?
CSS variables (custom properties) store a value that you can reuse. You define them with --name and read them with var().
:root { --main-color: #0073aa; }
a { color: var(--main-color); }
button { background: var(--main-color); }
Change the variable once and every place that uses it changes. They are also easy to change with JavaScript, for themes. See CSS Variables.
What is a CSS reset?
Every browser has its own default styles, like margins on body and sizes of headings. A CSS reset removes them, so you start from the same base in all browsers.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
Normalize.css is a softer idea: it keeps the useful defaults and only fixes the differences between browsers. Modern projects often use a small reset with box-sizing: border-box. See CSS Box Model.
- « Previous
- 1
- 2