Frontend School
DashboardProblemsLearnPracticeMock InterviewReportsStatsBuy Credits
DashboardProblemsLearnPracticeMock Interview
CSS Layouts: Flexbox, Grid & Responsive
  1. 1.Introduction to CSS & Selectors
  2. 2.The CSS Box Model & Basic Styling
  3. 3.Display Property & Positioning Fundamentals
  4. 4.Units, Values & Responsive Basics
  5. 5.Flexbox Fundamentals
  6. 6.Advanced Flexbox & Real-world Patterns
  7. 7.CSS Grid Fundamentals
  8. 8.Advanced CSS Grid & Area Layouts
  9. 9.Responsive Design Strategies & Mobile First
  10. 10.CSS Transitions, Transforms & Animations
  11. 11.Advanced CSS Concepts & Performance
  12. 12.Layout Techniques for Complex UIs & Interview Prep
CSS Layouts: Flexbox, Grid & Responsive
02

CSS · Lesson 2 of 12 · 3 min

The CSS Box Model & Basic Styling

This is not a 'nice-to-know'. Get this wrong, and you fail the CSS round.

After this, you will be able to explain precisely how an element's size and spacing are calculated by the browser. This is a foundational concept, and interviewers use it as a filter. If you can't explain box-sizing, you won't move on.

The Four Layers of Every Element

Every HTML element is rendered as a rectangular box. This box is composed of four nested layers, from the inside out:

  1. Content: The text, image, or other media. Its dimensions are set by width and height.
  2. Padding: Transparent space around the content, inside the border. It takes on the element's background color.
  3. Border: A line that goes around the padding and content.
  4. Margin: Transparent space outside the border, pushing other elements away.

Understanding this order is non-negotiable for debugging any layout issue.

How box-sizing Controls Sizing

By default, when you set width: 200px, that 200px applies only to the content box. The element's total rendered width will be width + padding + border. This is almost never what you want.

This behavior is controlled by the box-sizing property.

  • box-sizing: content-box (the default): width and height apply only to the content. Padding and border are added on top, making the element bigger than you specified.
  • box-sizing: border-box: width and height apply to the entire visible box—from one edge of the border to the other. The content area shrinks to make room for any padding and border you add.

This makes layout math predictable. If you set width: 25% on four items, they will fit perfectly in a row, even with padding and borders.

This is the expected answer in an interview. You should state that border-box is the modern standard. Most projects start with a CSS reset that applies it globally.

html {
  box-sizing: border-box;
}
*, *::before, *::after {
  box-sizing: inherit;
}

This makes all elements use border-box by default, which is a sane starting point for any layout.

Controlling Space with Margin and Padding

Use padding for internal space and margin for external space.

  • padding pushes content away from the border. It's part of the element's clickable area and background.
  • margin pushes the entire element away from its neighbors. It's always transparent.

Both properties use shorthand for setting values on all four sides. The order is clockwise: top, right, bottom, left.

/* 4 values: top, right, bottom, left */
margin: 10px 20px 15px 5px;

/* 3 values: top, horizontal (left/right), bottom */
padding: 10px 20px 15px;

/* 2 values: vertical (top/bottom), horizontal (left/right) */
margin: 10px 20px;

/* 1 value: all four sides */
padding: 10px;

The trap: Vertical margins of adjacent block-level elements can collapse. If one element has margin-bottom: 20px and the next has margin-top: 30px, the space between them will be 30px, not 50px. The larger margin wins.

Horizontal margins never collapse. Knowing about margin collapsing is a detail that signals experience. Modern layouts often use Flexbox or Grid with the gap property, which provides predictable spacing without collapsing.

Building a Practical Card Component

Let's apply this to a common UI element. This card's total width will be exactly 300px because of box-sizing: border-box. The padding creates internal breathing room, and the margin on the inner elements controls their vertical spacing.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Styled Card Component</title>
    <style>
        body {
            font-family: system-ui, sans-serif;
            background-color: #f4f7f6;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
        }

        .card {
            /* This width is the final rendered width thanks to border-box */
            width: 300px;
            box-sizing: border-box;

            /* Internal spacing */
            padding: 24px;

            /* Visual styling */
            background-color: #ffffff;
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.05);
        }

        .card h3 {
            /* Control spacing between elements inside the card */
            margin-top: 0;
            margin-bottom: 8px;
            font-size: 1.25rem;
            color: #333;
        }

        .card p {
            margin-top: 0;
            margin-bottom: 0;
            color: #666;
            line-height: 1.6;
        }
    </style>
</head>
<body>
    <div class="card">
        <h3>Card Title</h3>
        <p>This component's size is predictable because `box-sizing: border-box` makes the `width` property include `padding` and `border`.</p>
    </div>
</body>
</html>

Try it

Take the card component code. Add a button inside it, below the paragraph. Use margin-top to create space between the paragraph and the button. Then, create a second identical card and use margin on the .card class to create space between the two cards when they are next to each other.

Next: We will cover the display property and positioning fundamentals, which control how these boxes interact with each other in the document flow.

Lock it in — apply this lesson under interview conditions.

Try an AI mock interview
Previous

Introduction to CSS & Selectors

Next

Display Property & Positioning Fundamentals