{ "$schema": "../schemas/code-crispies-module-schema.json", "id": "box-model", "title": "CSS Box Model", "description": "Master the fundamental principles of space management in web design through the CSS box model. This module explores how content, padding, borders, and margins combine to create layout structures that are both visually appealing and structurally sound.", "difficulty": "beginner", "lessons": [ { "id": "box-model-1", "title": "Padding", "description": "Every element in CSS is a box with four layers: content, padding, border, and margin. Padding creates breathing room between your content and the box's edge.

Without padding, text presses against borders awkwardly. Padding makes content readable and visually balanced.

.card {\n  padding: 1rem;\n}
", "task": "The text inside this profile card is pressed right against the edges. Give it some inner breathing room.", "previewHTML": "

Sarah Chen

Frontend Developer

", "previewBaseCSS": "body { font-family: system-ui, sans-serif; padding: 1rem; background: #f5f5f5; } .card { background: white; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); } .card h3 { margin: 0 0 4px; } .card p { margin: 0; color: #666; }", "sandboxCSS": "", "codePrefix": ".card {\n ", "initialCode": "", "codeSuffix": "\n}", "solution": "padding: 1rem;", "previewContainer": "preview-area", "validations": [ { "type": "property_value", "value": { "property": "padding", "expected": "1rem" }, "message": "Which property adds space between content and the element's edge?" } ] }, { "id": "box-model-2", "title": "Borders", "description": "Borders create visual boundaries around elements. The border shorthand takes three values: width, style, and color.

Common styles: solid, dashed, dotted, none", "task": "This card could use a colored accent line along its left edge.", "previewHTML": "

Sarah Chen

Frontend Developer

", "previewBaseCSS": "body { font-family: system-ui, sans-serif; padding: 1rem; background: #f5f5f5; } .card { background: white; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); padding: 1rem; } .card h3 { margin: 0 0 4px; } .card p { margin: 0; color: #666; }", "sandboxCSS": "", "codePrefix": ".card {\n ", "initialCode": "", "codeSuffix": "\n}", "solution": "border-left: 4px solid steelblue;", "previewContainer": "preview-area", "validations": [ { "type": "regex", "value": "border-left:\\s*4px\\s+solid\\s+steelblue", "message": "Use the shorthand that sets a border on just one side", "options": { "caseSensitive": false } } ] }, { "id": "box-model-3", "title": "Margins", "description": "Margins create space outside the element, separating it from neighbors. While padding pushes content inward, margins push other elements away.", "task": "These two profile cards are touching each other. Add some space below each card to separate them.", "previewHTML": "

Sarah Chen

Frontend Developer

Alex Rivera

UX Designer

", "previewBaseCSS": "body { font-family: system-ui, sans-serif; padding: 1rem; background: #f5f5f5; } .card { background: white; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); padding: 1rem; border-left: 4px solid steelblue; } .card h3 { margin: 0 0 4px; } .card p { margin: 0; color: #666; }", "sandboxCSS": "", "codePrefix": ".card {\n ", "initialCode": "", "codeSuffix": "\n}", "solution": "margin-bottom: 1rem;", "previewContainer": "preview-area", "validations": [ { "type": "property_value", "value": { "property": "margin-bottom", "expected": "1rem" }, "message": "Which property pushes neighboring elements away from the bottom?" } ] }, { "id": "box-model-4", "title": "Box Sizing", "description": "By default, width only sets the content width. Padding and borders add to the total. This causes layout headaches.

box-sizing: border-box includes padding and border in the width, making sizing predictable. Most developers apply this to all elements.", "task": "Both cards are set to the same width, but the left one overflows because padding and border are added on top. Fix the right card so its size includes padding and border.", "previewHTML": "
Content-box
Border-box
", "previewBaseCSS": "body { font-family: system-ui, sans-serif; padding: 1rem; background: #f5f5f5; } .demo { display: flex; gap: 1rem; } .card { width: 200px; padding: 1rem; border: 4px solid steelblue; background: white; border-radius: 8px; }", "sandboxCSS": "", "codePrefix": ".fix {\n ", "initialCode": "", "codeSuffix": "\n}", "solution": "box-sizing: border-box;", "previewContainer": "preview-area", "validations": [ { "type": "property_value", "value": { "property": "box-sizing", "expected": "border-box" }, "message": "Which sizing mode includes padding and border in the element's width?" } ] }, { "id": "box-model-5", "title": "Padding Shorthand", "description": "Padding accepts 1-4 values:
• 1 value: all sides
• 2 values: vertical | horizontal
• 4 values: top | right | bottom | left", "task": "This button feels too tight. Give it more space on the sides than on top and bottom, using the two-value shorthand.", "previewHTML": "", "previewBaseCSS": "body { font-family: system-ui, sans-serif; padding: 1rem; } .btn { background: steelblue; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 1rem; }", "sandboxCSS": "", "codePrefix": ".btn {\n ", "initialCode": "", "codeSuffix": "\n}", "solution": "padding: 8px 1rem;", "previewContainer": "preview-area", "validations": [ { "type": "regex", "value": "padding:\\s*8px\\s+1rem", "message": "Use the two-value shorthand: vertical first, then horizontal", "options": { "caseSensitive": false } } ] }, { "id": "box-model-6", "title": "Margin Shorthand", "description": "Margin uses the same shorthand pattern as padding. A common pattern is centering block elements horizontally with margin: 0 auto.", "task": "This card is stuck to the left. Center it horizontally using the margin shorthand with auto side margins.", "previewHTML": "

Sarah Chen

Frontend Developer

", "previewBaseCSS": "body { font-family: system-ui, sans-serif; padding: 1rem; background: #f5f5f5; } .card { width: 250px; background: white; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); padding: 1rem; border-left: 4px solid steelblue; } .card h3 { margin: 0 0 4px; } .card p { margin: 0; color: #666; }", "sandboxCSS": "", "codePrefix": ".card {\n ", "initialCode": "", "codeSuffix": "\n}", "solution": "margin: 0 auto;", "previewContainer": "preview-area", "validations": [ { "type": "regex", "value": "margin:\\s*0\\s+auto", "message": "Use the shorthand that auto-calculates equal horizontal margins", "options": { "caseSensitive": false } } ] }, { "id": "box-model-7", "title": "Border Radius", "description": "While not part of the classic box model, border-radius rounds the corners of an element's border box. Use 50% on a square element to create a circle.", "task": "The square avatar image should appear as a perfect circle.", "previewHTML": "
\"Avatar\"

Sarah Chen

Frontend Developer

", "previewBaseCSS": "body { font-family: system-ui, sans-serif; padding: 1rem; background: #f5f5f5; } .card { background: white; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); padding: 1rem; text-align: center; } .avatar { width: 80px; height: 80px; background: #ddd; margin-bottom: 8px; } .card h3 { margin: 0 0 4px; } .card p { margin: 0; color: #666; }", "sandboxCSS": "", "codePrefix": ".avatar {\n ", "initialCode": "", "codeSuffix": "\n}", "solution": "border-radius: 50%;", "previewContainer": "preview-area", "validations": [ { "type": "property_value", "value": { "property": "border-radius", "expected": "50%" }, "message": "Which property rounds corners? Think about what percentage makes a circle" } ] }, { "id": "box-model-8", "title": "Complete Card", "description": "Let's combine everything. This notification card needs styling to look polished.", "task": "This notification needs three things: inner spacing so text isn't cramped, a colored accent on the left edge, and slightly rounded corners.", "previewHTML": "
New message

You have 3 unread notifications

", "previewBaseCSS": "body { font-family: system-ui, sans-serif; padding: 1rem; } .alert { background: seashell; } .alert strong { color: coral; } .alert p { margin: 4px 0 0; color: #666; }", "sandboxCSS": "", "codePrefix": ".alert {\n ", "initialCode": "", "codeSuffix": "\n}", "solution": "padding: 1rem;\n border-left: 4px solid coral;\n border-radius: 4px;", "previewContainer": "preview-area", "validations": [ { "type": "property_value", "value": { "property": "padding", "expected": "1rem" }, "message": "Add inner spacing to the notification" }, { "type": "regex", "value": "border-left:\\s*4px\\s+solid\\s+coral", "message": "Add a colored accent on the left edge", "options": { "caseSensitive": false } }, { "type": "property_value", "value": { "property": "border-radius", "expected": "4px" }, "message": "Soften the corners of the notification" } ] } ] }