Borders help you add lines around elements to make them stand out or separate them visually.
Border in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
CSS
selector {
border: <border-width> <border-style> <border-color>;
}The border property is a shortcut for setting width, style, and color all at once.
Common border styles include solid, dashed, and dotted.
Examples
CSS
div {
border: 2px solid black;
}CSS
p {
border: 1rem dashed blue;
}CSS
img {
border: 5px dotted red;
}Sample Program
This example shows a green solid border around a box with text centered inside. The border is 3 pixels thick.
CSS
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Border Example</title> <style> .box { width: 200px; height: 100px; border: 3px solid #4CAF50; padding: 1rem; margin: 1rem auto; font-family: Arial, sans-serif; text-align: center; line-height: 100px; color: #333; } </style> </head> <body> <div class="box">This box has a green border</div> </body> </html>
Important Notes
You can set borders on each side separately using border-top, border-right, border-bottom, and border-left.
If you only set border style or color without width, the border may not show.
Use colors with good contrast to keep borders visible and accessible.
Summary
Borders add lines around elements to separate or highlight them.
Use the border shorthand to set width, style, and color in one line.
Common styles are solid, dashed, and dotted.
Practice
1. What does the CSS
border property do?easy
Solution
Step 1: Understand the purpose of the border property
The border property in CSS is used to add a visible line around an element.Step 2: Compare with other options
Other options describe unrelated CSS properties like background color or font size, which are not related to borders.Final Answer:
Adds a line around an element to separate or highlight it -> Option BQuick Check:
Border = line around element [OK]
Hint: Border means line around element edges [OK]
Common Mistakes:
- Confusing border with background color
- Thinking border changes text size
- Assuming border removes element
2. Which of the following is the correct CSS syntax to set a solid red border of 2px thickness?
easy
Solution
Step 1: Recall the correct order in border shorthand
The correct order is border-width, border-style, then border-color.Step 2: Check each option's order
border: 2px solid red; follows the correct order: 2px (width), solid (style), red (color). Others have wrong order or invalid syntax.Final Answer:
border: 2px solid red; -> Option DQuick Check:
Width Style Color order = correct [OK]
Hint: Remember: width style color order in border [OK]
Common Mistakes:
- Mixing order of width, style, and color
- Using invalid units or missing units
- Putting color before style
3. What will be the visible border style of this CSS rule?
div { border: 3px dashed blue; }medium
Solution
Step 1: Read the border shorthand values
The rule sets border width to 3px, style to dashed, and color to blue.Step 2: Match the description to the options
The option describing a 3px thick dashed blue border correctly matches the CSS rule. Others mismatch style, color, or visibility.Final Answer:
3px thick dashed blue border -> Option AQuick Check:
3px dashed blue = dashed border [OK]
Hint: Match border style word exactly (solid, dashed, dotted) [OK]
Common Mistakes:
- Confusing dashed with dotted or solid
- Ignoring the color specified
- Assuming no border if style is unknown
4. Identify the error in this CSS border declaration:
p { border: 5px dotted; }medium
Solution
Step 1: Check the border shorthand completeness
The border shorthand requires width, style, and optionally color. Here, color is missing.Step 2: Verify other options
Width unit '5px' is correct, 'dotted' is valid style, and border can be applied to paragraphs.Final Answer:
Missing border color value -> Option CQuick Check:
Border shorthand needs width, style, color [OK]
Hint: Border shorthand needs width, style, and color [OK]
Common Mistakes:
- Forgetting to add color in border shorthand
- Thinking dotted is invalid style
- Assuming border can't be on text elements
5. You want to create a responsive card with a border that changes thickness on smaller screens. Which CSS approach correctly applies a 4px solid black border normally, and a 2px solid black border on screens narrower than 600px?
hard
Solution
Step 1: Identify correct selector syntax
Classes require a dot prefix, so '.card' is correct, 'card' without dot is invalid.Step 2: Check media query logic for screen width
To apply styles on screens narrower than 600px, use max-width: 600px. Options using min-width apply to wider screens.Final Answer:
.card { border: 4px solid black; } @media (max-width: 600px) { .card { border: 2px solid black; } } -> Option AQuick Check:
Class selector + max-width media query = correct [OK]
Hint: Use .class selector and max-width for smaller screens [OK]
Common Mistakes:
- Missing dot for class selector
- Using min-width instead of max-width for smaller screens
- Incorrect nesting of media query rules
