Skip to content
RED SAGSkills

HTML interview questions and answers

HTML questions go beyond tag names. Interviewers check semantic markup, forms and accessibility, because the right element gives you keyboard support, screen reader support and better SEO for free.

Below are 60 HTML questions with the correct answer and a short explanation, grouped from fresher to experienced level. Try each one before you open the answer.

What HTML interviews usually check

  • ✓Semantic elements: header, nav, main, article and section
  • ✓Forms, labels, input types and validation
  • ✓Accessibility basics: alt text, headings and ARIA
  • ✓Meta tags, the document head and SEO basics
  • ✓Images, tables, links and buttons used correctly

HTML interview questions for freshers easy

1. Semantic element for site navigation

Which HTML element is the correct semantic choice to wrap the main menu links of a website?

  1. A.<div class="menu">
  2. B.<section>
  3. C.<nav>
  4. D.<aside>
Show answer

Answer: C. <nav>

The <nav> element marks a block of major navigation links. Screen readers expose it as a navigation landmark so users can jump to it directly. A div has no meaning, and section or aside describe different kinds of content.

2. Alt text for a decorative image

An image is purely decorative and adds no information. What is the best alt attribute for it?

  1. A.alt="" (empty)
  2. B.alt="image"
  3. C.Leave out the alt attribute completely
  4. D.alt="decorative background picture"
Show answer

Answer: A. alt="" (empty)

An empty alt tells screen readers to skip the image. If alt is missing, many screen readers read the file name instead, which is noise. Describing a decorative image wastes the user time, so empty alt is correct.

3. What 1rem is relative to

What is the computed padding of the paragraph?

html { font-size: 16px; }
.card { font-size: 20px; }
.card p { padding: 1rem; }
  1. A.20px
  2. B.32px
  3. C.It depends on the width of the viewport
  4. D.16px
Show answer

Answer: D. 16px

rem means "root em": it is always relative to the font size of the root html element, here 16px. The em unit is the one that follows the font size of the current element, which would give 20px. Using rem keeps spacing consistent and still respects the user changing the default font size.

4. What the main element is for

What should the <main> element wrap on a page?

  1. A.The site header, footer and navigation together
  2. B.The primary content unique to that page
  3. C.Every section that has a heading
  4. D.Only the scripts the page needs
Show answer

Answer: B. The primary content unique to that page

<main> marks the central content of the page, excluding repeated parts like the header, sidebar and footer. There should be one visible <main> per page. Screen reader users can jump straight to it.

5. article versus section

A blog list page shows ten post cards, each of which makes sense on its own and could be shared. Which element should wrap each card?

  1. A.<section>
  2. B.<aside>
  3. C.<div role="main">
  4. D.<article>
Show answer

Answer: D. <article>

<article> is for self-contained content that could stand alone, such as a post, a product card or a comment. <section> groups related content within a page, usually with a heading. <aside> is for side content.

6. The lang attribute

An Arabic page has <html lang="en"> by mistake. What is the main problem?

  1. A.Screen readers read the Arabic with English rules
  2. B.The page will not display any Arabic letters
  3. C.Search engines refuse to index it
  4. D.CSS stops working for the page
Show answer

Answer: A. Screen readers read the Arabic with English rules

lang tells assistive technology, translation tools and browsers which language the text is in. A wrong value makes screen readers mispronounce everything and can affect hyphenation and fonts. Use lang="ar" (with dir="rtl") for Arabic.

7. Sizing chips that sit in a line

Tag chips should sit side by side in a line of text but each have a fixed height and padding that pushes neighbours. Which display value fits?

  1. A.inline-block
  2. B.inline
  3. C.block
  4. D.table-caption
Show answer

Answer: A. inline-block

inline-block boxes flow inline like words, so they sit next to each other, but they also respect width, height and vertical padding and margin like blocks. Plain inline elements ignore width and height.

8. Spacing between flex items

You want 16px between every flex item but no extra space at the outer edges. What is the cleanest way?

  1. A.margin: 16px on every item
  2. B.padding: 16px on the container
  3. C.gap: 16px on the flex container
  4. D.justify-content: space-around
