Your cart is empty

Continue shopping

Your cart

HTML and CSS Interview Questions

Top 20 HTML & CSS Interview Questions

HTML Questions

1. What is semantic HTML and why does it matter?

Semantic HTML uses elements that describe their meaning, such as <header>, <nav>, <main>, <article>, <section>, <aside>, and <footer>, instead of generic <div> and <span> elements. It improves accessibility because screen readers can navigate by landmarks, helps SEO because search engines understand page structure, and makes code easier to read and maintain.

2. What is the purpose of the DOCTYPE declaration?

<!DOCTYPE html> tells the browser to render the page in standards mode using HTML5 rules. Without it, browsers fall back to quirks mode, which emulates old, inconsistent behavior (for example, a different box model calculation). It must be the first line of the document and is not an HTML tag.

3. What is the difference between block, inline, and inline-block elements?

Block elements (div, p, h1, section) start on a new line and take the full available width; width, height, and all margins/padding apply. Inline elements (span, a, strong) flow within text, take only the width of their content, and ignore width/height and vertical margins. Inline-block elements flow inline like text but respect width, height, and vertical spacing, which is useful for buttons and badges.

4. What are data attributes and how are they used?

Data attributes (data-*) store custom information on HTML elements, for example <div data-user-id="42">. JavaScript reads them through element.dataset.userId, and CSS can select them with attribute selectors such as [data-state="open"]. They keep extra data in valid HTML without misusing class names or non-standard attributes.

5. What is the difference between script, script async, and script defer?

A plain <script> blocks HTML parsing while it downloads and runs. async downloads in parallel and runs as soon as it is ready, possibly before parsing finishes and in no guaranteed order, which suits independent scripts like analytics. defer downloads in parallel and runs after parsing completes, in document order, which suits most application scripts that depend on the DOM. Module scripts (type="module") are deferred by default.

6. How do you make an HTML page accessible?

Use semantic elements and a logical heading order (h1 to h6), give images meaningful alt text, associate every form input with a <label>, ensure full keyboard navigation and visible focus styles, maintain sufficient color contrast, and set the lang attribute on <html>. Use ARIA attributes (aria-label, aria-expanded, role) only when native HTML cannot express the meaning: the first rule of ARIA is to prefer native elements.

7. What are the new form features in HTML5?

HTML5 added input types such as email, url, tel, number, range, date, color, and search, which give built-in validation and suitable mobile keyboards. It also added attributes like required, placeholder, pattern, min, max, step, autocomplete, and autofocus, plus elements like <datalist> and <output>. The Constraint Validation API lets JavaScript check validity and customize error messages.

8. What is the difference between localStorage, sessionStorage, and cookies?

localStorage keeps data with no expiration (about 5–10 MB per origin) until it is cleared. sessionStorage is similar but scoped to a single tab and cleared when that tab closes. Cookies are small (about 4 KB), can have expiration dates, and are sent to the server with every HTTP request, which makes them suitable for sessions and authentication. Cookies can be flagged HttpOnly, Secure, and SameSite for security; web storage is only accessible to JavaScript.

9. How do you optimize images in HTML?

Use modern formats (WebP, AVIF), serve responsive images with srcset and sizes or the <picture> element, add loading="lazy" for offscreen images, and always set width and height attributes so the browser reserves space and avoids layout shift (CLS). Use fetchpriority="high" on the main hero image to improve Largest Contentful Paint.

10. What are meta tags and which ones are important?

Meta tags in the <head> provide metadata about the page. Key ones: <meta charset="UTF-8"> for character encoding, <meta name="viewport" content="width=device-width, initial-scale=1"> for responsive design, the description meta tag for search result snippets, Open Graph and Twitter tags for social previews, and robots to control indexing. The <title> element, while not a meta tag, is equally important for SEO.

CSS Questions

11. Explain the CSS box model

Every element is a box made of content, padding, border, and margin, from inside out. With the default box-sizing: content-box, width and height apply only to the content, so padding and border add to the total size. With box-sizing: border-box, width and height include padding and border, which makes layouts more predictable. Most projects set border-box globally with *, *::before, *::after { box-sizing: border-box; }.

12. How does CSS specificity work?

Specificity decides which rule wins when several target the same element. It is calculated as (IDs, classes/attributes/pseudo-classes, elements/pseudo-elements): #nav is (1,0,0), .btn:hover is (0,2,0), div p is (0,0,2). Inline styles beat selectors, and !important overrides normal declarations. When specificity ties, the rule that appears later wins. :where() has zero specificity, while :is() and :not() take the specificity of their most specific argument.

13. What is the difference between Flexbox and CSS Grid?

Flexbox is one-dimensional: it lays items out in a row or a column and is ideal for navigation bars, aligning items, and distributing space within a component. Grid is two-dimensional: it controls rows and columns at the same time and is ideal for page layouts and card galleries. They work well together, for example Grid for the overall page and Flexbox inside each component.

14. Explain the different values of the position property

static is the default and follows normal flow. relative stays in flow but can be offset with top/left, and it becomes the reference for absolutely positioned children. absolute is removed from flow and positioned relative to the nearest positioned ancestor. fixed is positioned relative to the viewport and stays put while scrolling. sticky behaves like relative until a scroll threshold is reached, then sticks like fixed within its container.

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

The simplest modern approaches: on the parent, display: flex; justify-content: center; align-items: center; or display: grid; place-items: center;. For absolutely positioned elements: top: 50%; left: 50%; transform: translate(-50%, -50%);. For horizontal centering of a block with a width, use margin: 0 auto;. For text, use text-align: center.

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

Pseudo-classes (single colon) select elements based on state or position, such as :hover, :focus, :focus-visible, :nth-child(), :first-child, :checked, and :has(). Pseudo-elements (double colon) style a specific part of an element or insert content, such as ::before, ::after, ::first-line, ::placeholder, and ::selection. ::before and ::after require a content property to render.

17. What is responsive design and how do media queries work?

Responsive design makes layouts adapt to different screen sizes. Media queries apply styles conditionally, for example @media (min-width: 768px) { ... }. A mobile-first approach writes base styles for small screens and adds min-width queries for larger ones. Modern tools reduce the need for breakpoints: flexible units (%, rem, vw), clamp() for fluid typography, Grid's auto-fit with minmax(), and container queries (@container), which respond to a component's container size instead of the viewport.

18. What are CSS custom properties (variables)?

Custom properties are defined with two dashes, for example :root { --primary: #4CAF50; }, and used with var(--primary, fallback). Unlike Sass variables, they are live at runtime: they cascade, inherit, can be scoped to any element, and can be changed with JavaScript or media queries. This makes them ideal for theming, such as dark mode, and design tokens.

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

px is a fixed unit. em is relative to the element's own font size (or the parent's when used for font-size), so nested ems compound. rem is relative to the root (html) font size, which makes it predictable and accessible because it respects user font settings. % is relative to the parent's size. vh and vw are 1% of the viewport height and width; on mobile, dvh, svh, and lvh handle browser toolbars more reliably than vh.

20. What is z-index and what is a stacking context?

z-index controls stacking order on the z-axis for positioned elements and flex/grid items; higher values appear in front. A stacking context is an isolated layering group: z-index values only compete within the same context. New stacking contexts are created by elements with position plus a z-index, opacity below 1, transform, filter, isolation: isolate, and others. This explains why z-index: 9999 sometimes fails: the element is trapped inside a parent's stacking context.