
- CSS - Home
- CSS - Roadmap
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Types
- CSS - Measurement Units
- CSS - Selectors
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Border Block
- CSS - Border Inline
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursor
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS - Inline Block
- CSS - Dropdowns
- CSS - Visibility
- CSS - Overflow
- CSS - Clearfix
- CSS - Float
- CSS - Arrows
- CSS - Resize
- CSS - Quotes
- CSS - Order
- CSS - Position
- CSS - Hyphens
- CSS - Hover
- CSS - Display
- CSS - Focus
- CSS - Zoom
- CSS - Translate
- CSS - Height
- CSS - Hyphenate Character
- CSS - Width
- CSS - Opacity
- CSS - Z-Index
- CSS - Bottom
- CSS - Navbar
- CSS - Overlay
- CSS - Forms
- CSS - Align
- CSS - Icons
- CSS - Image Gallery
- CSS - Comments
- CSS - Loaders
- CSS - Attr Selectors
- CSS - Combinators
- CSS - Root
- CSS - Box Model
- CSS - Counters
- CSS - Clip
- CSS - Writing Mode
- CSS - Unicode-bidi
- CSS - min-content
- CSS - All
- CSS - Inset
- CSS - Isolation
- CSS - Overscroll
- CSS - Justify Items
- CSS - Justify Self
- CSS - Tab Size
- CSS - Pointer Events
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - Max Block Size
- CSS - Min Block Size
- CSS - Mix Blend Mode
- CSS - Max Inline Size
- CSS - Min Inline Size
- CSS - Offset
- CSS - Accent Color
- CSS - User Select
- CSS - Cascading
- CSS - Universal Selectors
- CSS - ID Selectors
- CSS - Group Selectors
- CSS - Class Selectors
- CSS - Child Selectors
- CSS - Element Selectors
- CSS - Descendant Selectors
- CSS - General Sibling Selectors
- CSS - Adjacent Sibling Selectors
- CSS Advanced
- CSS - Grid
- CSS - Grid Layout
- CSS - Flexbox
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Paged Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS - Image Sprites
- CSS - Important
- CSS - Data Types
- CSS3 Advanced Features
- CSS - Rounded Corner
- CSS - Border Images
- CSS - Multi Background
- CSS - Color
- CSS - Gradients
- CSS - Box Shadow
- CSS - Box Decoration Break
- CSS - Caret Color
- CSS - Text Shadow
- CSS - Text
- CSS - 2d transform
- CSS - 3d transform
- CSS - Transition
- CSS - Animation
- CSS - Multi columns
- CSS - Box Sizing
- CSS - Tooltips
- CSS - Buttons
- CSS - Pagination
- CSS - Variables
- CSS - Media Queries
- CSS - Functions
- CSS - Math Functions
- CSS - Masking
- CSS - Shapes
- CSS - Style Images
- CSS - Specificity
- CSS - Custom Properties
- CSS Responsive
- CSS RWD - Introduction
- CSS RWD - Viewport
- CSS RWD - Grid View
- CSS RWD - Media Queries
- CSS RWD - Images
- CSS RWD - Videos
- CSS RWD - Frameworks
- CSS References
- CSS Interview Questions
- CSS Online Quiz
- CSS Online Test
- CSS Mock Test
- CSS - Quick Guide
- CSS - Cheatsheet
- CSS - Properties References
- CSS - Functions References
- CSS - Color References
- CSS - Web Browser References
- CSS - Web Safe Fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
CSS - background Property
CSS background property is a shorthand property to set a number of background properties in one go. Properties like background color, image, position, size, repeat, origin, clip, and attachment can be set at once. This property is used for styling the appearance of the elements.
Syntax
background: bg-color bg-image position/bg-size bg-repeat bg-origin bg-clip bg-attachment | initial | inherit;
Property Values
Value | Description |
---|---|
background-color | It specifies the background color to be used. |
background-image | It specifies one or more background images to be used. |
background-position | It specifies the position of the background images. |
background-size | It specifies the size of the background images. |
background-repeat | It specifies how to repeat the background images. |
background-origin | It specifies the positioning area of the background images. |
background-clip | It specifies the painting area of the background images. |
background-attachment | It specifies the type of scroll for the background images. |
initial | This sets the property to its default value. |
inherit | This inherits the property from the parent element. |
Examples of CSS Background Property
Below listed examples explain how the background property works with its individual components.
Setting Background Color
The background-color component of background property sets the background color of the element. In the following example, background-color property has been used to set the background color of the div tag with red color.
Example
<!DOCTYPE html> <html> <head> <style> p { background-color: red; padding: 20px; } </style> </head> <body> <h2>CSS background property</h2> <div> <p> This is some text.</p> </div> </body> </html>
Setting Background Image
The background-image component of background property sets the background image of the element. In the following example, background-image property has been used to set the background image as flowers.
Example
<!DOCTYPE html> <html> <head> <style> body { margin: 0; height: 200px; background-image: url('/https/www.tutorialspoint.com/css/images/border.png'); } </style> </head> <body> <h2>CSS background property</h2> </body> </html>
Setting Background Position
The background-position component of background property sets the background positon of the element. In the following example, background-position property has been used to set the background position as center.
Example
<!DOCTYPE html> <html> <head> <style> #box { height: 200px; border: 8px solid black; padding: 25px; background-position: center; background-image: url("/https/www.tutorialspoint.com/css/images/orange-flower.jpg"); } </style> </head> <body> <h2>The background-position Property</h2> <p>Center Position</p> <div id="box"> </div> </body>
Setting Background Size
The background-size component of background property sets the background size of the element. In the following example, background-size property has been used to set the background size as cover. This will completely fit the image to the entire width.
Example
<!DOCTYPE html> <html> <head> <style> body { background-image: url("/https/www.tutorialspoint.com/css/images/orange-flower.jpg"); height:400px; background-size: cover; color:white; } </style> </head> <body> <h2>CSS background property</h2> </body> </html>
Setting Background Repeat
The background-repeat component of background property determines the repetition of the background image. In the following example, background-repeat property has been used and is given the repeat-y value. The image repeats along the cross axis.
Example
<!DOCTYPE html> <html> <head> <style> .first { padding: 30%; background-repeat: repeat-y; background-image: url("/https/www.tutorialspoint.com/css/images/logo.png"); } </style> </head> <body> <h2>CSS background property</h2> <p> Repeat along Y direction.</p> <div class="first"> </div> </body> </html>
Setting Background Origin
The background-origin component of background property determines the background's origin, the from from where the image has to start. In the following example, background-repeat property has been used and is set to content box. The image starts from the edge of the content box.
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Background Origin Example</title> <style> .box { width: 200px; height: 200px; background: url('/https/www.tutorialspoint.com/css/images/white-flower.jpg'); background-origin: content-box; padding:20px; border: 10px dashed red; } </style> </head> <body> <h2>CSS background property</h2> <div class="box"></div> </body> </html>
Setting Background Clip
The background-clip component of background property determines the painitng area of the background images. In the following example, background-clip property has been used with content box value.
Example
<!DOCTYPE html> <html> <head> <style> .box { width: 200px; height: 200px; background: url('/https/www.tutorialspoint.com/css/images/white-flower.jpg'); background-clip: content-box; padding: 20px; border: 10px dashed grey; } </style> </head> <body> <h2>CSS background property</h2> <div class="box"></div> </body> </html>
Setting Background Attachment
The background-attachment component of background property determines the type of scroll on the background images. In the following example, background-attachment property has been used with fixed value.
Example
<!DOCTYPE html> <html> <head> <style> body { height: 400px; background-image: url('/https/www.tutorialspoint.com/css/images/orange-flower.jpg'); background-size:cover; background-attachment: fixed; color: white; } </style> </head> <body> <h2>CSS background property</h2> <p>This is a sample text. The image here is fixed.</p> </body> </html>
Supported Browsers
Property | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
background | 1.0 | 4.0 | 1.0 | 1.0 | 3.5 |