Show answer

Answer: C. gap: 16px on the flex container

gap adds space only between items, both in flexbox and grid, and it works with wrapping too. Margins on every item also add space at the edges, and space-around puts half gaps at the ends.

9. Nested em font sizes

The root font size is 16px. What is the font size of the inner span?

<div style="font-size: 1.5em">
  <span style="font-size: 1.5em">Total</span>
</div>
  1. A.24px
  2. B.36px
  3. C.16px
  4. D.32px
Show answer

Answer: B. 36px

For font-size, em is relative to the parent’s font size. The div is 1.5 × 16 = 24px, and the span is 1.5 × 24 = 36px. This compounding is why rem is often preferred for font sizes.

10. A ::before that does not show

Why is nothing shown for this rule?

.required::before {
  color: red;
  margin-right: 4px;
}
  1. A.::before only works on images
  2. B.It has no content property
  3. C.color is not allowed on pseudo-elements
  4. D.It must be written :before with one colon
Show answer

Answer: B. It has no content property

::before and ::after only generate a box when content is set, even content: "". Add content: "*" here. Note that ::before does not work on replaced elements such as <img> or <input>.

11. Images that overflow on phones

A 1200px wide image overflows its column on a 360px phone. Which rule fixes this for all images?

  1. A.img { width: 1200px; height: 800px }
  2. B.img { overflow: hidden; height: auto }
  3. C.img { max-width: 100%; height: auto }
  4. D.img { position: absolute; left: 0 }
Show answer

Answer: C. img { max-width: 100%; height: auto }

max-width: 100% lets the image shrink to its container but never grow past its natural size, and height: auto keeps the aspect ratio. overflow on the img itself does not resize it.

12. Striping table rows

Which selector shades the 1st, 3rd, 5th and so on rows of a table body?

  1. A.tbody tr:nth-child(2n)
  2. B.tbody tr:first-child
  3. C.tbody tr:nth-child(odd)
  4. D.tbody tr + tr
Show answer

Answer: C. tbody tr:nth-child(odd)

:nth-child(odd) is the same as :nth-child(2n+1), matching positions 1, 3, 5 and onward. 2n (or even) matches 2, 4, 6. tr + tr matches every row except the first.

13. A hover transition

Which CSS makes a button’s background fade from blue to dark blue over 200 ms on hover?

  1. A..btn { transition: background-color 200ms } plus a :hover colour
  2. B..btn:hover { animation: 200ms; background-color: darkblue }
  3. C..btn { transition: 200ms } with no hover rule at all
  4. D..btn:hover { transition-delay: 200ms }
Show answer

Answer: A. .btn { transition: background-color 200ms } plus a :hover colour

A transition animates a property between two values when it changes, so you need the transition on the element and a rule that changes the value, such as :hover. Putting transition on the base rule makes it animate both in and out.

14. A loading spinner that never stops

A spinner uses @keyframes spin { to { transform: rotate(360deg) } }. Which declaration keeps it spinning?

  1. A.animation: spin 1s linear once
  2. B.transition: spin 1s infinite
  3. C.animation: spin 1s linear infinite
  4. D.animation-name: spin; animation-duration: 0
Show answer

Answer: C. animation: spin 1s linear infinite

animation applies keyframes by name with a duration, a timing function and an iteration count. infinite repeats forever, and linear keeps the speed even. transition cannot run keyframes.

15. Hover effects on touch screens

A card reveals buttons only on :hover, so phone users never see them. Which media query targets devices that can really hover?

  1. A.@media (max-width: 768px) and (screen)
  2. B.@media (orientation: landscape) only
  3. C.@media screen and (touch: none)
  4. D.@media (hover: hover) and (pointer: fine)
Show answer

Answer: D. @media (hover: hover) and (pointer: fine)

The hover and pointer media features describe the main input device. Use them to add hover-only reveals for mouse users while keeping the buttons visible by default for touch users. Screen width does not tell you whether a device can hover.

16. Button or link

