Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Adding Padding to a Box
📖 Scenario: You are creating a simple webpage with a colored box. You want to add space inside the box around the text so it looks neat and not cramped.
🎯 Goal: Build a webpage with a colored box that has padding inside it. The padding should create space between the box border and the text inside.
📋 What You'll Learn
Create an HTML structure with a <div> element containing text.
Add a CSS rule to select the box by its class.
Add padding of 2rem inside the box using CSS.
Use semantic HTML and include basic meta tags for accessibility and responsiveness.
💡 Why This Matters
🌍 Real World
Padding is used in web pages to create space inside elements so content looks neat and is easy to read.
💼 Career
Web developers use padding to improve the design and user experience of websites by controlling spacing inside elements.
Progress0 / 4 steps
1
Create the HTML structure with a box
Create an HTML file with a <div> element that has the class box and contains the text "This is a box". Also include the basic HTML5 structure with <!DOCTYPE html>, <html lang="en">, <head> with charset UTF-8 and viewport meta tags, and a <body>.
CSS
Hint
Remember to include the div with class box inside the body tag.
2
Add a CSS style block and select the box
Inside the <head> section, add a <style> block. Inside it, write a CSS rule that selects the class box using .box.
CSS
Hint
Use .box { } inside the <style> block to select the box.
3
Add padding inside the box
Inside the CSS rule for .box, add a padding property and set it to 2rem. This will add space inside the box around the text.
CSS
Hint
Use padding: 2rem; inside the .box CSS rule.
4
Add background color and border for better visibility
Inside the .box CSS rule, add a background-color property with the value #cce5ff and a border property with 2px solid #004085. This will make the box visible and show the padding effect clearly.
CSS
Hint
Add background-color: #cce5ff; and border: 2px solid #004085; inside the .box CSS rule.
Practice
(1/5)
1. What does the CSS padding property do in a webpage layout?
easy
A. It sets the font size of the text inside the element.
B. It adds space outside the element's border.
C. It changes the color of the element's background.
D. It creates space inside an element between the content and its border.
Solution
Step 1: Understand padding's role
Padding adds space inside the element, between content and border, not outside.
Step 2: Differentiate from margin and other properties
Margin adds space outside the border, background color changes color, font size changes text size.
Final Answer:
It creates space inside an element between the content and its border. -> Option D
Quick Check:
Padding = space inside element [OK]
Hint: Padding is inside space, margin is outside space [OK]
Common Mistakes:
Confusing padding with margin
Thinking padding changes background color
Mixing padding with font size
2. Which of the following is the correct CSS syntax to set padding of 20 pixels on all sides of an element?
easy
A. padding: 20px;
B. padding: 20;
C. padding: 20px all;
D. padding: all 20px;
Solution
Step 1: Review CSS padding syntax
The correct way to set padding on all sides is using a single value with units, like padding: 20px;.
Step 2: Identify invalid syntax
padding: 20; misses units, padding: 20px all; and padding: all 20px; use invalid keywords.
Final Answer:
padding: 20px; -> Option A
Quick Check:
Use units and no extra keywords [OK]
Hint: Always include units like px for padding values [OK]
Common Mistakes:
Omitting units like px
Adding invalid keywords like 'all'
Using wrong order or syntax
3. Given the CSS rule:
div { padding: 10px 20px 30px 40px; }
What is the padding on the right side of the div element?
medium
A. 20px
B. 10px
C. 30px
D. 40px
Solution
Step 1: Understand padding shorthand order
Padding shorthand with four values sets padding in order: top, right, bottom, left.
All padding values must include units like px, em, %, etc. Here, 20 lacks units.
Step 2: Confirm padding accepts two values
Padding can have two values: first for top/bottom, second for left/right, but both must have units.
Final Answer:
Missing units for the second padding value. -> Option C
Quick Check:
All padding values need units [OK]
Hint: Every padding value needs units like px or em [OK]
Common Mistakes:
Forgetting units on some values
Thinking padding only accepts one value
Assuming default units if omitted
5. You want to add padding only to the left and right sides of a section element, leaving top and bottom padding at 0. Which CSS rule achieves this correctly?