AI-assisted, automatically checked• Updated August 2026

HTML/CSS MCQs

HTML5 semantic tags, CSS flexbox, grid layout, responsive design, and animations.

46 MCQsProgrammingAll Levels
Key Concept Overview & Exam Context

Review core principles of HTML/CSS before answering questions. These questions simulate placement technical assessments (TCS, Infosys, Amazon), university exams (CS/IT), and technical certifications.

Target Readiness
Placements & Job Interviews
Question Format
Multiple-Choice with Explanations
Explained Solutions
Written explanations

HTML/CSS MCQ Questions

Page 1 of 1 (46 total)
Q1

What is the CSS box model made up of?

easy
View Answer & Explanation
Correct Answer: Content, padding, border, and margin, arranged from innermost to outermost
Explanation:
Every HTML element is treated as a rectangular box, and the box model describes how the content sits inside padding, which is surrounded by a border, which is then surrounded by margin, spacing the element from its neighbors.
Q2

What does the CSS property 'display: flex' enable on a container element?

medium
View Answer & Explanation
Correct Answer: It turns the container into a flex container, allowing its child elements to be laid out flexibly in a row or column
Explanation:
Flexbox makes it much easier to align and distribute space among child elements, whether the layout needs to be a horizontal row or a vertical column, without relying on older techniques like floats.
Q3

What is the purpose of the HTML 'alt' attribute on an image tag?

medium
View Answer & Explanation
Correct Answer: To provide alternative text describing the image, which is used by screen readers and shown if the image fails to load
Explanation:
The alt attribute plays an important accessibility role by giving visually impaired users, through screen reader software, a text description of what the image shows, and it also acts as a fallback if the image cannot load.
Q4

What is the difference between 'id' and 'class' attributes in HTML?

easy
View Answer & Explanation
Correct Answer: An 'id' should be unique to a single element on a page, while a 'class' can be applied to multiple elements
Explanation:
Because an id is meant to identify one specific element, using the same id on multiple elements is considered invalid HTML, while classes are specifically designed to group and style multiple elements together consistently.
Q5

What does CSS specificity determine?

hard
View Answer & Explanation
Correct Answer: Which CSS rule takes precedence when multiple rules target the same element
Explanation:
Specificity is calculated based on the types of selectors used, with inline styles and IDs generally carrying more weight than classes, and classes carrying more weight than element selectors, determining which conflicting rule actually gets applied.
Q6

What is the purpose of a CSS media query?

medium
View Answer & Explanation
Correct Answer: To apply different styles depending on characteristics of the device or viewport, such as screen width
Explanation:
Media queries are the foundation of responsive web design, allowing a single stylesheet to adjust layout, font size, or visibility of elements based on conditions like the browser window's width.
Q7

What is the difference between 'position: relative' and 'position: absolute' in CSS?

hard
View Answer & Explanation
Correct Answer: 'relative' positions an element relative to its normal position, while 'absolute' positions it relative to its nearest positioned ancestor
Explanation:
An absolutely positioned element is removed from the normal document flow and positioned based on its closest ancestor that has a position value other than static, while a relatively positioned element still occupies its original space and shifts visually from there.
Q8

What is the purpose of semantic HTML tags, such as <header> and <article>?

medium
View Answer & Explanation
Correct Answer: To describe the meaning and structure of content, improving accessibility and search engine understanding of the page
Explanation:
Unlike a generic div, semantic tags like <nav>, <article>, and <footer> convey the actual purpose of the content they wrap, which helps screen readers and search engines interpret the page structure more accurately.
Q9

What does the CSS 'z-index' property control?

medium
View Answer & Explanation
Correct Answer: The stacking order of overlapping elements along the vertical axis, determining which appears in front
Explanation:
Elements with a higher z-index value are displayed in front of elements with a lower value, but z-index only has an effect on elements that have a position value other than static.
Q10

What is the purpose of CSS Grid layout?

