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?
- <h6>
- <h1>
- <header>
- <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?
- title
- alt
- name
- 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>- The link opens in a new tab, with security protection
- The link downloads a file
- The link is disabled
- 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"?
- #box
- .box
- box
- *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)?
- Content, Border, Padding, Margin
- Content, Padding, Border, Margin
- Padding, Content, Margin, Border
- 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; }- top/bottom 20px, left/right 10px
- top/bottom 10px, left/right 20px
- 10px everywhere
- 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?
<label name="x">and<input id="x"><label for="x">and<input id="x"><label id="x">and<input for="x">- 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?
- em
- rem
- px
- 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?
- <div>
- <p>
- <span>
- <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?
- It loads the CSS file
- It switches on the browser's standards mode (no quirks mode)
- It defines the language
- 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>- A bulleted list with dots
- A numbered list (1. A, 2. B)
- A definition list
- 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?
- No difference
noneremoves the element from the layout,hiddenonly hides it and leaves the space freehiddenremoves the element,noneleaves space freenoneonly 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;- Blue
- Green
- Red
- 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>- Red
- Blue
- Green
- 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;
}- 200px
- 240px
- 250px
- 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;
}- 200px
- 250px
- 240px
- 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)?
- Centers all items
- Distributes items so that the first is at the start, the last at the end and the rest evenly in between
- Stretches the items in height
- 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?
- Along the main axis (horizontal)
- Along the cross axis (vertical)
- Only in Grid
- 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?
- The item is exactly 1px wide
- The item grows and shrinks and shares free space evenly (1 1 0%)
- The item is hidden
- 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);
}- Three rows
- Three equal-width columns
- Three columns 1px wide
- 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));- It makes the grid automatically responsive: as many columns as fit at a minimum of 200px
- It limits the grid to 200 elements
- It creates exactly one column
- 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; }
}- Only at exactly 600px
- From a window width of 600px and larger
- Below 600px
- 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?
- The page is built for smartphones only
- Base styles apply to small screens, larger layouts are added via
min-widthqueries - Desktop styles are overridden with
max-width - 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?
- The browser window
- The nearest ancestor with
positionother thanstatic - Always the
<body> - 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) */- 20px
- 30px
- 50px
- 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?
- All list items
- The 1st, 3rd, 5th … item
- Only the first item
- 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>?
- So that smartphones render the page at the real device width instead of as a shrunken desktop view
- So that the page loads faster
- So that search engines find it
- 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?
- For zooming
- For the stacking order of overlapping, positioned elements (and Flex/Grid items)
- For font size
- 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);- 10px
- 16px
- 32px
- 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); }- The color stays default black
- The fallback
tomatois used - There is an error
- 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?
- No, only
:rootknows them - Yes, they are inherited by default and can be overridden in subtrees
- Only with
!important - 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?
- width and height
- transform and opacity
- margin and padding
- 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?
- The user wants as little animation/motion as possible
- The screen is small
- The user has enabled dark mode
- 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?
- Selects images in cards
- Selects elements with class
kartethat contain animg - Selects all cards except those with an image
- 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; }- 15px
- 20px
- 30px
- 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?
- ARIA is always better than native elements
- Use native elements first (e.g.
<button>); ARIA only when HTML is not enough - ARIA replaces CSS
- 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?
- 2:1
- 3:1
- 4,5:1
- 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?
- It makes the button slower
- Keyboard users can no longer see where the focus is
- It disables the button
- 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?
- For media queries on print media
- For applying styles depending on the size of a parent container instead of the window
- For defining container classes
- 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?
- The value is too high
- An ancestor forms its own stacking context (e.g. through
transformoropacity < 1) - z-index only exists in Flexbox
- 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?
- Creates layers in graphics
- Orders the cascade into layers; later layers win regardless of specificity
- Defines animations
- 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.