CSS Backgrounds & Borders
Master the essential properties for styling backgrounds and borders.
1. `background-color`
Sets the background color of an element.
.box {
background-color: lightblue;
padding: 20px;
}
2. `background-image`
Adds an image as the background of an element.
.box {
background-image: url('https://via.placeholder.com/300');
background-size: cover;
padding: 20px;
}
3. `border`
Adds borders around an element.
.box {
border: 3px solid red;
padding: 20px;
}
4. `border-radius`
Rounds the corners of an element.
.box {
border: 2px solid black;
border-radius: 15px;
padding: 20px;
}
5. `box-shadow`
Adds shadow around an element for a raised effect.
.box {
box-shadow: 5px 5px 15px rgba(0,0,0,0.3);
padding: 20px;
}
6. `background-repeat`
Controls how the background image is repeated.
.box {
background-image: url('https://via.placeholder.com/50');
background-repeat: no-repeat;
padding: 20px;
}
7. `background-size`
Defines how the background image is scaled.
.box {
background-image: url('https://via.placeholder.com/50');
background-size: contain;
padding: 20px;
}
8. `border-style`
Specifies the style of the border, e.g., solid, dotted, dashed.
.box {
border: 3px dotted green;
padding: 20px;
}
9. `border-width`
Specifies the thickness of the border.
.box {
border: 10px solid orange;
padding: 20px;
}
10. `background-position`
Defines the position of the background image.
.box {
background-image: url('https://via.placeholder.com/300');
background-position: center;
background-size: cover;
padding: 20px;
}
11. `border-color`
Defines the color of the border.
.box {
border: 3px solid;
border-color: blue;
padding: 20px;
}
12. `background-attachment`
Defines whether the background image scrolls with the page or is fixed.
.box {
background-image: url('https://via.placeholder.com/300');
background-attachment: fixed;
background-size: cover;
}
13. `background-clip`
Specifies whether the background extends into the border or padding.
.box {
border: 3px solid black;
background-color: lightgreen;
background-clip: content-box;
padding: 20px;
}
14. `border-collapse`
Specifies whether table borders are collapsed into a single border or separated.
Cell 1 | Cell 2 |
table {
border-collapse: collapse;
border: 1px solid black;
}
td {
border: 1px solid black;
padding: 10px;
}
15. `border-spacing`
Specifies the spacing between table borders when not collapsed.
Cell 1 | Cell 2 |
table {
border-spacing: 15px;
border: 1px solid black;
}
td {
border: 1px solid black;
padding: 10px;
}
16. `border-image`
Applies an image as the border of an element.
.box {
border: 15px solid transparent;
border-image: url('https://via.placeholder.com/50') 30 round;
padding: 20px;
}
17. `background-blend-mode`
Defines how background layers blend together.
.box {
background: url('https://via.placeholder.com/150') no-repeat, lightblue;
background-blend-mode: multiply;
padding: 20px;
}
18. `border-radius` with percentage
Creates circular or elliptical borders using percentages.
.box {
border: 2px solid black;
border-radius: 50%;
padding: 20px;
}
19. `box-decoration-break`
Specifies how box decorations (e.g., borders, padding) behave across multiple lines.
.box {
border: 2px solid black;
padding: 10px;
box-decoration-break: clone;
}