hard
View Answer & Explanation
Correct Answer: To create two-dimensional layouts by arranging elements into rows and columns simultaneously
Explanation:
Unlike Flexbox, which is primarily designed for one-dimensional layouts along a single row or column, CSS Grid allows precise control over both rows and columns at the same time, making it well suited for complex page layouts.
Q11

What does the HTML5 <section> tag represent?

medium
View Answer & Explanation
Correct Answer: A thematic grouping of content, typically with its own heading
Explanation:
The section tag is meant to group related content together under a common theme, such as a chapter or a distinct part of a page, and it is commonly used alongside a heading element to label that grouping.
Q12

What is the purpose of the CSS 'float' property?

medium
View Answer & Explanation
Correct Answer: To position an element to the left or right, allowing text and inline elements to wrap around it
Explanation:
Float was originally designed for wrapping text around images, and although it was once widely used for full page layouts, modern CSS layout techniques like Flexbox and Grid are generally preferred for that purpose now.
Q13

What is the difference between an inline element and a block-level element in HTML?

easy
View Answer & Explanation
Correct Answer: A block-level element starts on a new line and takes up the full available width, while an inline element only takes up as much width as its content
Explanation:
Elements like <div> and <p> are block-level by default, forcing a line break before and after them, while elements like <span> and <a> are inline and sit within the surrounding text flow without forcing a new line.
Q14

What does the CSS 'em' unit represent?

hard
View Answer & Explanation
Correct Answer: A relative unit based on the font size of the element it is applied to, or its parent
Explanation:
Because 'em' is relative rather than fixed, using it for sizing allows text and spacing to scale proportionally if the base font size changes, which is useful for building flexible, accessible layouts.
Q15

What is the purpose of the HTML <meta name='viewport'> tag?

medium
View Answer & Explanation
Correct Answer: To control how a webpage is scaled and displayed on mobile devices
Explanation:
Without a properly configured viewport meta tag, mobile browsers often render pages at a desktop width and then scale them down, making text unreadable, so this tag is essential for responsive web design.
Q16

What is the effect of setting 'box-sizing: border-box' on an element in CSS?

hard
View Answer & Explanation
Correct Answer: The element's specified width and height include its padding and border, rather than adding them on top
Explanation:
With the default box-sizing value, padding and border are added on top of the specified width, which can make sizing elements precisely more difficult, so many developers set border-box globally to make width calculations more predictable.
Q17

What is the purpose of a CSS pseudo-class, such as ':hover'?

medium
View Answer & Explanation
Correct Answer: To apply styles to an element based on a specific state or condition, such as when the mouse is hovering over it
Explanation:
Pseudo-classes let developers style elements dynamically based on user interaction or document state, without needing JavaScript, such as changing a button's background color while a user is hovering over it.
Q18

What does the CSS 'transition' property allow you to do?

medium
View Answer & Explanation
Correct Answer: Animate changes to a CSS property smoothly over a specified duration, rather than having the change happen instantly
Explanation:
By specifying which property to transition and how long the animation should take, elements can smoothly change states, such as gradually changing color when hovered, instead of switching abruptly.
Q19

What is the purpose of the <form> element in HTML?

easy
View Answer & Explanation
Correct Answer: To collect user input and submit it to a server for processing
Explanation:
A form typically contains input fields, checkboxes, or buttons, and when submitted, the browser packages the entered data and sends it to a specified URL, often to be processed by a server-side script.
Q20

What does the CSS 'overflow: hidden' property do to an element?

medium
View Answer & Explanation
Correct Answer: It clips any content that extends beyond the element's box, hiding the overflowing part from view
Explanation:
This is different from 'overflow: scroll', which would add scrollbars to let users access the extra content, since 'hidden' simply discards the visual overflow without providing any way to view it.
Q21

What is the purpose of the HTML 'lang' attribute on the html tag?

easy
View Answer & Explanation
Correct Answer: It specifies the primary language of the page's content, helping browsers and screen readers process it correctly
Explanation:
Declaring the correct language, such as lang='en' for English, also helps search engines and translation tools understand the page more accurately.
Q22

What is the difference between CSS 'margin' and 'padding'?

