background
Property โ Syntax & ExamplesThe background
property in CSS is used to set the background styles of an element, including color, image, position, size, and more.
selector {
background: value;
}
You can also use individual background properties:
selector {
background-color: value;
background-image: url('image.jpg');
background-repeat: repeat | no-repeat;
background-position: top | center | bottom left;
background-size: cover | contain;
background-attachment: fixed | scroll;
}
body {
background-color: lightblue;
}
This sets the background color of the entire page to light blue.
body {
background-image: url('background.jpg');
background-repeat: no-repeat;
background-size: cover;
}
background-image
: Sets an image as the background.background-repeat: no-repeat
: Prevents the image from repeating.background-size: cover
: Ensures the image covers the full area.body {
background-image: url('background.jpg');
background-attachment: fixed;
background-size: cover;
}
background-attachment: fixed;
keeps the image fixed when scrolling.body {
background: lightblue url('background.jpg') no-repeat center/cover fixed;
}
This is equivalent to:
body {
background-color: lightblue;
background-image: url('background.jpg');
background-repeat: no-repeat;
background-position: center;
background-size: cover;
background-attachment: fixed;
}
The background
property in CSS helps style elements with colors, images, and positioning. Learning how to use it properly can make your web design visually appealing! ๐
Padding is a CSS property that adds space inside an element, between its content and its border. It helps in controlling the spacing inside elements without affecting their border or margin.
element {
padding: value; /* Can be in px, %, em, etc. */
}
div {
padding: 20px; /* Adds 20px padding on top, right, bottom, and left */
}
div {
padding-top: 10px;
padding-right: 20px;
padding-bottom: 15px;
padding-left: 25px;
}
div {
padding: 10px 20px 15px 25px;
/* Order: TOP RIGHT BOTTOM LEFT */
}
padding: 10px 20px;
(Top-Bottom 10px, Left-Right 20px)padding: 10px 20px 15px;
(Top 10px, Left-Right 20px, Bottom 15px)padding: 10px;
(Applies 10px padding on all sides)<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Padding Example</title>
<style>
.box {
background-color: lightblue;
padding: 20px; /* Adds space inside the box */
border: 2px solid blue;
width: 200px;
text-align: center;
}
</style>
</head>
<body>
<div class="box">This is a padded box</div>
</body>
</html>
px
, %
, em
, rem
, etc.