HTML and CSS MCQ Questions and Answers: 40 Practice Questions with Explanations
By AbairTech Solution · From the team that designs and builds our client websites
Published by AbairTech Solution · Reviewed November 2026 · Editorial policy
How to use this page. Questions 1–12 cover HTML, 13–34 cover CSS and 35–40 cover performance and accessibility practices. Try each question before you open Show answer. The best way to check CSS answers is to change the property in your browser’s developer tools and watch what happens.
HTML and CSS study notes
HTML describes what content is – a heading, a paragraph, a navigation menu, a form. CSS describes how it looks. Keeping that split clean is what makes a site accessible, easy to restyle and understandable to search engines.
Semantic HTML. Elements such as <header>, <nav>, <main>, <article> and <footer> tell browsers, screen readers and search engines what each part of the page does. A page built only from <div>s looks the same but means much less.
The cascade. When rules conflict, the browser compares importance (!important), then specificity, then source order – the later rule wins a tie.
The box model. Every element is a box: content, then padding, then border, then margin. Setting box-sizing: border-box makes widths include padding and border, which makes layouts far easier to reason about.
Layout. Use Flexbox for one-dimensional layouts (a row of buttons, a navbar) and Grid for two-dimensional ones (a card gallery, a page skeleton).
Questions 1–12: HTML structure, semantics and forms
1. What does HTML stand for?
High Tech Modern Language
HyperText Markup Language
Hyperlink and Text Management Language
Home Tool Markup Language
Show answer
Answer: B. “Markup” means tags describe the structure of content; HTML is not a programming language.
2. What is the purpose of <!DOCTYPE html>?
It links the CSS file
It makes the page load faster
It is a comment
It tells the browser to render the page in standards mode
Show answer
Answer: D. Without it, browsers fall back to “quirks mode”, which imitates old browser bugs and can break layouts.
3. Which element should wrap a site’s main navigation links?
<nav>
<menu-bar>
<navigation>
<links>
Show answer
Answer: A. Screen-reader users can jump straight to the nav landmark. Only <nav> is a real HTML element among the options.
4. Why is the alt attribute on images important?
It sets the image size
It adds a tooltip in all browsers
It provides text for screen readers, search engines and when the image fails to load
It compresses the image
Show answer
Answer: C. Describe what the image shows. For purely decorative images, use an empty alt (alt="") so screen readers skip them.
5. How many <h1> headings should a typical page have?
None
One, describing the page’s main topic
One per paragraph
At least five
Show answer
Answer: B. HTML allows more, but one clear h1 followed by h2 and h3 in order gives the clearest outline for users and search engines.
6. Which of these elements is inline by default?
<div>
<p>
<section>
<span>
Show answer
Answer: D. Inline elements sit within a line of text and ignore width and height; block elements start on a new line and take the full width.
7. When a link opens in a new tab with target="_blank", what is good practice to add?
rel="noopener"
href="new"
type="tab"
window="new"
Show answer
Answer: A. It stops the new page from controlling the original through window.opener. Modern browsers apply this by default, but adding it is still common practice.
8. Which form method should be used to submit a login form?
GET
FETCH
POST
PUT, because it is a form
Show answer
Answer: C – POST. GET puts the data in the URL, where it ends up in browser history and server logs. Use HTTPS as well.
9. Which input type gives built-in email format validation and an email keyboard on phones?
type="text"
type="email"
type="mail"
type="address"
Show answer
Answer: B. Similarly, type="tel" shows a number pad. Always validate on the server too.
10. How do you link a <label> to an input with id="phone"?
<label name="phone">
<label id="phone">
<label input="phone">
<label for="phone">
Show answer
Answer: D. Screen readers then announce the label, and clicking the label focuses the input – a bigger tap target on mobile.
11. What does <meta name="viewport" content="width=device-width, initial-scale=1"> do?
Makes mobile browsers use the device width instead of a zoomed-out desktop width
Hides the page from search engines
Sets the page title
Loads the page faster
Show answer
Answer: A. Without it, media queries do not work as expected on phones and text appears tiny.
12. Which element suits a self-contained piece of content such as a blog post or news story?
<aside>
<div>
<article>
<span>
Show answer
Answer: C. An article makes sense on its own. <aside> is for related side content, such as a sidebar.
Questions 13–22: selectors, the box model, positioning and layout
13. Which selector has the highest specificity?
.menu
nav ul li a
#menu
a:hover
Show answer
Answer: C – #menu. One ID outweighs any number of classes and elements. Inline styles beat IDs, and !important beats both – use it sparingly.
14. What is the order of the box model, from inside to outside?
Content, padding, border, margin
Content, margin, border, padding
Padding, content, border, margin
Margin, border, padding, content
Show answer
Answer: A. Padding adds space inside the border (and takes the background colour); margin adds space outside it.
15. With box-sizing: border-box, an element has width: 300px; padding: 20px; border: 5px. How wide is it on screen?
350px
300px
340px
250px
Show answer
Answer: B – 300px. Padding and border fit inside the width. With the default content-box, it would be 300 + 40 + 10 = 350px.
16. An element with position: absolute is positioned relative to:
Its previous sibling
The browser window, always
Its normal position
Its nearest positioned ancestor (or the page if there is none)
Show answer
Answer: D. That is why a parent often gets position: relative – so an absolute badge or icon is placed inside it.
17. Which position value keeps a header in the flow until it reaches a scroll threshold, then fixes it in place?
fixed
static
sticky
relative
Show answer
Answer: C – sticky.position: sticky; top: 0; is how the header on this page stays visible. It needs a top (or other) offset to work.
18. What is the difference between display: none and visibility: hidden?
display: none removes the element from layout; visibility: hidden hides it but keeps its space
They are identical
visibility: hidden removes the space
display: none only works on images
Show answer
Answer: A. Both hide content from screen readers. To hide something visually but keep it readable by screen readers, use a “visually hidden” utility class.
19. In a flex container with the default flex-direction: row, what does justify-content: space-between control?
Vertical alignment
Distribution along the main (horizontal) axis
Font spacing
Wrapping onto new lines
Show answer
Answer: B. The first item goes to the start, the last to the end, with equal space between. If flex-direction is column, the main axis becomes vertical.
20. Which Flexbox property aligns items on the cross axis, such as centring them vertically in a row?
justify-content
flex-wrap
order
align-items
Show answer
Answer: D.display: flex; justify-content: center; align-items: center; is the classic way to centre something in both directions.
21. What does grid-template-columns: repeat(3, 1fr) create?
Three rows
Three columns of 1px
Three columns of equal width
One column repeated three times vertically
Show answer
Answer: C. For a responsive gallery without media queries, use repeat(auto-fit, minmax(220px, 1fr)).
22. In CSS Grid, what is the fr unit?
A fraction of the free space in the grid container
A fixed number of pixels
A font-relative unit
A frame rate
Show answer
Answer: A.2fr 1fr makes the first column twice as wide as the second, after fixed-size tracks and gaps are subtracted.
Questions 23–34: units, responsive design and styling
23. Which media query applies styles to screens 600px wide or narrower?
@media (min-width: 600px)
@media (max-width: 600px)
@media screen = 600px
@screen (600px)
Show answer
Answer: B. max-width targets narrow screens; min-width targets screens at least that wide.
24. What is rem relative to?
The parent element’s font size
The viewport width
The element’s own width
The root (html) element’s font size
Show answer
Answer: D. With the default 16px root size, 1.5rem is 24px. rem sizes respect users who increase their browser’s default font size.
25. When used for font-size, what is em relative to?
The root font size
The viewport height
The parent element’s font size
A fixed 16px
Show answer
Answer: C. Because em compounds through nested elements, rem is usually easier to predict for font sizes.
26. Why might z-index: 999 have no effect?
The element is not positioned (it is static) and is not a flex or grid item
z-index only accepts values below 100
z-index only works on images
The element has a background colour
Show answer
Answer: A. Also check stacking contexts: an element can never appear above something outside its parent’s stacking context.
27. Which of these is a pseudo-class?
::before
:hover
.active
#main
Show answer
Answer: B. Pseudo-classes (single colon) select elements in a state, such as :hover, :focus or :nth-child(2).
28. What does the ::before pseudo-element do?
Selects the previous sibling
Runs styles before the page loads
Selects the first child
Inserts generated content before an element’s content (it needs a content property)
Show answer
Answer: D. Often used for icons and decorations. Generated content should not carry important information, since some assistive tools ignore it.
29. How do you declare and use a CSS custom property (variable)?
$brand: blue; and color: $brand;
@brand = blue;
--brand: blue; and color: var(--brand);
var brand = blue;
Show answer
Answer: C. Custom properties live in the browser, cascade and can change at run time – ideal for themes and dark mode. Option A is Sass syntax.
30. What is the difference between ul li and ul > li?
ul li matches li elements at any depth inside ul; ul > li matches only direct children
They are identical
ul > li matches any depth
ul li only matches the first li
Show answer
Answer: A. The child combinator is useful for nested menus, where you want to style only the top level.
31. Two rules with the same specificity set different colours on the same element. Which wins?
The first one in the stylesheet
The one that appears later
The shorter one
Neither; the browser uses black
Show answer
Answer: B. Source order is the final tie-breaker in the cascade.
32. Which property is inherited by child elements by default?
border
margin
padding
color
Show answer
Answer: D – color. Text properties such as color, font-family and line-height inherit; box properties such as border, margin and padding do not.
33. Which property animates a change smoothly, for example when a button’s background changes on hover?
animation-name
transform
transition
@keyframes
Show answer
Answer: C.transition: background-color .2s ease;. Use @keyframes with animation for multi-step or looping animations.
34. Which unit equals 1% of the viewport width?
vw
%
vh
px
Show answer
Answer: A – vw.font-size: clamp(32px, 4.6vw, 50px), used for headings on this page, scales with the screen but stays within limits.
Questions 35–40: performance, accessibility and best practice
35. What is the standard way to load an external stylesheet?
<style src="style.css">
<link rel="stylesheet" href="style.css"> in the head
<css href="style.css">
<script src="style.css">
Show answer
Answer: B. External stylesheets can be cached by the browser and shared across every page of a site.
36. What does loading="lazy" on an <img> do?
Lowers the image quality
Loads the image after all scripts
Makes the image fade in
Delays loading off-screen images until the user scrolls near them
Show answer
Answer: D. It speeds up the first page load. Do not lazy-load the main image at the top of the page, as that slows the Largest Contentful Paint.
37. What does the defer attribute on a <script> tag do?
Runs the script immediately, blocking the page
Prevents the script from running
Downloads the script in parallel and runs it after the HTML is parsed, in order
Runs the script only on mobile
Show answer
Answer: C.async also downloads in parallel but runs as soon as the script arrives, in any order – fine for independent scripts such as analytics.
38. Which elements are meant for an image or chart with a caption?
<figure> and <figcaption>
<picture> and <title>
<image> and <caption>
<div> and <p>
Show answer
Answer: A.<caption> exists, but only for tables. <picture> chooses between image sources for different screens.
39. Why should a form field not rely on placeholder text instead of a label?
Placeholders are not supported in modern browsers
The placeholder disappears as the user types and is often low-contrast and poorly announced, so the field loses its description
Placeholders slow down the page
Labels are required for the form to submit
Show answer
Answer: B. Use a visible label and, if helpful, a placeholder showing an example format, such as 0300-1234567.
40. What does a “mobile-first” CSS approach mean?
Building a separate mobile website
Hiding content on desktop
Using only percentage widths
Writing base styles for small screens, then adding min-width media queries for larger screens
Show answer
Answer: D. Most visitors now browse on phones, and Google indexes the mobile version of pages first, so starting small is the practical default.
What to practise next
If the box model, specificity or Flexbox questions caught you out, build a small page – a pricing card or a navbar – and experiment in the browser’s developer tools. Then add behaviour with the JavaScript MCQs. All topics are on the Programming MCQs hub.
Want a fast, professional website?
AbairTech designs and builds responsive, SEO-friendly websites and web apps for businesses.
This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
Strictly Necessary Cookies
Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.
If you disable this cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.