easy
View Answer & Explanation
Correct Answer: Margin creates space outside an element's border, while padding creates space inside the border, between the border and the content
Explanation:
Understanding this distinction is central to the CSS box model, since both properties affect an element's overall footprint on the page but in different ways.
Q23

What is the purpose of the HTML 'label' element when used with a form input?

medium
View Answer & Explanation
Correct Answer: It provides a descriptive, clickable caption for a form control, improving usability and accessibility
Explanation:
Associating a label with an input using the 'for' attribute means clicking the label text will focus or activate the linked input, which is especially helpful on touch devices.
Q24

What does the CSS 'flex-wrap' property control within a flex container?

medium
View Answer & Explanation
Correct Answer: Whether flex items are forced onto a single line or allowed to wrap onto multiple lines
Explanation:
By default flex items try to fit on one line, but setting flex-wrap to wrap lets items move onto additional lines when there is not enough horizontal space.
Q25

What is the purpose of the HTML5 <nav> tag?

easy
View Answer & Explanation
Correct Answer: It semantically marks a section of a page intended for navigation links
Explanation:
Using <nav> instead of a generic <div> gives browsers, search engines, and assistive technologies a clearer signal about the purpose of that part of the page.
Q26

What does the CSS 'opacity' property control?

medium
View Answer & Explanation
Correct Answer: The transparency level of an entire element, including its content, where lower values make it more see through
Explanation:
Because opacity affects the whole element uniformly, reducing it also fades any text or child elements inside, which differs from adjusting just the background color's alpha value.
Q27

What is the purpose of the CSS ':nth-child()' pseudo-class?

hard
View Answer & Explanation
Correct Answer: It selects one or more elements based on their numeric position among sibling elements
Explanation:
For example, ':nth-child(2n)' selects every second element, which is a common way to style alternating rows in a table without adding extra classes manually.
Q28

What is the difference between the HTML <ol> and <ul> tags?

easy
View Answer & Explanation
Correct Answer: <ol> creates an ordered, numbered list, while <ul> creates an unordered list with bullet points
Explanation:
Choosing between them depends on whether the order of items actually matters, such as steps in a recipe for <ol> versus a simple list of ingredients for <ul>.
Q29

What is the purpose of the CSS 'min-width' and 'max-width' properties?

medium
View Answer & Explanation
Correct Answer: They set lower and upper limits on how wide an element can become, regardless of other sizing rules
Explanation:
These properties are especially useful in responsive design, letting an element shrink or grow within a sensible range instead of stretching too wide or shrinking too small on different screens.
Q30

What does the HTML 'required' attribute do when added to a form input?

easy
View Answer & Explanation
Correct Answer: It prevents the form from being submitted until that field has a value
Explanation:
This attribute enables basic client side validation without needing any JavaScript, though server side validation is still recommended as a backup for security.
Q31

What is the purpose of CSS custom properties, also known as CSS variables?

medium
View Answer & Explanation
Correct Answer: They let you define reusable values, such as colors or sizes, that can be referenced throughout a stylesheet
Explanation:
Declared with a syntax like '--main-color: blue;' and used with 'var(--main-color)', CSS variables make it much easier to update a value consistently across an entire stylesheet.
Q32

What is the difference between 'position: fixed' and 'position: sticky' in CSS?

hard
View Answer & Explanation
Correct Answer: 'fixed' keeps an element in the same place relative to the browser window at all times, while 'sticky' toggles between relative and fixed behavior based on scroll position
Explanation:
A sticky element behaves normally until the page scrolls to a certain point, then it sticks in place, which is commonly used for things like a table header that stays visible while scrolling.
Q33

What is the purpose of the HTML <figure> and <figcaption> elements?

medium
View Answer & Explanation
Correct Answer: They group an image or illustration together with a caption that describes it semantically
Explanation:
Using these tags helps convey the relationship between an image and its caption to both browsers and assistive technologies, rather than relying on generic divs.
Q34

What does the CSS 'vw' unit represent?

