Home Videos Exercises MCQ Q&A Quiz E-Store Services Blog Sign in Appointment Payment

CSS Background

The CSS background properties are used to add background effects for elements.


CSS background

The background shorthand CSS property sets all background style properties at once, such as color, image, origin and size, or repeat method.

The background property in CSS allows you to control different aspects of this background, like setting the color, adding an image, and deciding how that image repeats or is positioned. It combines several settings into one line for easier styling.


CSS background-color

The background-color property specifies the background color of an element.

Example:
The background color of paragraph is set like this:
p {
background-color:red;
}


Background-attachment

The background-attachment CSS property sets whether a background image's position is fixed within the viewport or scrolls with its containing block.


Background Property Description
background-color Defines the background color of an element using color names, HEX, or RGB values.
background-image Adds one or more images as the background of an element.
background-repeat Specifies how the background image should be repeated-horizontally, vertically, or not at all.
background-attachment Controls the scrolling behavior of the background image, making it fixed or scrollable with the page
background-position Determines the initial position of the background image within the element.
background-origin Adjusts the placement of the background image retrive to the padding, border, or content box.
background-clip Sets how far the background( color or image) should extend within an element (e.g., to the padding or border)

Key points of CSS Background

1. Background Color

  • Defined with background-color, which sets a solid color as the background.
  • Uses color values like hex (#fff), RGB (rgb(255,255,255), RGBA (for transparency), HSL, and color names (red, blue, etc.)

2. Background Image

  • Set with background-image:url("image.jpg"); for adding an image as the background.
  • Multiple images can be layered with background-image:url("img1.jpg"),url("img2.jgp"); stacked from front to back

3. Background Position

  • Defines the starting position of a background image with background-position (e.g., center, top right, 10px 20px).
  • Can use keywords (left, right, top, bottom, center) or precise positioning.

4. Background Size

  • Controls image sizing with background-size, using values like cover (fills the container while maintaining aspect ratio), contain (fit entirely within the container), or specific dimensions (100px 200px).
  • Can also use percentages for scaling.

5. Background Repeat

  • Sets if/how an image repeats with background-repeat, allowing options like repeat, repeat-x, repeat-y, or no-repeat.
  • Control for repeating patterns or single images.

6. Background Attachment

  • Controls scrolling behavior with background-attachment: scroll (default, scrolls with content), fixed (stays in place during scrolling), or local (scrolls within the element it’s applied to).