CSS Concepts and Techniques Quiz Quiz

Test your knowledge of core CSS concepts, selectors, box model, positioning, and styling techniques with these 15 intermediate-level questions. Perfect for interview preparation and mastering essential CSS skills for web development.

  1. CSS Purpose

    Which of the following best describes the main purpose of CSS in web development?

    1. To retrieve data from a database
    2. To control the structure of web pages
    3. To style and visually present HTML elements
    4. To add client-side interactivity to web pages

    Explanation: CSS is primarily used to control the styling, layout, and visual presentation of HTML elements on a web page. HTML controls structure, while interactivity is handled by JavaScript, and databases use different languages altogether. Choosing structural or data-oriented options is incorrect because CSS does not manage those aspects.

  2. Types of CSS

    What is a key difference between internal and external CSS?

    1. Internal CSS is written in a .css file, while external CSS is inside HTML tags
    2. Internal CSS cannot use classes, but external CSS can
    3. Internal CSS is placed inside the u003Cheadu003E section, while external CSS is linked from a separate file
    4. External CSS uses inline style attributes

    Explanation: Internal CSS is included within a u003Cstyleu003E block in the HTML u003Cheadu003E, while external CSS is written in a separate file and linked in the HTML. The options mentioning file extensions or inline styles are incorrect, and both internal and external CSS can use classes.

  3. Selector Types

    Given this CSS: .big { color: orange; }, what does the selector '.big' target?

    1. All elements named 'big'
    2. All elements with id 'big'
    3. All elements with class 'big'
    4. Only u003Cbigu003E elements

    Explanation: A dot (.) before a name in CSS refers to a class selector, targeting all elements with that class. An id selector uses a hash (#), and there is no 'big' HTML tag. The distractors confuse classes, IDs, and tag names.

  4. Box Model Structure

    In the CSS box model, which area surrounds the content and is directly inside the border?

    1. Margin
    2. Padding
    3. Outline
    4. Shadow

    Explanation: Padding provides space between the content and the border in the box model. Margin is outside the border, outline is an extra line outside the border (but not a box model layer), and shadow is a visual effect, not a box layer.

  5. Margin vs Padding

    Which statement accurately distinguishes margin from padding in CSS?

    1. Margin creates space outside the border; padding creates space between content and border
    2. Margin is the space inside the element's border; padding is outside
    3. Margin adds space between the content and border; padding adds space outside the border
    4. Both margin and padding do the same thing

    Explanation: Margin is for spacing outside the border, while padding is for space inside the border, between the content and border. Option one confuses the two, and option two reverses their roles. Saying they're identical is incorrect.

  6. Positioning Defaults

    By default, what is the value of the CSS 'position' property for HTML elements?

    1. Static
    2. Fixed
    3. Absolute
    4. Relative

    Explanation: The default 'position' of all HTML elements is 'static,' which means elements flow normally in the document. 'Fixed', 'relative', and 'absolute' must be explicitly set to alter positioning. Choosing any of the others would ignore this default behavior.

  7. Z-Index Application

    What does the CSS z-index property control?

    1. Stacking order of overlapping positioned elements
    2. The outer margin size
    3. Text alignment inside an element
    4. Element's background color

    Explanation: The z-index property determines the stacking order of elements with a position set other than static. It does not affect colors, alignment, or margins. Distractors refer to unrelated styling features.

  8. Pseudo-classes Usage

    Which CSS selector styles an element when a user hovers a mouse pointer over it?

    1. element:focus
    2. element:checked
    3. element:visited
    4. element:hover

    Explanation: The :hover pseudo-class targets elements while the mouse is over them. :focus applies when an element is active (like in a form), :checked relates to checkboxes, and :visited refers to links already clicked.

  9. Unit Differences

    Which CSS unit is always based on the root (html) element's font size?

    1. rem
    2. px
    3. em
    4. %

    Explanation: The 'rem' unit is relative to the root element's font size, while 'em' is relative to the parent element's font size. 'px' is an absolute unit, and '%' is relative to another length (often the parent element). Choosing any but 'rem' is inaccurate here.

  10. Multiple Classes

    How can you apply multiple CSS classes to a single HTML element?

    1. u003Cdiv class='one-two'u003Eu003C/divu003E
    2. u003Cdiv class='one, two'u003Eu003C/divu003E
    3. u003Cdiv class='one two'u003Eu003C/divu003E
    4. u003Cdiv class='one.two'u003Eu003C/divu003E

    Explanation: Multiple classes are added to the class attribute separated by spaces, as in class='one two'. Hyphens, commas, or dots are not valid methods for combining classes in HTML attributes.

  11. CSS Specificity

    Which of the following selector combinations has the highest specificity?

    1. An ID selector
    2. A class selector
    3. A universal selector
    4. An element selector

    Explanation: ID selectors are more specific than class or element selectors, and the universal selector (*) has the lowest specificity. Using a class or element won't override an ID rule.

  12. Comment Syntax

    Which is the correct way to write a comment in CSS?

    1. // This is a comment
    2. u003C!-- This is a comment --u003E
    3. /* This is a comment */
    4. # This is a comment

    Explanation: CSS comments use /* ... */ syntax. Double slashes and hash symbols are for other languages, and HTML comments (u003C!-- --u003E) are not valid in CSS. Using any other format leads to parsing errors.

  13. Display None vs Visibility Hidden

    What is the key difference between 'display: none;' and 'visibility: hidden;' in CSS?

    1. 'display: none;' hides the element but keeps its space; 'visibility: hidden;' removes its space
    2. 'display: none;' and 'visibility: hidden;' always behave the same
    3. 'display: none;' removes the element from layout; 'visibility: hidden;' keeps it in the layout but makes it invisible
    4. 'display: none;' only works with images

    Explanation: With 'display: none,' the element is removed from the document flow and does not take up space. 'Visibility: hidden' makes the element invisible but reserves its space. The first and last choices are incorrect or too narrow, and they do not act identically.

  14. Overflow Control

    Which CSS property manages how content is displayed when it overflows its container?

    1. float
    2. align
    3. clear
    4. overflow

    Explanation: The 'overflow' property specifies what happens if content overflows its container. 'Float' changes positioning, 'clear' affects floating elements, and 'align' is not a standard CSS property. Only 'overflow' directly solves content overflow issues.

  15. Selector Example

    Which selector targets all u003Cpu003E elements inside a u003Cdivu003E?

    1. divu003Ep
    2. div p
    3. div, p
    4. div p
    5. #div.p

    Explanation: The selector 'div p' selects all u003Cpu003E elements inside a u003Cdivu003E. 'div, p' would select all divs and all paragraphs separately, 'divu003Ep' only selects direct child paragraphs, and '#div.p' is attempting to select elements with both id 'div' and class 'p', which isn't relevant here.

  16. Width Properties

    How does 'max-width' differ from 'width' in CSS?

    1. 'width' can use only px; 'max-width' only uses % units
    2. 'width' sets an exact width; 'max-width' limits the maximum width allowed
    3. 'max-width' sets an exact width; 'width' sets an upper limit
    4. There is no difference between them

    Explanation: 'width' determines the exact width of an element, while 'max-width' acts as an upper constraint and allows the element to shrink if needed. The other options confuse their behavior or are incorrect.