A control opens a modal dialog on the same page without changing the URL. Which element should it be?

  1. A.A <button>
  2. B.An <a href="#">
  3. C.A <div> with onclick
  4. D.A <span> with a cursor style
Show answer

Answer: A. A <button>

Links navigate to a new location; buttons perform actions such as opening dialogs or submitting forms. Using the right element gives screen reader users accurate expectations and the right keyboard behaviour (Space activates buttons).

17. Grouping radio buttons

A form has three radio buttons labelled 'Standard', 'Express' and 'Same day' under the question 'Delivery speed'. What markup best ties the question to the group?

  1. A.A <div> with bold text above
  2. B.A placeholder on each radio
  3. C.A <table> around the radios
  4. D.<fieldset> with a <legend>
Show answer

Answer: D. <fieldset> with a <legend>

A <fieldset> with a <legend> groups related controls, and screen readers announce the legend with each option, for example 'Delivery speed, Standard, radio button'. Plain text above the group is not linked to the inputs.

18. The main landmark

How many visible <main> elements should a typical web page have?

  1. A.One per section
  2. B.None, divs are enough
  3. C.One per heading
  4. D.One
Show answer

Answer: D. One

A page should have one visible <main> element containing its primary content. Screen reader users can jump straight to it with landmark navigation, and it also supports skip links.

19. Marking a field as required

What is the simplest way to tell assistive technology that an email input is mandatory?

  1. A.Add a red asterisk image only
  2. B.Add the required attribute
  3. C.Make the label text bold
  4. D.Add title="must fill"
Show answer

Answer: B. Add the required attribute

The HTML required attribute exposes the required state to assistive technologies and enables browser validation. A visible indicator, such as an asterisk explained at the top of the form, should accompany it for sighted users.

20. Landmark for the site footer

Which landmark role does a page-level <footer> (not inside an article or section) map to?

  1. A.banner
  2. B.contentinfo
  3. C.complementary
  4. D.region
Show answer

Answer: B. contentinfo

A top-level <footer> maps to the contentinfo landmark, which usually holds copyright, contact and policy links. A top-level <header> maps to banner and <aside> to complementary.

HTML intermediate interview questions medium

21. What showModal gives you

You open a confirmation using the native <dialog> element with dialog.showModal(). Which behaviour do you get from the browser without extra code?

  1. A.The dialog is automatically centred and animated with a fade-in transition
  2. B.Form data inside the dialog is saved to localStorage
  3. C.The dialog closes itself after a timeout if the user does nothing
  4. D.Content outside becomes inert, focus moves into the dialog, and Escape closes it
Show answer

Answer: D. Content outside becomes inert, focus moves into the dialog, and Escape closes it

showModal places the dialog in the top layer, makes the rest of the page inert so focus cannot leave, moves focus inside and closes on Escape. It also exposes the right dialog role to assistive technology. You still decide which element gets initial focus and style the ::backdrop yourself.

22. Images that push content down

Text jumps down as product images load, hurting CLS. What is the simplest fix?

  1. A.Add width and height attributes to each img
  2. B.Add loading="eager" to each img
  3. C.Move the images to CSS backgrounds
  4. D.Convert the images to PNG
Show answer

Answer: A. Add width and height attributes to each img

With width and height attributes, the browser knows the aspect ratio before the file arrives and reserves the right space. CSS like width: 100%; height: auto still makes the image responsive. Without them, the box starts at zero height and grows.

23. Order of async scripts

Two scripts are loaded with async: <script async src="lib.js"> then <script async src="app.js">. app.js uses lib.js. What can go wrong?

  1. A.Neither script runs until the page fully loads
  2. B.async scripts block HTML parsing
  3. C.The browser merges them into one file
  4. D.app.js may run first if it downloads first
Show answer

Answer: D. app.js may run first if it downloads first

async scripts download in parallel and each runs as soon as it arrives, in no fixed order. That suits independent scripts like analytics. For scripts that depend on each other, use defer (runs in document order) or ES modules.

24. target versus currentTarget

A click listener is on a <button> that contains an <svg> icon. The user clicks the icon. What are event.target and event.currentTarget?

  1. A.Both of them are the button element
  2. B.target is the button; currentTarget is the svg
  3. C.Both of them are the svg icon element
  4. D.target is the svg part; currentTarget is the button