medium
View Answer & Explanation
Correct Answer: One percent of the viewport's width
Explanation:
So an element with a width of 50vw will always take up half of the browser window's visible width, regardless of the size of its parent container.
Q35

What is the purpose of CSS Flexbox's 'justify-content' property?

medium
View Answer & Explanation
Correct Answer: It controls how flex items are distributed along the main axis of a flex container
Explanation:
Values like 'space-between' or 'center' let you quickly control horizontal spacing among items in a row without manually calculating margins for each one.
Q36

What is the purpose of the HTML <iframe> element?

easy
View Answer & Explanation
Correct Answer: It embeds another HTML document within the current page
Explanation:
Iframes are commonly used to embed content from another source, such as a map or a video, directly inside a webpage while keeping that content isolated from the main page.
Q37

What does the CSS 'visibility: hidden' property do differently compared to 'display: none'?

medium
View Answer & Explanation
Correct Answer: 'visibility: hidden' hides an element but still reserves its space in the layout, while 'display: none' removes it from the layout entirely
Explanation:
This distinction matters for layout purposes, since a hidden element with 'visibility: hidden' still takes up its normal spot, leaving a visible gap where it would have appeared.
Q38

What is the purpose of the CSS 'aspect-ratio' property?

hard
View Answer & Explanation
Correct Answer: It sets a preferred width to height ratio for an element, helping it maintain proportions as its size changes
Explanation:
This property is particularly useful for responsive images or video containers, ensuring they keep a consistent shape, such as 16 to 9, even as the surrounding layout resizes.
Q39

What is the purpose of the HTML <script> tag's 'defer' attribute?

hard
View Answer & Explanation
Correct Answer: It tells the browser to download the script in the background but wait to execute it until the HTML has finished parsing
Explanation:
Using defer generally improves page load performance, since it avoids blocking the parsing of the rest of the HTML document while the script downloads.
Q40

What does the CSS 'text-overflow: ellipsis' property do when combined with 'overflow: hidden' and 'white-space: nowrap'?

hard
View Answer & Explanation
Correct Answer: It displays truncated text with three dots at the end when it does not fit within its container
Explanation:
This combination is a common pattern for showing a preview of longer text, such as a headline, without letting it break the layout of a card or button.
Q41

What is the purpose of the HTML <button> element's 'type' attribute?

medium
View Answer & Explanation
Correct Answer: It specifies the button's behavior, such as submitting a form, resetting a form, or doing nothing by default
Explanation:
Forgetting to set type='button' on a button meant only to trigger JavaScript can accidentally submit a form, since the default type inside a form is 'submit'.
Q42

What is the purpose of CSS Grid's 'grid-template-columns' property?

medium
View Answer & Explanation
Correct Answer: It defines the number and size of columns in a grid container
Explanation:
Values like 'grid-template-columns: 1fr 2fr 1fr' create three columns where the middle one takes up twice as much available space as each of the outer columns.
Q43

What is the purpose of the HTML <noscript> tag?

hard
View Answer & Explanation
Correct Answer: It defines content to display if a browser does not support or has disabled JavaScript
Explanation:
This tag provides a fallback experience, such as a simple message explaining that certain features require JavaScript to function properly.
Q44

What is the purpose of the CSS 'cursor' property?

easy
View Answer & Explanation
Correct Answer: It changes the appearance of the mouse pointer when hovering over an element
Explanation:
Setting cursor to 'pointer' on a clickable element, for instance, gives users a visual cue that it can be interacted with, even if it is not a traditional link or button.
Q45

What is the difference between an HTML attribute and a CSS property?

medium
View Answer & Explanation
Correct Answer: An HTML attribute provides additional information directly on an element in the markup, while a CSS property defines how that element should be styled visually
Explanation:
For example, the 'src' attribute on an img tag tells the browser which file to load, while a CSS property like 'width' controls how large that image appears once it is displayed.
Q46

Which CSS property is used to control the alignment of flex items along the main axis?

easy
View Answer & Explanation
Correct Answer: justify-content
Explanation:
justify-content controls the alignment of flex items along the main axis, while align-items controls cross-axis alignment.