Borders
Use borders and shadows to separate, frame, and elevate elements.
Borders and shadows are powerful CSS features that help make web pages look more attractive and professional. They allow developers to highlight important elements, separate content sections, and create depth within a design.
Think about a product card on an online shopping website. A border can outline the card, while a shadow can make it appear as if it is floating above the page. These small design details can greatly improve the user experience.
Why Use Borders and Shadows?
Borders and shadows help developers:
- Highlight important content
- Separate sections visually
- Create modern user interfaces
- Improve readability
- Add depth and visual interest
Without borders and shadows, many websites would look flat and less organized.
CSS Borders
A border is a line that surrounds an HTML element. CSS allows you to control the border's width, style, and color.
Basic Border Syntax
.box {
border: 2px solid blue;
}In this example:
- 2px is the border width.
- solid is the border style.
- blue is the border color.
This creates a blue border around the element.
Border Styles
CSS provides several border styles.
Example
.box {
border-style: dashed;
}Common border styles include:
- solid
- dashed
- dotted
- double
- groove
- ridge
Each style creates a different visual effect.
Border Radius
The border-radius property is used to create rounded corners.
Example
.button {
border-radius: 10px;
}Rounded corners are very popular in modern web design because they create a softer and more friendly appearance.
To create a perfect circle:
.avatar {
border-radius: 50%;
}This is commonly used for profile images.
CSS Box Shadows
The box-shadow property adds a shadow around an element.
Example
.card {
box-shadow: 4px 4px 10px gray;
}This creates a shadow that:
- Moves 4px horizontally
- Moves 4px vertically
- Has a blur effect of 10px
Shadows help elements stand out from the background.
Creating Modern Cards
Borders and shadows are often combined.
Example
.card {
border: 1px solid #ddd;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
padding: 20px;
}This creates a clean and modern card design commonly seen on blogs, dashboards, and e-commerce websites.
Text Shadows
CSS also allows shadows to be applied directly to text using the text-shadow property.
Example
h1 {
text-shadow: 2px 2px 4px lightgray;
}This creates a subtle shadow behind the text, making it more noticeable.
Text shadows should be used carefully because excessive shadows can reduce readability.
Real-World Example
Imagine you are designing a portfolio website:
- Add borders around project cards.
- Use rounded corners for buttons.
- Apply soft shadows to featured sections.
- Add subtle text shadows to headings.
These small enhancements make the website feel modern and polished.
Conclusion
CSS borders and shadows are simple but powerful tools for improving website design. Borders help define and separate elements, while shadows add depth and visual appeal. By using properties such as border, border-radius, box-shadow, and text-shadow, you can create professional-looking interfaces that are both attractive and easy to use. Learning these properties is an important step toward building modern and visually engaging websites.