CSS Interview Questions

25 Questions

CSS Basics

Q1

What is CSS?

CSS (Cascading Style Sheets) is used to style and design HTML webpages. It controls colors, layouts, spacing, fonts, and responsiveness. Without CSS, webpages look plain and unstructured. Example: h1 { color: blue; }

Q2

What are the different ways to add CSS to HTML?

CSS can be added using inline CSS, internal CSS, and external CSS. Inline styles work on single elements, internal CSS stays inside the HTML file, and external CSS is stored in a separate .css file. Example: <p style="color:red">Hello</p>

Q3

What is the difference between inline, internal, and external CSS?

Inline CSS styles one element directly. Internal CSS is written inside the <style> tag within the HTML file. External CSS is written in a separate file and linked to multiple webpages. Example: <link rel="stylesheet" href="style.css">

Q4

What is the difference between class and id selectors?

A class selector can be used on multiple elements, while an id selector should be unique for one element only. Classes use . and ids use # in CSS. Example: .box { color: blue; } #title { color: red; }

Q5

What is specificity in CSS?

Specificity decides which CSS rule is applied when multiple rules target the same element. Inline CSS has the highest priority, followed by id selectors, classes, and element selectors. Example: #heading { color: red; }

Q6

What is the difference between relative, absolute, fixed, and sticky positioning?

relative moves an element from its normal position. absolute positions relative to its parent. fixed stays fixed on screen while scrolling. sticky behaves like relative until scrolling reaches a specific point. Example: .box { position: fixed; top: 0; }

Q7

What is the difference between margin and padding?

Margin creates space outside an element's border, while padding creates space inside the border around the content. Both help improve layout spacing and readability. Example: div { margin: 20px; padding: 10px; }

Q8

What is the CSS box model?

The CSS box model describes how elements are structured using content, padding, border, and margin. Every HTML element follows this box structure for spacing and layout calculations. Example: div { border: 2px solid black; }

Q9

What is z-index in CSS?

z-index controls which element appears on top when elements overlap. Higher z-index values appear above lower values. It only works on positioned elements. Example: .box { position: absolute; z-index: 10; }

Q10

What is the difference between display: none and visibility: hidden?

display: none completely removes the element from the layout. visibility: hidden hides the element visually but still keeps its space reserved in the page layout. Example: div { visibility: hidden; }

Flexbox

Q11

What is Flexbox?

Flexbox is a CSS layout system used to arrange items easily in rows or columns. It helps align, distribute, and center elements efficiently without complex positioning. Example: .container { display: flex; }

Q12

What is the difference between justify-content and align-items?

justify-content aligns items horizontally along the main axis, while align-items aligns items vertically along the cross axis inside a flex container. Example: .container { display: flex; justify-content: center; align-items: center; }

Q13

What does flex-wrap do?

flex-wrap controls whether flex items stay on one line or move to the next line when space becomes limited. It helps create responsive layouts. Example: .container { display: flex; flex-wrap: wrap; }

Q14

What is the difference between align-content and align-items?

align-items aligns individual flex items inside a row, while align-content controls spacing between multiple rows inside a flex container when wrapping occurs. Example: .container { align-content: space-between; }

Q15

How do you center a div using Flexbox?

Flexbox centers elements using justify-content and align-items. This method works for both horizontal and vertical alignment and is commonly used in modern layouts. Example: .container { display: flex; justify-content: center; align-items: center; }

CSS Grid

Q16

What is CSS Grid?

CSS Grid is a two-dimensional layout system used for creating rows and columns together. It is useful for designing complete webpage layouts and responsive grids. Example: .container { display: grid; grid-template-columns: 1fr 1fr; }

Q17

What is the difference between Grid and Flexbox?

Flexbox works mainly in one direction, either row or column. Grid works in two directions, handling both rows and columns together for complex layouts. Example: display: grid; display: flex;

Q18

What does grid-template-columns do?

grid-template-columns defines the number and width of columns inside a grid container. It helps structure layouts into equal or custom-sized columns. Example: .container { grid-template-columns: 200px 1fr; }

Q19

What is the difference between auto-fit and auto-fill?

auto-fit expands grid items to fill available space, while auto-fill keeps empty column tracks even if no items exist. Both are used in responsive grids. Example: grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));

Q20

How do you create a responsive grid layout?

Responsive grids are created using CSS Grid, flexible units, and media queries. minmax() and auto-fit help adjust columns automatically based on screen size. Example: .container { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }

Responsive Design

Q21

What is responsive web design?

Responsive web design makes websites adapt properly to different screen sizes like mobiles, tablets, and desktops. It improves readability and user experience across devices. Example: img { max-width: 100%; }

Q22

What are media queries?

Media queries apply different CSS styles based on screen size, resolution, or device type. They are important for building responsive websites. Example: @media (max-width: 600px) { body { background: lightgray; } }

Q23

What is the difference between em, rem, %, vh, and vw?

em depends on parent font size, rem depends on root font size, % depends on parent dimensions, vh uses viewport height, and vw uses viewport width. Example: div { width: 50vw; }

Q24

What is mobile-first design?

Mobile-first design means designing webpages for smaller mobile screens first, then adding styles for tablets and desktops later using media queries. Example: body { font-size: 14px; }

Q25

What is the viewport meta tag?

The viewport meta tag controls webpage scaling and responsiveness on mobile devices. Without it, webpages may appear zoomed out or improperly sized. Example: <meta name="viewport" content="width=device-width, initial-scale=1.0">