Top CSS Interview Questions for 2024

author image Hirely
at 07 Jan, 2025

Question: What is the difference between inline and block elements in CSS?

Answer:

In CSS, elements can be categorized as either inline or block based on their behavior in the document flow. Here’s a breakdown of the key differences:

1. Display Behavior

  • Block-level elements: These elements take up the full width available to them, meaning they expand to fill the container. They also start on a new line and stack vertically one after the other. Block-level elements are often used for larger layout structures.

    • Examples of block-level elements: <div>, <p>, <h1>, <section>, <article>, etc.
  • Inline elements: These elements only take up as much width as necessary for their content, meaning they do not force a line break. Inline elements flow alongside other elements on the same line, unless the line is too long, in which case the next inline element moves to the next line.

    • Examples of inline elements: <span>, <a>, <strong>, <em>, etc.

2. Line Breaks

  • Block-level elements: Always start on a new line and create a line break before and after them.

  • Inline elements: Do not start on a new line. They only occupy the space required for their content and are displayed within the same line as other inline elements.

3. Width and Height

  • Block-level elements: You can set the width and height properties on block elements. By default, block elements take up the full width of their parent container, but this can be changed using the width property.

  • Inline elements: Inline elements do not support setting width or height directly. They only take up the space needed by their content, and adjusting their width or height will have no effect.

4. CSS Properties

  • Block-level elements: You can apply margin, padding, width, height, and many other layout properties effectively. For example, you can control the left and right margins of a block element, making it ideal for layout purposes.

  • Inline elements: Inline elements only respect horizontal margins (margin-left and margin-right) but do not respect vertical margins (margin-top and margin-bottom). You cannot apply width or height to inline elements unless you change their display type.

5. Example of Block and Inline Behavior:

<div style="background-color: lightblue;">This is a block-level element</div>
<span style="background-color: lightgreen;">This is an inline element</span>

In the above example:

  • The <div> element takes up the entire width and will push other content to the next line.
  • The <span> element will only take up as much space as needed for its content and will flow with other inline elements on the same line.

6. Converting Between Inline and Block:

You can change the display behavior of an element using the display property in CSS:

  • To make a block-level element behave like an inline element:
    .element {
        display: inline;
    }
  • To make an inline element behave like a block-level element:
    .element {
        display: block;
    }

Summary of Differences:

FeatureBlock ElementsInline Elements
Display BehaviorTakes up full width, starts on a new line.Takes up only as much space as its content.
Line BreaksCreates line breaks before and after the element.Does not create line breaks.
Width/HeightCan set width and height.Cannot set width or height.
CSS PropertiesCan apply margin, padding, width, height, etc.Margin and padding only apply horizontally.
Examples<div>, <h1>, <p>, <section>, <footer><span>, <a>, <strong>, <em>, etc.

Conclusion:

  • Block-level elements are typically used for larger, structural components of a webpage, such as containers, paragraphs, sections, and headers.
  • Inline elements are used for smaller pieces of content that should appear within other content, such as links, text formatting, and other inline tags.

Choosing the appropriate element type helps to control the layout and flow of content effectively in your web page.

Question: What are the different types of CSS positioning?

Answer:

CSS provides several types of positioning methods to control how elements are placed on the page. The main types of CSS positioning are:

1. Static Positioning (position: static;)

  • Default positioning for all elements, unless specified otherwise.
  • Elements with position: static; are positioned according to the normal document flow. This means that they appear where they would naturally appear based on the surrounding content, without any special adjustments.
  • You cannot use top, right, bottom, or left properties with position: static; since these properties only apply when using relative, absolute, or fixed positioning.

Example:

div {
    position: static;
}

2. Relative Positioning (position: relative;)

  • The element is positioned relative to its normal position in the document flow.
  • It still takes up space in the document layout, but you can move it away from its original position using the top, right, bottom, and left properties.
  • The space it occupies in the document remains unchanged.

Example:

div {
    position: relative;
    top: 20px; /* Moves the element 20px down from its original position */
    left: 10px; /* Moves the element 10px to the right from its original position */
}

3. Absolute Positioning (position: absolute;)

  • The element is positioned relative to the nearest positioned ancestor element (an element with a position other than static).
  • If there is no positioned ancestor, it will be positioned relative to the initial containing block, which is usually the <html> element.
  • The element is removed from the document flow, meaning it will not take up space and other elements will behave as if it does not exist.
  • You can use top, right, bottom, and left to specify the exact position.

Example:

div {
    position: absolute;
    top: 50px;
    left: 100px; /* Positions the element 50px from the top and 100px from the left */
}

4. Fixed Positioning (position: fixed;)

  • The element is positioned relative to the viewport, which means it will remain fixed in place even when the page is scrolled.
  • It is removed from the document flow, and it will not affect the positioning of other elements.
  • You can use top, right, bottom, and left properties to position the element.

Example:

div {
    position: fixed;
    top: 10px; /* Fixed 10px from the top of the viewport */
    right: 10px; /* Fixed 10px from the right of the viewport */
}

5. Sticky Positioning (position: sticky;)

  • The element is positioned based on the user’s scroll position. It behaves like a relative element until it reaches a certain point on the page, at which point it “sticks” in place and behaves like a fixed element.
  • The top, right, bottom, or left properties must be defined for the sticky behavior to work, and the element will stick once it reaches the defined position within the viewport.

Example:

div {
    position: sticky;
    top: 0; /* Sticks the element to the top of the viewport when you scroll */
}

Summary of CSS Positioning:**

Positioning TypeDescriptionEffect on Document FlowPositioning Context
staticDefault positioning. Positioned according to the normal document flow.Does not remove the element from flow.Normal document flow
relativePositioned relative to its normal position. Can move element with top, left.Element still takes up space.Nearest positioned ancestor or document
absolutePositioned relative to the nearest positioned ancestor or the document.Removed from document flow.Nearest positioned ancestor or document
fixedPositioned relative to the viewport. Stays fixed even when scrolling.Removed from document flow.Viewport (browser window)
stickyPositioned relative until scrolling past a defined position, then becomes fixed.Element stays in normal flow until sticky threshold is reached.Viewport and scroll position

Conclusion:

  • Static: Default, no positioning adjustments.
  • Relative: Moves the element relative to its normal position.
  • Absolute: Moves the element outside of the normal flow and positions it relative to the nearest positioned ancestor.
  • Fixed: Positions the element relative to the viewport and stays in place during scrolling.
  • Sticky: Behaves like a relative element until it reaches a certain point, then sticks like a fixed element.

Each positioning method serves different use cases, allowing developers to control how elements behave on the page, particularly for layouts, sticky elements, and elements that need to stay visible during scrolling.

Read More

If you can’t get enough from this article, Aihirely has plenty more related information, such as CSS interview questions, CSS interview experiences, and details about various CSS job positions. Click here to check it out.

Related Posts

Trace Job opportunities

Hirely, your exclusive interview companion, empowers your competence and facilitates your interviews.

Get Started Now