Courses/HTML & CSS/CSS Box Model & Display

    CSS Box Model & Display

    Lesson 6 โ€ข Beginner Track

    What You'll Learn

    Understand margin, padding, border, and content areas
    Difference between content-box and border-box sizing
    How display: block, inline, and inline-block work
    Control element spacing and sizing precisely
    Debug layout issues caused by the box model
    Use box-sizing: border-box for predictable layouts

    ๐Ÿ’ก Think of It Like This

    Imagine a picture frame: The picture itself is the content. The matting around the picture is the padding. The frame itself is the border. And the space between the frame and the wall (or other frames) is the margin.

    Box LayerPicture Frame AnalogyCSS Property
    ContentThe picture itselfwidth / height
    PaddingThe matting around the picturepadding
    BorderThe frame itselfborder
    MarginSpace between frames on the wallmargin

    The CSS Box Model

    Every HTML element is a rectangular box. The box model describes how the browser calculates the total size of each element.

    Box Model Visualised

    See how margin, padding, border, and content work together

    Try it Yourself ยป
    Code Preview
    <!DOCTYPE html>
    <html>
    <head>
      <title>CSS Box Model</title>
      <style>
        body {
          background: #0f172a;
          font-family: system-ui, sans-serif;
          padding: 20px;
          color: #e5e7eb;
        }
    
        .box-demo {
          /* Content: 200x100 */
          width: 200px;
          height: 100px;
    
          /* Padding: space inside the border */
          padding: 20px;
    
          /* Border: the visible frame */
          border: 5px solid #3b82f6;
    
          /* Margin: space outside the border */
          margin: 30px auto;
    
      
    ...

    box-sizing: border-box vs content-box

    By default, width only sets the content width. Padding and border are added on top, making the element bigger than you expect. box-sizing: border-box fixes this by including padding and border inside the width.

    content-box vs border-box

    Compare how the same width behaves differently

    Try it Yourself ยป
    Code Preview
    <!DOCTYPE html>
    <html>
    <head>
      <title>box-sizing Comparison</title>
      <style>
        body {
          background: #0f172a;
          font-family: system-ui, sans-serif;
          padding: 20px;
          color: #e5e7eb;
        }
    
        .container {
          background: #1e293b;
          width: 300px;
          padding: 20px;
          border-radius: 8px;
          margin-bottom: 20px;
        }
    
        .content-box-demo {
          box-sizing: content-box; /* default */
          width: 300px;
          padding: 20px;
          border: 5px solid #ef4444;
       
    ...

    The display Property

    The display property controls how an element flows in the page. The three most common values are block, inline, and inline-block.

    ValueTakes Full Width?Width/Height Work?Examples
    blockYes โ€” new lineโœ… Yesdiv, p, h1โ€“h6, section
    inlineNo โ€” same lineโŒ Nospan, a, strong, em
    inline-blockNo โ€” same lineโœ… Yesimg, button, input

    block vs inline vs inline-block

    See how display values change element behaviour

    Try it Yourself ยป
    Code Preview
    <!DOCTYPE html>
    <html>
    <head>
      <title>Display Property</title>
      <style>
        body {
          background: #0f172a;
          font-family: system-ui, sans-serif;
          padding: 20px;
          color: #e5e7eb;
        }
    
        h3 { color: #22c55e; margin: 25px 0 8px; }
    
        .block-demo div {
          display: block;
          background: #3b82f6;
          padding: 10px;
          margin: 5px 0;
          border-radius: 4px;
          color: white;
        }
    
        .inline-demo span {
          display: inline;
          background: #8b5cf6;
          pa
    ...

    โš ๏ธ Common Mistakes

    • Forgetting box-sizing: border-box โ€” Without it, padding and border make elements larger than the width you set. Always add *, *::before, *::after { box-sizing: border-box; } to your CSS.
    • Setting width/height on inline elements โ€” span and a ignore width and height. Use display: inline-block or display: block first.
    • Collapsing margins โ€” When two vertical margins touch, they collapse into one (the larger value wins). This doesn't happen with padding.
    • Using margin for internal spacing โ€” Use padding for space inside an element; use margin for space between elements.

    ๐ŸŽ‰ Lesson Complete

    You now understand the CSS box model โ€” the foundation of every layout on the web. Key takeaways:

    • โœ… Every element has content โ†’ padding โ†’ border โ†’ margin
    • โœ… Use box-sizing: border-box for predictable sizing
    • โœ… block = full width + new line; inline = flows in text; inline-block = best of both
    • โœ… Padding = space inside; Margin = space outside

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy Policy โ€ข Terms of Service