Backgrounds and Borders
๐จ Backgrounds and Borders
In CSS, backgrounds and borders are used to
visually style elements and give your web pages structure and aesthetics.
๐ฒ Background Properties
CSS allows you to control the background of an
element using several properties:
selector {
background-color:
#f0f0f0;
/* Sets background color */
background-image:
url(
'image.jpg');
/* Sets background image */
background-repeat: no-repeat;
/* Prevents image from repeating */
background-position: center center;
/* Positions background */
background-size: cover;
/* Scales image to cover element */
background-attachment: fixed;
/* Fixes background when scrolling */
}
Shorthand:
background:
#f0f0f0
url(
'image.jpg') no-repeat center center / cover fixed;
๐งฑ Border Properties
CSS borders add lines around elements.
selector {
border-width:
2px;
/* Thickness */
border-style: solid;
/* solid, dashed, dotted, double, none */
border-color:
#333;
/* Color of the border */
}
Shorthand:
border:
2px solid
#333;
You can also style individual sides:
border-top:
1px dashed red;
border-right:
2px solid blue;
border-bottom: none;
border-left:
3px dotted green;
๐ Border Radius
To make rounded corners:
border-radius:
10px;
/* Applies to all corners */
border-radius:
10px
5px
15px
0;
/* top-left, top-right, bottom-right, bottom-left */
๐งช Example
<div
style=
"
background: #eee url('pattern.png') repeat;
border: 2px solid #444;
border-radius: 10px;
padding: 20px;">
This box has a background, border, and rounded corners.
</div>