CSS Backgrounds
Welcome to the CSS Backgrounds tutorial on codeswithpankaj.com
. In this tutorial, we will explore various CSS background properties, including background-color
, background-image
, background-repeat
, background-position
, background-size
, and background-attachment
.
CSS Background Properties
1. background-color
background-color
The background-color
property sets the background color of an element.
Example:
HTML:
2. background-image
background-image
The background-image
property sets an image as the background of an element.
Example:
HTML:
3. background-repeat
background-repeat
The background-repeat
property specifies how the background image is repeated. It can take the following values:
repeat
(default): The background image is repeated both vertically and horizontally.repeat-x
: The background image is repeated horizontally only.repeat-y
: The background image is repeated vertically only.no-repeat
: The background image is not repeated.
Example:
HTML:
4. background-position
background-position
The background-position
property sets the starting position of the background image. It can take values like left
, right
, center
, top
, bottom
, or specific pixel or percentage values.
Example:
HTML:
5. background-size
background-size
The background-size
property specifies the size of the background image. It can take values like auto
, cover
, contain
, or specific width and height values.
auto
: Default value. The background image is displayed at its original size.cover
: The background image is scaled to cover the entire container.contain
: The background image is scaled to fit within the container.
Example:
HTML:
6. background-attachment
background-attachment
The background-attachment
property sets whether a background image is fixed or scrolls with the rest of the page. It can take the following values:
scroll
(default): The background image scrolls with the page.fixed
: The background image is fixed in the viewport.local
: The background image scrolls with the element's content.
Example:
HTML:
Conclusion
CSS background properties allow you to control the background color, image, and behavior of HTML elements. By mastering these properties, you can create visually appealing web pages with customized backgrounds. Stay tuned to codeswithpankaj.com
for more tutorials and web development tips!
Last updated