Show answer

Answer: D. target is the svg part; currentTarget is the button

event.target is the deepest element actually clicked, which can be the icon or its path. event.currentTarget is the element whose listener is running. Reading data attributes from target instead of currentTarget is a common bug.

25. form.submit versus requestSubmit

A custom "Save" control calls form.submit(). The required fields are skipped and the submit handler never runs. What should it call instead?

  1. A.form.dispatchEvent(new Event("click"))
  2. B.form.reset()
  3. C.form.requestSubmit()
  4. D.form.checkValidity() alone
Show answer

Answer: C. form.requestSubmit()

form.submit() submits directly, skipping constraint validation and the submit event. form.requestSubmit() behaves like clicking a submit button: it validates, fires submit (so your handler can preventDefault) and then submits.

26. Skip link at the top

Keyboard users must press Tab 40 times to get past the mega menu on every page. What is the standard fix?

  1. A.A "Skip to main content" link as the first focusable element
  2. B.Remove the menu links from the tab order with tabindex="-1"
  3. C.Add aria-hidden="true" to the menu
  4. D.Tell users to use the mouse for the menu
Show answer

Answer: A. A "Skip to main content" link as the first focusable element

A skip link at the start of the page jumps focus to <main>. It can be visually hidden until focused. Removing links from the tab order or hiding them would make the menu unusable for keyboard and screen reader users.

27. Inline style versus !important

An element has style="color: blue" and a stylesheet rule .note { color: red !important }. Which colour shows?

  1. A.Blue
  2. B.Red
  3. C.Purple
  4. D.The browser default
Show answer

Answer: B. Red

Inline styles beat normal stylesheet rules, but an !important declaration beats normal inline styles. Only an inline !important would win over it. Overusing !important makes CSS hard to change, so keep it for utilities and overrides.

28. :is() versus :where()

A design system wants base link styles that any app class can override easily. Why use :where(nav, footer) a instead of :is(nav, footer) a?

  1. A.:where() is supported in more browsers
  2. B.:where() matches more elements than :is()
  3. C.:is() cannot take a list of selectors
  4. D.:where() adds zero specificity
Show answer

Answer: D. :where() adds zero specificity

Both match the same elements. :is() takes the specificity of its most specific argument, while :where() always counts as zero. So :where(nav, footer) a has specificity (0, 0, 1), and any class rule overrides it.

29. Styling a card that has an image

Cards that contain an image should get a different padding. Which pure CSS selector does this?

  1. A..card:has(img)
  2. B..card > img:parent
  3. C.img < .card
  4. D..card:contains(img)
Show answer

Answer: A. .card:has(img)

:has() matches an element if the selector inside it matches something relative to it, so .card:has(img) selects cards that contain an img. It is supported in all modern browsers. :parent, < and :contains() are not valid CSS.

30. A featured card that spans two columns

In a 3-column grid, a featured product card should be twice as wide as the others. Which rule goes on that card?

  1. A.grid-row: span 2
  2. B.grid-column: span 2
  3. C.width: 200%
  4. D.flex: 2
Show answer

Answer: B. grid-column: span 2

grid-column: span 2 makes the item cover two column tracks from wherever it is placed. grid-row: span 2 would make it taller instead, and width: 200% would overflow its cell rather than take a second track.

31. Column width with gaps

A 640px-wide grid has grid-template-columns: repeat(3, 1fr) and column-gap: 20px. How wide is each column?

  1. A.213.33px
  2. B.193.33px
  3. C.220px
  4. D.200px
Show answer

Answer: D. 200px

Gaps are taken out before fr units share the space. Three columns have two gaps: 640 − 2 × 20 = 600px, and 600 ÷ 3 = 200px per column.

32. Moving an element with position: relative

An icon gets position: relative; top: 4px. What happens to the elements around it?

  1. A.They move down 4px with the icon
  2. B.They move up into the icon’s old space
  3. C.They stay put; the icon’s original space is kept
  4. D.The parent grows by 4px
