{jsonbody.com}

Top 40 HTML & CSS Interview Questions & Answers

Top 40 HTML and CSS interview questions and answers for freshers and experienced developers: semantic HTML, box model, Flexbox, Grid and responsive design.

Loading…

HTML Fundamentals

Q1. What is semantic HTML?

Using elements that describe their meaning, such as <header>, <nav>, <main>, <article> and <footer>, instead of generic <div>s. It helps screen readers, search engines and other developers understand the page.

Q2. What does <!DOCTYPE html> do?

It tells the browser to render the page in standards mode using HTML5 rules. Without it, browsers fall back to quirks mode, which copies old, inconsistent behaviour.

Q3. What is the difference between block and inline elements?

Block elements such as <div> and <p> start on a new line and take the full width. Inline elements such as <span> and <a> sit within text and only take the width of their content.

Q4. What is the difference between <section>, <article> and <div>?

<article> is self-contained content that makes sense alone, such as a blog post. <section> groups related content, usually with a heading. <div> has no meaning and is only for styling or layout.

Q5. What are data-* attributes?

Custom attributes, such as data-user-id="42", that store extra information on an element. JavaScript reads them through element.dataset.userId.

Q6. What is the difference between async and defer on a script?

Both download the script without blocking HTML parsing. async runs it as soon as it downloads, in any order. defer runs scripts in order after the HTML is parsed.

Q7. Why is the alt attribute important?

It describes an image for screen readers and is shown if the image fails to load. Decorative images should use an empty alt="" so assistive technology skips them.

Q8. What are the main HTML5 form input types?

Types such as email, number, date, tel, url, range and color give built-in validation and the right mobile keyboard. Attributes like required, pattern and min add validation without JavaScript.

Q9. What is the purpose of the <label> element?

It names a form control for screen readers and makes the label text clickable. Link it with for matching the input's id, or wrap the input inside the label.

Q10. What is ARIA and when should you use it?

Accessible Rich Internet Applications attributes, such as aria-label and role, add accessibility information. Use native HTML elements first, and ARIA only when no element has the meaning you need.

CSS Basics, Box Model & Specificity

Q11. What is the CSS box model?

Every element is a box made of content, padding, border and margin, from the inside out. Its size and the space around it come from these four layers.

Q12. What does box-sizing: border-box do?

It makes width and height include padding and border, so a 200px-wide box stays 200px when you add padding. Most projects apply it to every element.

Q13. How does CSS specificity work?

When rules conflict, the more specific selector wins: inline styles beat IDs, IDs beat classes, attributes and pseudo-classes, and those beat element selectors. With equal specificity, the later rule wins.

Q14. What does !important do, and why avoid it?

It makes a declaration win over normal rules regardless of specificity. Overusing it makes styles hard to override and debug, so keep it for utilities or third-party overrides.

Q15. What is the difference between em, rem, %, vh and px?

px is fixed. em is relative to the element's font size and rem to the root font size. % is relative to the parent, and vh/vw to the viewport's height and width.

Q16. What is margin collapsing?

The vertical margins of adjacent block elements merge into a single margin equal to the larger one. It does not happen in Flexbox or Grid layouts, or with padding or borders between them.

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

display: none removes the element from the layout entirely. visibility: hidden hides it but keeps its space.

Q18. What are pseudo-classes and pseudo-elements?

Pseudo-classes such as :hover and :nth-child() select elements in a certain state or position. Pseudo-elements such as ::before and ::after style a part of an element or add generated content.

Q19. What is the cascade in CSS?

The process that decides which declaration applies when several match. It weighs origin and importance, cascade layers, specificity and finally source order.

Q20. What are CSS custom properties?

Variables defined like --brand: #6d69b4 and used with var(--brand). They inherit, can change at runtime and per element, and suit theming such as dark mode.

Layout: Flexbox, Grid & Positioning

Q21. What is the difference between Flexbox and Grid?

Flexbox lays out items in one dimension, a row or a column, and suits toolbars and alignment. Grid lays out rows and columns together and suits page layouts and card grids.

Q22. How do you center an element horizontally and vertically?

On the parent, use display: flex; justify-content: center; align-items: center; or display: grid; place-items: center;.

Q23. What do justify-content and align-items do in Flexbox?

justify-content aligns items along the main axis, the row direction by default. align-items aligns them along the cross axis.

Q24. What does flex: 1 mean?

It is shorthand for flex-grow: 1; flex-shrink: 1; flex-basis: 0%, so the item grows and shrinks to share free space equally with other flex: 1 items.

Q25. What is the fr unit in CSS Grid?

A fraction of the free space in the grid container. grid-template-columns: 1fr 2fr gives the second column twice the space of the first.

Q26. What does repeat(auto-fill, minmax(200px, 1fr)) do?

It creates as many columns of at least 200px as fit, each sharing extra space equally. It makes a responsive grid without media queries.

Q27. What are the values of the position property?

static is the default. relative offsets an element from its normal place. absolute positions it against the nearest positioned ancestor. fixed positions it against the viewport. sticky scrolls normally until it reaches an offset.

Q28. How does z-index work?

It sets stacking order for positioned elements and flex or grid items. It only compares elements within the same stacking context, which properties such as opacity below 1 and transform create.

Q29. What does position: sticky need to work?

A top, bottom or other offset, and an ancestor that does not hide overflow. The element sticks only within its parent's bounds.

Q30. What is the difference between gap and margins for spacing?

gap adds space only between flex or grid items, never at the edges, and does not collapse. Margins can double up, collapse, or need edge cases to remove.

Responsive & Modern CSS

Q31. What is responsive design?

Making a page adapt to any screen size with flexible layouts, relative units, responsive images and media queries, so it works on phones, tablets and desktops.

Q32. What does the viewport meta tag do?

<meta name="viewport" content="width=device-width, initial-scale=1"> makes mobile browsers use the device width instead of a zoomed-out desktop width.

Q33. What is the mobile-first approach?

Writing base styles for small screens, then adding min-width media queries for larger ones. It keeps mobile CSS simple and loads the fewest overrides on phones.

Q34. What are container queries?

Rules written with @container that style a component based on the size of its container instead of the viewport, so it adapts wherever it is placed.

Q35. How do you make images responsive?

Use max-width: 100%; height: auto;, and srcset with sizes or the <picture> element so the browser downloads the right size and format.

Q36. What is the difference between transitions and animations?

A transition animates a property change between two states, such as on hover. A @keyframes animation can run on its own, loop and define many steps.

Q37. How do you support dark mode in CSS?

Define colours as custom properties and redefine them inside @media (prefers-color-scheme: dark). Setting color-scheme: dark also adapts form controls and scrollbars.

Q38. What is a CSS preprocessor?

A language such as Sass or Less that compiles to CSS and adds variables, nesting, mixins and functions. Native CSS now covers variables and nesting.

Q39. What is BEM?

Block Element Modifier, a class naming convention such as card__title--large. It keeps selectors flat and predictable and avoids specificity problems in large projects.

Q40. How do you improve CSS performance?

Remove unused CSS, keep critical CSS small, avoid animating layout properties like width (animate transform and opacity instead) and avoid very deep selectors.