</>HTML & CSS

HTML & CSS quiz – 41 questions with answers

Try it interactively

With scoring, random questions, and saved progress – or go straight to the timed exam with certificate.

Beginner (13 questions)

Which element stands for the most important heading of a page?

  1. <h6>
  2. <h1>
  3. <header>
  4. <title>
Show answer

Correct: <h1>

<h1> is the main heading (usually once per page). <title> is the page title in the browser tab, <header> a region element.

Which attribute is mandatory on <img> for accessibility?

  1. title
  2. alt
  3. name
  4. caption
Show answer

Correct: alt

alt provides the replacement text for screen readers and for when the image does not load. For purely decorative images you write alt="".

What does this code do?

<a href="https://example.com" target="_blank" rel="noopener">Link</a>
  1. The link opens in a new tab, with security protection
  2. The link downloads a file
  3. The link is disabled
  4. The link opens a popup window
Show answer

Correct: The link opens in a new tab, with security protection

target="_blank" opens a new tab; rel="noopener" prevents the new page from accessing window.opener.

Which selector selects all elements with class="box"?

  1. #box
  2. .box
  3. box
  4. *box
Show answer

Correct: .box

Classes are selected with a dot (.box), IDs with a hash (#box), elements without a symbol (box).

Which order of the box model layers is correct (from inside to outside)?

  1. Content, Border, Padding, Margin
  2. Content, Padding, Border, Margin
  3. Padding, Content, Margin, Border
  4. Margin, Border, Padding, Content
Show answer

Correct: Content, Padding, Border, Margin

From inside to outside: content, padding (inner spacing), border, margin (outer spacing).

How large are the spacings with this rule?

.box { margin: 10px 20px; }
  1. top/bottom 20px, left/right 10px
  2. top/bottom 10px, left/right 20px
  3. 10px everywhere
  4. only 10px on top and 20px on the left
Show answer

Correct: top/bottom 10px, left/right 20px

With two values: first value = top and bottom, second = left and right.

How do you link a <label> to an input field?

  1. <label name="x"> and <input id="x">
  2. <label for="x"> and <input id="x">
  3. <label id="x"> and <input for="x">
  4. It always happens automatically
Show answer

Correct: <label for="x"> and <input id="x">

for on the label points to the id of the field. Alternatively, the label directly wraps the field.

Which unit refers to the font size of the root element?

  1. em
  2. rem
  3. px
  4. vh
Show answer

Correct: rem

rem = "root em" (usually 16px). em refers to the font size of the current element or parent element.

Which element is an inline element by default?

  1. <div>
  2. <p>
  3. <span>
  4. <section>
Show answer

Correct: <span>

<span> is inline and flows within the text. <div>, <p> and <section> are block elements.

What is <!DOCTYPE html> at the start for?

  1. It loads the CSS file
  2. It switches on the browser's standards mode (no quirks mode)
  3. It defines the language
  4. It is a comment
Show answer

Correct: It switches on the browser's standards mode (no quirks mode)

Without a doctype, browsers render in "quirks mode" with outdated rules. The language is set by <html lang="en">.

What does this HTML code output for a list?

<ol>
  <li>A</li>
  <li>B</li>
</ol>
  1. A bulleted list with dots
  2. A numbered list (1. A, 2. B)
  3. A definition list
  4. Two paragraphs
Show answer

Correct: A numbered list (1. A, 2. B)

<ol> (ordered list) numbers the items, <ul> (unordered list) shows bullet points.

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

  1. No difference
  2. none removes the element from the layout, hidden only hides it and leaves the space free
  3. hidden removes the element, none leaves space free
  4. none only affects images
Show answer

Correct: none removes the element from the layout, hidden only hides it and leaves the space free

With display: none the element takes up no space and is also gone for screen readers; visibility: hidden keeps the space.

Which color does this value produce?

color: #ff0000;
  1. Blue
  2. Green
  3. Red
  4. Black
Show answer

Correct: Red

Hex colors write red, green, blue as two digits each: ff red, 00 green, 00 blue = pure red.

Intermediate (15 questions)

What color is the paragraph?

<style>
  p { color: red; }
  .t { color: blue; }
  #x { color: green; }
</style>
<p id="x" class="t">Text</p>
  1. Red
  2. Blue
  3. Green
  4. Black
Show answer

Correct: Green

The ID rule has the highest specificity (1,0,0) compared with class (0,1,0) and element (0,0,1).

How wide is the box in total (default content-box)?

.box {
  width: 200px;
  padding: 20px;
  border: 5px solid;
}
  1. 200px
  2. 240px
  3. 250px
  4. 225px
Show answer

Correct: 250px

200 + 2 × 20 (padding) + 2 × 5 (border) = 250px. With box-sizing: border-box it would be 200px.

How wide is the box with box-sizing: border-box?

.box {
  box-sizing: border-box;
  width: 200px;
  padding: 20px;
  border: 5px solid;
}
  1. 200px
  2. 250px
  3. 240px
  4. 210px
Show answer

Correct: 200px

With border-box, width includes padding and border: total width 200px, the content is only 150px wide.

What does justify-content: space-between do in a flex container (row)?

  1. Centers all items
  2. Distributes items so that the first is at the start, the last at the end and the rest evenly in between
  3. Stretches the items in height
  4. Sets a fixed gap of 1rem
Show answer

Correct: Distributes items so that the first is at the start, the last at the end and the rest evenly in between

space-between distributes the free space between the items; no space is left on the outside.

In which direction does align-items act with flex-direction: row?

  1. Along the main axis (horizontal)
  2. Along the cross axis (vertical)
  3. Only in Grid
  4. Not at all
Show answer

Correct: Along the cross axis (vertical)

align-items aligns on the cross axis; with row that is the vertical one. justify-content acts on the main axis.

What does flex: 1 mean on a flex item?

  1. The item is exactly 1px wide
  2. The item grows and shrinks and shares free space evenly (1 1 0%)
  3. The item is hidden
  4. The item gets rank 1 in the order
Show answer

Correct: The item grows and shrinks and shares free space evenly (1 1 0%)

flex: 1 stands for flex: 1 1 0% – the item takes its share of the available space.

What does this rule create?

.raster {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
}
  1. Three rows
  2. Three equal-width columns
  3. Three columns 1px wide
  4. A grid with three areas
Show answer

Correct: Three equal-width columns

repeat(3, 1fr) creates three columns that share the space in equal parts.

What is this pattern for?

grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  1. It makes the grid automatically responsive: as many columns as fit at a minimum of 200px
  2. It limits the grid to 200 elements
  3. It creates exactly one column
  4. It only works with Flexbox
Show answer

Correct: It makes the grid automatically responsive: as many columns as fit at a minimum of 200px

The browser picks the number of columns itself; each column is at least 200px wide and shares the rest in equal parts.

From which width does this rule apply?

@media (min-width: 600px) {
  .nav { flex-direction: row; }
}
  1. Only at exactly 600px
  2. From a window width of 600px and larger
  3. Below 600px
  4. Only on smartphones
Show answer

Correct: From a window width of 600px and larger

min-width: 600px means: the condition is met when the window is at least 600px wide.

What does "mobile first" mean?

  1. The page is built for smartphones only
  2. Base styles apply to small screens, larger layouts are added via min-width queries
  3. Desktop styles are overridden with max-width
  4. Images are loaded first
Show answer

Correct: Base styles apply to small screens, larger layouts are added via min-width queries

You start with the lean mobile view and extend it step by step for larger screens.

What does a position: absolute element orient itself to?

  1. The browser window
  2. The nearest ancestor with position other than static
  3. Always the <body>
  4. The previous sibling
Show answer

Correct: The nearest ancestor with position other than static

Without a positioned ancestor it is the initial containing block (window/document). That is why position: relative is often set on the parent element.

How large is the gap between the two blocks?

.a { margin-bottom: 20px; }
.b { margin-top: 30px; }
/* .a sits directly above .b (both block elements) */
  1. 20px
  2. 30px
  3. 50px
  4. 10px
Show answer

Correct: 30px

Vertical margins of block elements collapse: the larger of the two values applies, i.e. 30px.

Which elements does li:nth-child(odd) match?

  1. All list items
  2. The 1st, 3rd, 5th … item
  3. Only the first item
  4. The 2nd, 4th, 6th … item
Show answer

Correct: The 1st, 3rd, 5th … item

odd stands for odd positions (1, 3, 5 …), even for even ones. Equivalent to 2n+1 or 2n.

Why should <meta name="viewport" content="width=device-width, initial-scale=1"> be in the <head>?

  1. So that smartphones render the page at the real device width instead of as a shrunken desktop view
  2. So that the page loads faster
  3. So that search engines find it
  4. So that images get bigger
Show answer

Correct: So that smartphones render the page at the real device width instead of as a shrunken desktop view

Without the viewport tag, mobile browsers simulate a ~980px wide view, and media queries do not work as expected.

What is z-index for?

  1. For zooming
  2. For the stacking order of overlapping, positioned elements (and Flex/Grid items)
  3. For font size
  4. For aligning in rows
Show answer

Correct: For the stacking order of overlapping, positioned elements (and Flex/Grid items)

Higher z-index values sit further in front – but only if the element is positioned or is a Flex/Grid item.

Advanced (13 questions)

What is the font size at a viewport width of 400px? (1rem = 16px)

font-size: clamp(1rem, 2.5vw, 2rem);
  1. 10px
  2. 16px
  3. 32px
  4. 25px
Show answer

Correct: 16px

2.5vw = 10px is below the minimum 1rem = 16px, so 16px applies.

What happens here if --farbe is not defined?

.btn { color: var(--farbe, tomato); }
  1. The color stays default black
  2. The fallback tomato is used
  3. There is an error
  4. The rule is ignored completely
Show answer

Correct: The fallback tomato is used

The second parameter of var() is the fallback value in case the variable is not set.

Are custom properties (--x) inherited by child elements?

  1. No, only :root knows them
  2. Yes, they are inherited by default and can be overridden in subtrees
  3. Only with !important
  4. Only in grid containers
Show answer

Correct: Yes, they are inherited by default and can be overridden in subtrees

Custom properties are inheritable; that is exactly what makes local overrides and theming possible.

Which two properties can be animated most performantly?

  1. width and height
  2. transform and opacity
  3. margin and padding
  4. top and left
Show answer

Correct: transform and opacity

transform and opacity can be animated by the compositor without recalculating the layout; the others trigger reflow.

What does the media query prefers-reduced-motion: reduce stand for?

  1. The user wants as little animation/motion as possible
  2. The screen is small
  3. The user has enabled dark mode
  4. The page loads slowly
Show answer

Correct: The user wants as little animation/motion as possible

The system setting "Reduce motion". Such users should not see large movements, parallax or endless animations.

What does the selector .karte:has(img) do?

  1. Selects images in cards
  2. Selects elements with class karte that contain an img
  3. Selects all cards except those with an image
  4. It does not exist in CSS
Show answer

Correct: Selects elements with class karte that contain an img

:has() is the relational selector (effectively a parent selector): it matches the element that contains the given selector.

How is the font size of the <span> calculated?

.eltern { font-size: 20px; }
.eltern span { font-size: 1.5em; }
  1. 15px
  2. 20px
  3. 30px
  4. 24px
Show answer

Correct: 30px

em refers to the font size of the parent element: 1.5 × 20px = 30px. Nested em values multiply.

How do the ARIA rules relate to native HTML elements?

  1. ARIA is always better than native elements
  2. Use native elements first (e.g. <button>); ARIA only when HTML is not enough
  3. ARIA replaces CSS
  4. ARIA is only needed for images
Show answer

Correct: Use native elements first (e.g. <button>); ARIA only when HTML is not enough

The first rule of ARIA: if a native element exists, use it. It comes with role, keyboard operation and focus behavior.

Which minimum contrast ratio does WCAG AA require for normal text?

  1. 2:1
  2. 3:1
  3. 4,5:1
  4. 7:1
Show answer

Correct: 4,5:1

Normal text: 4.5:1 (AA), 7:1 (AAA). Large text and UI components: 3:1.

Why is button:focus { outline: none; } without a replacement problematic?

  1. It makes the button slower
  2. Keyboard users can no longer see where the focus is
  3. It disables the button
  4. It prevents clicks
Show answer

Correct: Keyboard users can no longer see where the focus is

Without a visible focus the page is barely usable by keyboard (WCAG 2.4.7). Better: define your own :focus-visible style.

What is @container meant for?

  1. For media queries on print media
  2. For applying styles depending on the size of a parent container instead of the window
  3. For defining container classes
  4. For Docker containers
Show answer

Correct: For applying styles depending on the size of a parent container instead of the window

Thanks to container-type and @container, components adapt to their placement, independent of the window width.

Why does z-index: 9999 sometimes have no effect?

  1. The value is too high
  2. An ancestor forms its own stacking context (e.g. through transform or opacity < 1)
  3. z-index only exists in Flexbox
  4. Browsers ignore values above 100
Show answer

Correct: An ancestor forms its own stacking context (e.g. through transform or opacity < 1)

Within a stacking context, comparison happens only among its members. The element can never rise above sibling contexts of higher rank.

What does @layer do in CSS?

  1. Creates layers in graphics
  2. Orders the cascade into layers; later layers win regardless of specificity
  3. Defines animations
  4. Groups HTML elements
Show answer

Correct: Orders the cascade into layers; later layers win regardless of specificity

With @layer basis, komponenten, utilities; you determine the priority of whole CSS groups – useful for frameworks and large projects.