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 is the CSS class selector?
The class selector selects all elements that have a given class. In CSS you write a dot before the class name.
.note { background: #fff3cd; padding: 8px; }
p.note { border-left: 4px solid orange; } /* only p elements with the class */
<p class="note">A note</p>
<div class="note important">Another note</div>
One class can be used on many elements, and one element can have many classes. An id is different: see id and class and CSS Selectors.
What is grouping in CSS and what is it used for?
Grouping means writing several selectors together, separated by commas, so that one set of rules applies to all of them. It avoids repeating the same code.
/* without grouping */
h1 { font-family: Arial; color: #333; }
h2 { font-family: Arial; color: #333; }
h3 { font-family: Arial; color: #333; }
/* with grouping */
h1, h2, h3 {
font-family: Arial;
color: #333;
}
See CSS Selectors.
Explain the various ways to apply CSS styles to a web page
There are three ways to apply CSS to a page.
| Way | How | Best for |
|---|---|---|
| Inline | style="color:red" attribute on one element | Quick tests |
| Internal | <style> tag in the head | One page |
| External | <link rel="stylesheet" href="style.css"> | Whole websites. Best |
<link rel="stylesheet" href="style.css">
Inline style is the strongest and external is the easiest to maintain. See CSS Inclusion.
What are child selectors in CSS?
The child selector uses the > sign and selects only the direct children of an element. The descendant selector (a space) selects children at any depth.
div > p { color: green; } /* only p directly inside div */
div p { color: red; } /* every p inside div, at any depth */
<div>
<p>Direct child</p>
<section>
<p>Not a direct child</p>
</section>
</div>
Here div > p matches only the first paragraph. See CSS Selectors.
What is the use of float property in CSS?
The float property moves an element to the left or right, and the text and inline content wrap around it. It is mostly used to place an image inside a paragraph.
img.photo { float: left; margin: 0 12px 8px 0; }
.footer { clear: both; }
clear stops the next element from wrapping around the float. A parent does not grow around floated children, so a clearfix is needed. Today we use Flexbox or Grid for layouts. See CSS Float and Clear.
Why do we use shorthand properties in CSS?
A shorthand property sets several related properties in one line. We use them to write shorter and cleaner CSS.
/* long way */
margin-top: 10px;
margin-right: 20px;
margin-bottom: 10px;
margin-left: 20px;
/* shorthand */
margin: 10px 20px;
border: 2px solid #333;
background: #eee url("bg.png") no-repeat center;
font: italic bold 16px/1.5 Arial, sans-serif;
Common shorthands are margin, padding, border, background, font, flex and transition. Be careful: a shorthand also resets the values you do not write. See CSS Syntax.
What are the advantages of CSS?
The main advantages of CSS are:
- Separation: content stays in HTML and design stays in CSS.
- Reuse: one CSS file styles many pages, so a change in one place updates the whole site.
- Faster pages: the browser caches the CSS file, so it is downloaded only once.
- Easy maintenance: no repeated style code in every tag.
- Responsive design: the same page can fit phones, tablets and desktops with media queries.
- More design power: shadows, gradients, animations and layouts that HTML alone cannot do.
See CSS Introduction.
What is the difference between id and class selector in CSS?
An id must be unique on the page and is selected with #. A class can be used on many elements and is selected with a dot ..
| Point | id | class |
|---|---|---|
| Selector | #header | .note |
| Elements | One | Many |
| Specificity | Higher (100) | Lower (10) |
#header { background: navy; }
.note { color: gray; }
Use classes for styling in most cases, and keep ids for page anchors and JavaScript. See CSS Specificity.
What is the CSS box model?
Every HTML element is a box with four layers, from inside to outside: content, padding, border and margin.
.box {
width: 200px;
padding: 20px;
border: 5px solid #333;
margin: 10px;
}
With the default content-box, the real width of this box is 200 + 40 (padding) + 10 (border) = 250px. With box-sizing: border-box, it is 200px. See CSS Box Model.
What is the difference between margin and padding in CSS?
Padding is the space inside the element, between the content and the border. Margin is the space outside the border, between this element and others.
| Point | Padding | Margin |
|---|---|---|
| Position | Inside the border | Outside the border |
| Background colour | Shows in the padding | Margin is always transparent |
| Negative value | Not allowed | Allowed |
auto | No | Yes, to centre a block |
What is specificity in CSS?
Specificity is the way the browser decides which rule wins when more than one rule applies to the same element. A stronger selector wins.
- Inline style is the strongest.
- Then
#idselectors. - Then
.class,[attribute]and:pseudo-class. - The weakest are element selectors like
p.
p { color: blue; }
.note { color: green; }
#intro { color: red; } /* wins for <p id="intro" class="note"> */
If the specificity is equal, the later rule wins. See CSS Specificity.
What does !important do in CSS?
Adding !important after a value makes that declaration win against all normal declarations, even inline styles. Only another !important with a higher specificity can beat it.
p { color: blue !important; }
Avoid it. It breaks the normal cascade and makes the code hard to fix. It is acceptable for small utility classes (like .hidden) or to override styles of a library you cannot edit. See CSS Specificity.
What is CSS?
CSS stands for Cascading Style Sheets. It is the language used to style HTML: colours, fonts, spacing, size and layout of a web page.
h1 {
color: white;
background: teal;
text-align: center;
}
HTML gives the structure and CSS gives the design. With CSS you can change the look of a whole website by editing one file. Read CSS Introduction.
What is the difference between display none and visibility hidden?
Both hide an element, but they treat the space differently.
| Rule | Visible | Space in layout |
|---|---|---|
display: none | No | Removed. Other elements move up |
visibility: hidden | No | Kept as an empty gap |
opacity: 0 | No (but still clickable) | Kept |
.a { display: none; }
.b { visibility: hidden; }
See the display property.
What is the difference between inline, block and inline-block?
These are values of the display property.
| Value | New line | Width and height | Example |
|---|---|---|---|
block | Yes | Work | div, p |
inline | No | Ignored | span, a |
inline-block | No | Work | buttons, menu items |
one two
See the display property.
What are the different values of the position property in CSS?
The position property has five values.
| Value | Meaning |
|---|---|
static | Default, normal flow |
relative | Offset from its normal place, the space stays reserved |
absolute | Placed against the nearest positioned parent, removed from the flow |
fixed | Placed against the browser window, stays while scrolling |
sticky | Normal until a scroll point, then sticks |
.parent { position: relative; }
.badge { position: absolute; top: 0; right: 0; }
See CSS Position.
What is z-index in CSS?
z-index decides which element is on top when elements overlap. An element with a bigger value is shown above those with a smaller value.
.modal { position: fixed; z-index: 100; }
.overlay { position: fixed; z-index: 50; }
It works only on positioned elements (not static) and on flex or grid items. A stacking context can limit its effect. See CSS z-index.
What is Flexbox in CSS?
Flexbox (Flexible Box Layout) is a CSS layout system to arrange items in a row or a column, and to align them and share the free space between them.
.container {
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
}
The parent is the flex container, and its children are flex items. See CSS Flexbox.
What is the difference between Flexbox and Grid?
Flexbox works in one direction (a row or a column). Grid works in two directions (rows and columns at the same time).
| Point | Flexbox | Grid |
|---|---|---|
| Direction | One | Two |
| Best for | Menus, button groups, centring, small components | Page layouts, card galleries |
| Content or layout first | Content decides the size | Layout decides the size |
They work together: a Grid for the page and Flexbox inside each part. See Flexbox and Grid.
How to center a div horizontally and vertically in CSS?
The easiest way is Flexbox on the parent.
.parent {
display: flex;
justify-content: center; /* horizontal */
align-items: center; /* vertical */
height: 200px;
}
To centre only horizontally, give the box a width and use auto margins:
.box { width: 300px; margin: 0 auto; }
Grid also works: display: grid; place-items: center;.