Top 15 CSS Q/A

Aman SrivastavAman Srivastav
5 min read

Q1: What is CSS?

A: Cascading Style Sheets — used to style HTML elements.

Q2: Types of CSS?

A: Inline, Internal, External.

Q3: Difference between relative, absolute, fixed, and sticky positioning?

  • relative → positioned relative to itself.

  • absolute → positioned relative to nearest positioned ancestor.

  • fixed → relative to viewport, stays in place on scroll.

  • sticky → toggles between relative & fixed based on scroll

One-line memory trick:

Relative = shift from self, Absolute = shift from parent, Fixed = stuck to screen, Sticky = sticks inside parent.

Q4: Difference between em, rem, px, %?

  • px → fixed pixels.

  • em → relative to parent font-size.

  • rem → relative to root (html) font-size.

  • % → relative to parent element’s size.

UnitBased OnKey Point
pxFixed pixel sizeAbsolute unit — doesn’t change with parent or root font size.
emParent element’s font sizeRelative unit — 1em = current element’s parent font size.
remRoot (html) element’s font sizeRelative unit — 1rem = font size of <html> (default 16px in browsers).
%Parent element’s sizeRelative to parent’s width/height or font size depending on property.

Q5: Difference between Flexbox and Grid?

A: Flexbox → 1D layout (row/column); Grid → 2D layout (rows & columns).

FeatureFlexboxGrid
Layout DirectionOne-dimensional (row or column)Two-dimensional (rows and columns)
Best ForAligning items in a single lineComplex layouts with rows and columns
Item Size ControlFlexible item sizing along one axisPrecise control over both axes
AlignmentEasy alignment and spacing between itemsFull control over alignment in both directions
Use CaseNavigation bars, buttons, simple layoutsFull page layouts, dashboards, complex grids

Q6: How to center a div with Flexbox?

Explanation:

  • display: flex; → Activates Flexbox

  • justify-content: center; → Centers horizontally

  • align-items: center; → Centers vertically

  • height: 100vh; → Makes sure parent takes full screen height

Q7: Difference between relative and absolute paths in CSS background images?

Path TypeExampleHow it Works
Relative Pathbackground-image: url('images/bg.jpg');Path is relative to the location of the CSS file (or HTML file if styles are inline). Changes if file location changes.
Absolute Pathbackground-image: url('https://example.com/images/bg.jpg');Full URL or path from the root directory or external source. Works regardless of CSS file location.

Q8: What is z-index?

In CSS, z-index decides which element appears on top when elements overlap. Higher z-index = in front, lower z-index = behind. It only works if the element’s position is not static.

Q9: Difference between inline, block, and inline-block?

TypeHow it BehavesExample Tags
inlineDoes not start on a new line, only takes up as much width as needed. You can’t set width/height.<span>, <a>, <strong>
blockStarts on a new line and takes full width by default. You can set width/height.<div>, <p>, <h1>
inline-blockBehaves like inline (doesn’t break line) but you can set width/height like block.<img>, <button>

inline = in line, no size control
block = big block, full width
inline-block = in line + size control

Q10: What are pseudo-classes and pseudo-elements?

TermWhat it DoesExample
Pseudo-classTargets an element based on its state (like hover, focus, visited).a:hover { color: red; } → Changes link color when mouse hovers.
Pseudo-elementTargets a specific part of an element and lets you style it.p::first-line { font-weight: bold; } → Styles only the first line of a paragraph.

Pseudo-class = state of element (one colon :)
Pseudo-element = part of element (two colons ::

Q11: What is a media query? Example?

A media query in CSS is used to apply styles only when certain conditions are true, like screen size, device type, or orientation. It’s mainly used for making responsive designs.

Q12: Difference between min-width and max-width in media queries?

PropertyWhen it AppliesExample
min-widthApplies styles when the screen width is equal to or greater than the given value.@media (min-width: 768px) → Styles for tablets & larger screens.
max-widthApplies styles when the screen width is equal to or less than the given value.@media (max-width: 600px) → Styles for mobile screens.

Q13: Difference between absolute, fixed, and sticky in navbar design?

Position TypeHow Navbar BehavesWhen to Use
absolutePositioned relative to its nearest positioned ancestor; moves when page scrolls.For navbars inside a specific section.
fixedStays in the same place relative to the viewport, even when scrolling.For always-visible navbars at the top/bottom.
stickyActs like relative until a scroll threshold is reached, then sticks like fixed within its parent container.For navbars that stick only after scrolling past a point.

absolute = moves with content
fixed = always stuck to screen
sticky = sticks only when scrolled to a certain point

Q14: What are CSS variables? Example?

CSS variables (also called custom properties) let you store values in one place and reuse them throughout your CSS. They make styling easier to maintain and update.

Q15: Difference between transition and animation?

FeatureTransitionAnimation
TriggerNeeds a user action or state change (like hover, click).Can run automatically without user interaction.
KeyframesNo keyframes — only from one state to another.Uses @keyframes for multiple steps.
ControlLimited control (start & end).Full control over steps, timing, looping, etc.
Use CaseSimple hover effects, color changes.Complex movements, looping effects, loaders.
0
Subscribe to my newsletter

Read articles from Aman Srivastav directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aman Srivastav
Aman Srivastav