CSS Background
Published by
sanya sanya
The CSS backgrounds and background properties allow you to control the visual appearance of the background of an element on a webpage. The background can include colors, images, gradients, and other visual effects.
Here's an overview of CSS background properties in the descriptive manner.
Background Color
The background-color property is used to set the background color.
We can specify the color using various formats, such as named colors (e.g., "red", "blue"), hexadecimal values (e.g., "#FF0000"), RGB values (e.g., "rgb(255, 0, 0)"), or HSL values (e.g., "hsl(0, 100%, 50%)").
Background Image
The background-image property is used to set an image as the background of an element.
You can specify the image using a URL or a data URI. Multiple images can be layered by separating them with commas.
div{ background-image: url('_______'); }
Background Repeat
The background-repeat property controls how the background image is repeated or tiled within the element's background area.
Some common properties of the Background Repeat are mentioned below -
- repeat - This property sets the background image to be repeated both horizontally and vertically.
- repeat-x - The background image is repeated only horizontally.
- repeat-y - The background image gets repeated only vertically.
- no-repeat - The background image is not repeated and appears only once.
body{ background-repeat: repeat: background-repeat: repeat-x: background-repeat: repeat-y: background-repeat: no-repeat: }
Background Position
The background-position property sets the starting position of the background image within the element's background area.
We can use keywords like left, right, top, bottom, and center, or specify precise coordinates using pixels or percentages.
div{ background-position: left; background-position: right; background-position: top; background-position: bottom; }
Background Size
The background-size property is used to control the size of the background image.
We can set it to values like cover (scales the image to cover the entire background area), contain (scales the image proportionally to fit within the background area), or specific dimensions using pixels or percentages.
div{ background-size: cover; background-size: contain; background-size: 50px; }
Background Attachment
The background-attachment property determines whether the background image scrolls with the content or remains fixed. Common values are scroll (default) and fixed.
Background Shorthand
CSS provides a shorthand property called background to combine multiple background properties into a single declaration.
It allows you to specify the background color, image, repeat, position, size, and attachment in a single line.
.element { background: #F00 url('image.jpg') no-repeat top right / 50% 50% fixed; }
In the example above, the element will have a red background color (#F00), an image (image.jpg) positioned at the top right, not repeated (no-repeat), scaled to 50% width and height (50% 50%), and fixed in place (fixed).
The CSS background properties provide a wide range of options to customize and enhance the visual appearance of elements on a webpage. By using these properties creatively, you can create visually appealing backgrounds that complement your overall design.
Library
WEB DEVELOPMENT
Basic
HTML - Hyper Text Markup Language
CSS - Cascading Style Sheets
Cascading Style Sheets (CSS)
Selectors and Combinators
CSS Transforms
Units
CSS Icons and Colors
CSS Borders
CSS Margin
CSS Padding
CSS Dimensions
CSS Box Model
CSS Background
Pseudo Classes in CSS
CSS Positions
CSS Flexbox
Responsiveness in CSS
CSS Transitions
CSS Animations
Other CSS Properties
Var()
CSS Frameworks
CSS Grids
CSS Display Properties
JavaScript
Frontend
Backend
Interview Questions
FAANG QUESTIONS
On this page
Background Color
Background Image
Background Repeat
Background Position
Background Size
Background Attachment
Background Shorthand