Show answer

Answer: C. They stay put; the icon’s original space is kept

Relative positioning moves the box visually but layout still treats it as being in its original place. Neighbours do not move, and the element may overlap them. It also makes the element a containing block for absolute children.

33. Spacing that flips for Arabic

A site serves English and Arabic (dir="rtl"). An icon needs space between it and the text that follows. Which property flips correctly with direction?

  1. A.margin-right
  2. B.margin-inline-end
  3. C.margin-left
  4. D.padding-block-end
Show answer

Answer: B. margin-inline-end

Logical properties follow the writing direction: inline-end is the right side in English and the left side in Arabic. Physical properties like margin-right stay on the right and look wrong in RTL layouts.

34. Focus rings only for the keyboard

Designers dislike focus rings after mouse clicks, but keyboard users need them. What is the right CSS?

  1. A.button:focus { outline: none } everywhere
  2. B.Use :hover instead of :focus for the ring
  3. C.Use :active to show the ring only while pressed
  4. D.Style :focus-visible and leave the ring for keyboard focus
Show answer

Answer: D. Style :focus-visible and leave the ring for keyboard focus

:focus-visible matches when the browser decides a focus indicator is helpful, mainly for keyboard focus, and usually not for mouse clicks on buttons. Removing outlines with :focus breaks navigation for keyboard users.

35. Reading a BEM class name

In BEM naming, what does card__title--large describe?

  1. A.A large card block that contains a title block
  2. B.The card block’s title element, large variant
  3. C.Two classes, card and title-large
  4. D.A card that is a child of a title
Show answer

Answer: B. The card block’s title element, large variant

BEM is block__element--modifier. card is the block, title is an element inside it, and large is a variation. The flat, explicit names keep specificity low and make it clear where a style belongs.

36. Anchor target hidden under a header

Clicking a link to #pricing scrolls the section heading under a 72px sticky header. What is the simplest fix?

  1. A.margin-top: -72px on the sticky header
  2. B.scroll-margin-top: 72px on the target sections
  3. C.z-index: -1 on the sticky header element
  4. D.A setTimeout that scrolls the page up 72px
Show answer

Answer: B. scroll-margin-top: 72px on the target sections

scroll-margin-top adds space above an element when it is scrolled into view, by anchor links or scrollIntoView, without changing layout. Setting it to the header height stops headings landing underneath. scroll-padding-top on the scroll container also works.

37. Users stuck on old version

A service worker uses cache-first for every request, including index.html. After a deploy, users still see the old site. Why?

  1. A.The browser deletes caches after each deploy
  2. B.Cache-first never stores HTML documents
  3. C.The CDN refuses to serve the new files
  4. D.index.html is always served from the old cache
Show answer

Answer: D. index.html is always served from the old cache

With cache-first for the HTML, the old page is served before the network is ever asked, and it links to old bundles. Use network-first or stale-while-revalidate for HTML, and version the service worker caches.

38. Autocomplete tokens on personal fields

Which change helps meet WCAG 1.3.5 Identify Input Purpose on a sign-up form?

<input id="email" name="user_mail" type="text">
  1. A.Rename the field to 'email-field'
  2. B.Add autocomplete="email" to the input
  3. C.Add a placeholder with an example
  4. D.Wrap the input in a fieldset
Show answer

Answer: B. Add autocomplete="email" to the input

1.3.5 asks that inputs collecting user information use the right autocomplete tokens, such as name, email or tel. Browsers can then autofill, and assistive tools can add icons, which helps people with memory or motor difficulties.

39. Two navigation menus on a page

A page has a main menu and a footer menu, both in <nav> elements. How can screen reader users tell them apart in a landmarks list?

  1. A.Give each nav a unique aria-label
  2. B.Use <div> for the footer menu
  3. C.Put role="menu" on both
  4. D.Add the same title to each
Show answer

Answer: A. Give each nav a unique aria-label

When a landmark type appears more than once, each instance should have a unique accessible name, such as aria-label="Main" and aria-label="Footer". Avoid repeating the word 'navigation', as the role is already announced.

40. Naming a data table

