HTML & CSS Interview
Questions & Answers
π±Beginner QuestionsQ1βQ10
Semantic HTML means using HTML elements that carry meaning about their content β not just how they look. A <nav> tells the browser and screen readers "this is navigation". A <div> says nothing.
Why it matters: Better SEO (search engines understand page structure), better accessibility (screen readers navigate by landmarks), cleaner code, and easier CSS targeting.
<header>, <nav>, <main>, <article>, <section>, <aside>, <footer>, <figure>, <figcaption>, <time>, <address>.id and class in HTML?| Feature | id | class |
|---|---|---|
| Uniqueness | Must be unique per page | Can be reused on many elements |
| CSS selector | #myId | .myClass |
| Specificity | Higher (0-1-0-0) | Lower (0-0-1-0) |
| JS access | getElementById('myId') | getElementsByClassName() |
| Multiple per element | β Only one id | β Multiple classes allowed |
| Anchor links | β Used as page anchors (#section) | β Not used for anchors |
Every HTML element is a box made up of four layers from inside to outside: content β padding β border β margin.
box-sizing: border-box on everything. It makes width/height behave intuitively β the value you set is the actual visible size. Almost all modern CSS resets (including Tailwind) do this by default.Specificity determines which CSS rule wins when multiple rules target the same element. It's calculated as a four-part value: inline : IDs : classes/attributes/pseudo-classes : elements/pseudo-elements.
style="..." (inline)
#id
.class / [attr] / :hover
div / p / ::before
display: block, inline, and inline-block?| Property | block | inline | inline-block |
|---|---|---|---|
| New line? | β Yes | β No | β No |
| Width/height | β Respects | β Ignored | β Respects |
| Top/bottom margin | β Works | β Ignored | β Works |
| Default width | 100% of parent | Content width | Content width |
| Examples | div, p, h1-h6 | span, a, strong | img, button |
- Pseudo-class (
:): Selects elements in a specific state. Examples::hover,:focus,:nth-child(),:first-child,:not(),:checked. - Pseudo-element (
::): Selects a specific part of an element or inserts content. Examples:::before,::after,::first-line,::placeholder.
position: relative, absolute, fixed, and sticky?| Value | Reference point | Removes from flow? | Use case |
|---|---|---|---|
static | Default β no positioning | β | Normal page flow |
relative | Its own normal position | β (space reserved) | Offset slightly; create containing block |
absolute | Nearest positioned ancestor | β | Tooltips, dropdowns, overlays |
fixed | Viewport | β | Sticky nav, chat button, cookie banner |
sticky | Scrolls until threshold, then fixed | β | Table headers, sidebar navigation |
position set to anything other than static. If none exists, it positions relative to <html>. Always add position: relative to the intended parent.em, rem, px, %, and vw/vh units?| Unit | Relative to | Best for |
|---|---|---|
px | Fixed pixel (device dependent) | Borders, shadows, precise sizing |
em | Parent element's font-size | Padding/margin relative to text size |
rem | Root (html) font-size | Typography, consistent spacing scale |
% | Parent element's dimension | Fluid widths, responsive layouts |
vw | 1% of viewport width | Full-width sections, fluid type |
vh | 1% of viewport height | Full-height hero sections |
ch | Width of "0" character | Ideal line length for readability (~60-75ch) |
rem for font sizes and spacing (respects user's browser font setting), % or vw for widths, px for borders and shadows, em for component-local spacing.alt attribute on images and why is it important?The alt attribute provides alternative text for an image when it cannot be displayed β and is read aloud by screen readers for users with visual impairments.
Benefits: Accessibility (WCAG compliance), SEO (search engines index alt text), fallback text when image fails to load.
The cascade is the algorithm that determines which CSS rule wins when multiple rules could apply to the same element. It considers (in order): importance (!important), origin (user-agent, author, user), specificity, and source order.
Inheritance means some CSS properties are automatically passed from parent to child elements without you having to set them explicitly.
β‘Intermediate QuestionsQ11βQ21
Flexbox is a one-dimensional layout system (row OR column). Set display: flex on a container β its direct children become flex items.
justify-content: space-between; align-items: center
justify-content always controls the main axis (direction flex is going). align-items always controls the cross axis. Centering something? display:flex; justify-content:center; align-items:center.CSS Grid is a two-dimensional layout system β it controls both rows AND columns simultaneously. Flexbox is one-dimensional (row or column, not both).
| Feature | Flexbox | Grid |
|---|---|---|
| Dimensions | 1D (row or column) | 2D (rows + columns) |
| Best for | Component layout, nav bars | Page layout, complex 2D grids |
| Content-driven? | β Items decide sizing | β Layout decides sizing |
Media queries apply CSS rules conditionally based on device/viewport characteristics β width, height, orientation, color scheme, etc. They're the foundation of responsive design.
min-width breakpoints (scale up). Desktop-first uses max-width (scale down). Mobile-first is generally preferred β it forces you to prioritise content and produces cleaner, lighter base styles.CSS custom properties (officially "CSS variables") let you store values that can be reused throughout your stylesheet. They're defined with --name and accessed with var(--name).
transition and animation in CSS?| Feature | transition | animation |
|---|---|---|
| Trigger | State change (hover, focus, class toggle) | Plays automatically or on load |
| Keyframes | Only start β end (2 states) | Multiple keyframes (0%, 50%, 100%) |
| Loop | β No | β
Yes (infinite) |
| Direction | Always forward + reverse | Can alternate, reverse, pause |
Data attributes (data-*) let you store custom data directly on HTML elements without using non-standard attributes or hidden inputs. They're accessible via JavaScript's dataset API and CSS attribute selectors.
visibility: hidden, display: none, and opacity: 0?| Property | Visible? | Takes space? | Clickable? | Transition? |
|---|---|---|---|---|
display: none | β | β | β | β |
visibility: hidden | β | β Yes | β | β (visible/hidden) |
opacity: 0 | β (transparent) | β Yes | β Still clickable! | β Yes (smooth fade) |
opacity: 0 elements are invisible but still interactive β users can accidentally click invisible buttons. For accessible hide/show with transitions, combine opacity: 0; pointer-events: none; visibility: hidden.z-index and how does stacking context work?z-index controls the stacking order of positioned elements (elements with position other than static). Higher values appear in front.
A stacking context is created by certain CSS properties and acts as an isolated layer. Elements inside a stacking context cannot appear above elements outside it, no matter how high their z-index.
will-change and when should you use it?will-change tells the browser in advance that a property is about to change, so it can create a compositor layer and optimise rendering beforehand β resulting in smoother animations.
will-change forces the browser to create a new GPU layer which consumes memory. Using it on everything defeats the purpose. Only add it to elements that will actually animate, and remove it when the animation ends.min-width, max-width, and clamp()?π₯Advanced QuestionsQ22βQ30
contain property?CSS contain tells the browser that a subtree is independent from the rest of the page, allowing it to skip recalculation of layout, style, or paint for areas that haven't changed β a significant rendering performance win.
CSS logical properties use flow-relative terms (inline/block, start/end) instead of physical directions (left/right/top/bottom). They automatically adapt to the writing direction (LTR, RTL, vertical text).
@layer) and why are they useful?CSS Cascade Layers (@layer), introduced in 2022, let you explicitly control the order in which groups of styles are applied β regardless of specificity. Styles in later layers win over earlier ones, no matter how specific the earlier selectors are.
:has() selector and what makes it powerful?:has() is the long-awaited "parent selector" in CSS. It selects an element if it contains a specific child β letting you style parents based on their children, purely in CSS.
Critical CSS refers to the minimum CSS required to render the above-the-fold content of a page. By inlining this CSS directly in the <head>, the browser can paint the visible page immediately without waiting for external stylesheets to load.
Impact: Improves First Contentful Paint (FCP) and Largest Contentful Paint (LCP) β Core Web Vitals that affect Google search ranking. Tools like Critters, Penthouse, or Next.js automatically extract critical CSS.
BEM (Block, Element, Modifier) is a CSS naming convention that makes stylesheets more readable, scalable, and maintainable β especially in teams.
Web accessibility ensures websites work for everyone, including people with visual, motor, hearing, or cognitive impairments. WCAG 2.2 is the standard.
CSS subgrid allows a grid item to participate in the parent grid's tracks β so its children align with the grandparent grid's columns or rows. Without subgrid, nested grids define their own independent tracks.
Use case: Card components where you want the title, body, and CTA button to align horizontally across a row of cards β even when they have different content lengths.
CSS Houdini is a set of browser APIs that expose the CSS engine's internals β letting developers extend CSS itself with JavaScript. It unlocks powerful capabilities that previously required workarounds.
- Paint Worklet (CSS Painting API): Use a
<canvas>-like API to paint backgrounds, borders, or masks β as a CSS value likebackground: paint(myEffect). - Properties & Values API: Register custom CSS properties with types, inheritance, and initial values β unlike bare CSS variables which are just strings.
- Layout Worklet: Define custom layout algorithms (like masonry) as CSS properties.
- Animation Worklet: Run animations off the main thread for butter-smooth 60fps+.
Up Next: Node.js & SQL Interview Questions
You've now covered HTML & CSS. Next in the series β Node.js (event loop, streams, REST APIs) and SQL (joins, indexes, transactions). Coming soon.
β React Q&A