Learn with Yasir

Share Your Feedback

CSS Properties - Comprehensive Guide with Syntax and Examples.


Master CSS properties with this comprehensive guide. Learn how to use background, padding, and other essential properties with syntax and practical examples to enhance your web design skills..

CSS Properties

CSS background Property โ€“ Syntax & Examples

The background property in CSS is used to set the background styles of an element, including color, image, position, size, and more.


1. Basic Syntax

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;
}

2. Examples

Example 1: Setting a Solid Background Color

body {
    background-color: lightblue;
}

This sets the background color of the entire page to light blue.


Example 2: Adding a Background Image

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.

Example 3: Fixed Background Image

body {
    background-image: url('background.jpg');
    background-attachment: fixed;
    background-size: cover;
}
  • background-attachment: fixed; keeps the image fixed when scrolling.

Example 4: Using Shorthand Background Property

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;
}

Conclusion

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 in CSS

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.


Syntax:

element {
    padding: value;  /* Can be in px, %, em, etc. */
}

Different Ways to Use Padding:

  1. Equal Padding on All Sides
    div {
        padding: 20px;  /* Adds 20px padding on top, right, bottom, and left */
    }
    
  2. Different Padding for Each Side
    div {
        padding-top: 10px;
        padding-right: 20px;
        padding-bottom: 15px;
        padding-left: 25px;
    }
    
  3. Shorthand Notation
    div {
        padding: 10px 20px 15px 25px;
        /* Order: TOP RIGHT BOTTOM LEFT */
    }
    
    • Two values: padding: 10px 20px; (Top-Bottom 10px, Left-Right 20px)
    • Three values: padding: 10px 20px 15px; (Top 10px, Left-Right 20px, Bottom 15px)
    • One value: padding: 10px; (Applies 10px padding on all sides)

Example:

<!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>

Key Points:

  • Padding increases the inner space of an element.
  • It does not affect the margin (space outside the element).
  • It can be set in various units like px, %, em, rem, etc.
  • Negative padding values are not allowed.

๐Ÿง  Practice & Progress