A page has three tables. How can each be given a name that screen readers announce when users reach it?

  1. A.A <caption> inside each table
  2. B.A bold paragraph above each table
  3. C.A title attribute on each <td>
  4. D.An <h6> inside the first cell
Show answer

Answer: A. A <caption> inside each table

The <caption> element gives a table an accessible name, announced when users enter the table and visible to everyone. It helps users choose the right table without reading its contents.

HTML interview questions for experienced candidates hard

41. Old version after deploy with a service worker

Your service worker serves index.html with a stale-while-revalidate strategy. After a deploy, users see the old app on the first reload and the new one on the second. Why?

  1. A.The browser HTTP cache ignores service workers for HTML files
  2. B.The worker answers immediately from the cache and fetches the new HTML in the background to update the cache for next time
  3. C.stale-while-revalidate always waits for the network before responding
  4. D.Service workers can only update once every 24 hours
Show answer

Answer: B. The worker answers immediately from the cache and fetches the new HTML in the background to update the cache for next time

Stale-while-revalidate trades freshness for speed: it returns the cached copy at once and refreshes the cache in the background. That is great for avatars or non-critical data, but for the HTML shell it means one stale visit after each deploy. Use network-first for HTML or show an "update available" prompt when a new worker is waiting.

42. aria-hidden on a parent of a button

A whole off-screen cart drawer has aria-hidden="true", but its buttons can still be reached with Tab. What is the problem?

  1. A.aria-hidden makes the buttons unclickable for mouse users
  2. B.The drawer cannot be shown again later
  3. C.Users focus buttons that screen readers announce as nothing
  4. D.aria-hidden is not valid on div elements
Show answer

Answer: C. Users focus buttons that screen readers announce as nothing

aria-hidden removes content from the accessibility tree but does not stop keyboard focus. Screen reader users then land on silent, invisible controls. Use the inert attribute or display: none on closed drawers so their contents cannot be focused.

43. Focus after client-side navigation

In a single-page app, clicking a nav link swaps the content but keyboard focus stays on the old link and screen readers say nothing. What is the usual fix?

  1. A.Move focus to the new heading and update the title
  2. B.Reload the full page on every navigation
  3. C.Add aria-live="assertive" to the whole body element
  4. D.Blur the link so nothing has focus
Show answer

Answer: A. Move focus to the new heading and update the title

Full page loads reset focus and announce the new title, but client-side routing does not. Moving focus to the new h1 (with tabindex="-1") or main region and updating document.title tells users where they are. A live region over the whole page would be noisy.

44. Global CSS and a shadow root

A web component renders a <button> inside an open shadow root. The page stylesheet has button { background: red }. What happens?

  1. A.The button turns red like any other button
  2. B.The rule does not reach the button inside the shadow root
  3. C.The rule applies only if the component is in <body>
  4. D.The page stylesheet is copied into the shadow root
Show answer

Answer: B. The rule does not reach the button inside the shadow root

Shadow DOM scopes styles: page selectors do not match elements inside a shadow root, and the component’s styles do not leak out. Inherited properties like color and font still pass through, and custom properties and ::part() let the page theme it on purpose.

45. What is inside a template tag

A <template id="row"> holds a <tr> with an <img src="a.png">. What happens before you clone it?

  1. A.The row is shown at the end of the page
  2. B.The image downloads, but the row is hidden
  3. C.The browser throws because tr needs a table
  4. D.Nothing renders and the image is not fetched
Show answer

Answer: D. Nothing renders and the image is not fetched

Template content is parsed into an inert DocumentFragment: it is not rendered, images do not load and scripts do not run. You clone it with template.content.cloneNode(true) and insert the copy, which then behaves normally.

46. A field that stays invalid

A form shows "Passwords do not match" forever, even after the user fixes it. What is missing?

confirm.addEventListener("input", () => {
  if (confirm.value !== pass.value) {
    confirm.setCustomValidity("Passwords do not match")
  }
})
  1. A.A call to confirm.reportValidity() at load time
  2. B.The novalidate attribute on the form
  3. C.An else branch calling confirm.setCustomValidity("")
  4. D.A required attribute on the confirm field
Show answer

Answer: C. An else branch calling confirm.setCustomValidity("")

Any non-empty custom validity message keeps the field invalid. You must reset it with setCustomValidity("") when the value becomes valid. Otherwise the form will never submit.

47. A parent that ignores floated children

A container holds only floated images and collapses to zero height, so its border does not wrap them. What is the modern fix?

  1. A.height: auto on the container
  2. B.float: none on the container
  3. C.display: flow-root on the container
  4. D.position: relative on the container
Show answer

Answer: C. display: flow-root on the container

Floats are taken out of normal flow, so a parent that only has floats has no in-flow height. display: flow-root makes the parent a new block formatting context, which contains its floats, without the old clearfix hack.

48. What padding-top: 50% is based on

A box inside a 400px wide, 1000px tall container has padding-top: 50%. How tall is that padding?

  1. A.200px
  2. B.500px
  3. C.50% of the box’s own height
  4. D.0px, since percentage padding needs a fixed height
Show answer

Answer: A. 200px

Percentage padding and margin, even on top and bottom, are based on the containing block’s width. So 50% of 400px is 200px. This was the old trick for fixed aspect ratios, now replaced by the aspect-ratio property.

49. A fixed modal that scrolls away

A modal with position: fixed sits inside a wrapper with transform: translateX(0). The modal now scrolls with the page and is clipped. Why?

  1. A.position: fixed is not supported inside divs
  2. B.The transformed wrapper becomes its containing block
  3. C.transform turns every child into position: static instead
  4. D.The modal needs z-index above 1000
Show answer

Answer: B. The transformed wrapper becomes its containing block

An ancestor with transform, filter, perspective or certain contain values becomes the containing block for fixed descendants. The modal is then fixed to that wrapper, not the viewport. Render modals near the end of body (for example with a portal).

50. Aligning titles across cards

Cards in a grid each have a title, text and a button. Long titles make buttons sit at different heights across a row. Which CSS feature lines up the inner parts of every card?

  1. A.align-items: baseline on the grid
  2. B.height: 100% on every card
  3. C.grid-template-rows: subgrid on each card
  4. D.display: table-cell on each card
Show answer

Answer: C. grid-template-rows: subgrid on each card

With subgrid, a card that spans several rows of the parent grid uses those parent rows for its own children. Titles, text and buttons across cards then share the same row tracks and line up. Subgrid is supported in all modern browsers.

51. What dense packing does

A photo gallery uses grid-auto-flow: dense to fill holes left by wide images. What is the side effect?

  1. A.Images are resized to fit the holes
  2. B.Rows become fixed at 100px
  3. C.Items can appear out of their DOM order
  4. D.Wide images are skipped
Show answer

Answer: C. Items can appear out of their DOM order

dense lets the grid place later items into earlier gaps, so the visual order can differ from the source order. That is fine for a photo wall, but for content that is read or tabbed through it breaks the expected order.

52. Animating a gradient angle

A transition on --angle in background: conic-gradient(from var(--angle), ...) jumps instead of animating smoothly. What makes it animate?

  1. A.Add will-change: background to the element
  2. B.Use transition: all instead of naming --angle
  3. C.Write the angle values in radians, not degrees
  4. D.Register --angle with @property as an <angle>
Show answer

Answer: D. Register --angle with @property as an <angle>

Unregistered custom properties are just token strings, so the browser cannot interpolate them and flips at the midpoint. @property --angle { syntax: "<angle>"; inherits: false; initial-value: 0deg } gives it a type, so it can be transitioned and animated.

53. Centring an absolute box with auto margins

A 300×200 dialog box has position: absolute; inset: 0. What else centres it inside its positioned parent?

  1. A.text-align: center on the dialog
  2. B.width: 300px; height: 200px; margin: auto
  3. C.vertical-align: middle on the parent
  4. D.float: left; margin: 50%
Show answer

Answer: B. width: 300px; height: 200px; margin: auto

With all four insets set and a fixed width and height, the leftover space is shared by auto margins on both axes, centring the box. This is the classic way to centre an absolute element without transforms.

54. Arabic and English user comments

Comments on a UAE site can be in Arabic or English, and Arabic ones show punctuation in the wrong place. What fixes direction per comment?

  1. A.dir="rtl" on the whole page
  2. B.text-align: right on all comments
  3. C.unicode-bidi: normal on the body
  4. D.dir="auto" on each comment element
Show answer

Answer: D. dir="auto" on each comment element

dir="auto" makes the browser pick the direction from the first strong character in that element, so Arabic comments become RTL and English ones LTR. text-align only moves the text; it does not fix the order of punctuation and mixed words.

55. Percentages in left versus translate

A 200px wide tooltip inside a 1000px wide parent has position: absolute; left: 50%; transform: translateX(-50%). Where is its left edge?

  1. A.At 500px from the parent’s left edge
  2. B.At 400px from the parent’s left edge
  3. C.At 0px, because the two cancel out
  4. D.At 300px from the parent’s left edge
Show answer

Answer: B. At 400px from the parent’s left edge

left: 50% is relative to the containing block, so it places the left edge at 500px. translateX(-50%) is relative to the element’s own width, moving it back 100px. The edge ends at 400px and the tooltip is centred.

56. Visual order changed with CSS

A developer uses CSS order on a flex container to show the price above the product name, while the DOM has name, then description, then price. A screen reader reads them in DOM order. When does this become a WCAG 1.3.2 failure?

.card { display: flex; flex-direction: column; }
.price { order: -1; }
  1. A.Whenever the CSS order property is used
  2. B.When the order change alters the meaning
  3. C.Only if the price is in a table
  4. D.Never, as screen readers ignore CSS
Show answer

Answer: B. When the order change alters the meaning

1.3.2 Meaningful Sequence fails only when the reading order differs in a way that changes meaning. Large differences between visual and DOM order also confuse keyboard users, so keeping the DOM in a logical order is the safest approach.

57. A live region added with its message

A script inserts <div aria-live="polite">Saved</div> into the page at the moment it wants the message read. Many screen readers stay silent. Why?

  1. A.Polite regions are never announced
  2. B.The region should already exist, then get the text
  3. C.The div must also be focusable
  4. D.The text must be longer than 10 characters
Show answer

Answer: B. The region should already exist, then get the text

Live regions are most reliable when the empty container is in the DOM before its content changes; screen readers then notice the update. Inserting a region and its text together is often missed, except with role="alert" in some browsers.

58. aria-label on a plain div

A developer puts aria-label="Product details" on a <div> with no role. What is the result?

  1. A.It becomes a region landmark
  2. B.It is read out as a heading
  3. C.It is not supported and often ignored
  4. D.It is read before every child
Show answer

Answer: C. It is not supported and often ignored

Generic elements like a plain <div> or <span> do not support naming, so aria-label is prohibited there and is often ignored. Give it a suitable role, such as region or group, or use a visible heading instead.

59. role="presentation" on a table

A developer adds role="presentation" to a <table> that holds a real data grid. What do screen reader users lose?

  1. A.Only the table's border styling
  2. B.Nothing, as tables ignore the role
  3. C.Table semantics such as headers
  4. D.Only the ability to tab to cells
Show answer

Answer: C. Table semantics such as headers

role="presentation" (or none) removes the element's implicit semantics, and for tables the row and cell structure too. Users can no longer navigate by cell or hear headers, so it suits only layout tables.

60. Making layout tables accessible

A screen reader reads your email as "table with 5 rows and 3 columns" again and again. What fixes this?

  1. A.Add role="presentation" to the layout tables
  2. B.Replace all text with images that have alt text
  3. C.Add a caption to every layout table
  4. D.Set the font size of every table to zero
Show answer

Answer: A. Add role="presentation" to the layout tables

role="presentation" tells assistive technology that the table is for layout, not data, so it reads the content without announcing rows and columns. Also set lang on the html element and use real text for headings.

Ready to test yourself?

Take a 15-question HTML MCQ test with a score at the end, or practise the full HTML question bank and earn points on the leaderboard.