CSS (Cascading Style Sheets) is a powerful tool for designing the visual appearance of your website, including background properties. Using CSS, you can easily customize the background properties of web pages, create unique designs, and enhance user experience. Using a declaration is an efficient way to set various background properties, which for web developers helps save time and keep code clean.
Before setting multiple background properties in one declaration, we need to understand the different background properties available in CSS and understand how each property works. Below is a brief overview of each property.
Background Color − This property allows setting the background color of the element.
Background-image - This property allows setting the background image of the element. Set a background image using an image URL, linear gradient, or radial gradient.
Background-repeat − This property allows setting how the background image repeats. Can be controlled using values such as repeat, repeat-x, repeat-y and no-repeat.
Background-position − This property allows setting the position of the background image. Background images can be positioned using values such as top, bottom, left, right and center.
Background-size − This property allows setting the size of the background image. The size of the background image can be set using automatic, overlay, containment or a specific size value in pixels, ems or percentage.
Background-attachment - This property allows setting whether the background image should scroll with the page or remain fixed.
Abbreviation attribute 'background' is used to set multiple background attributes, which allows setting background color, image, repeat, position and attachment in one statement.
selector { background: [background-color] [background-image] [background-repeat] [background-position] [background-size] [background-attachment]; }
The order of the attributes here is not important, each attribute is separated by spaces. Depending on design requirements, another property can be included in the "background" shorthand property.
Example of how to set multiple background properties in one declaration.
Now, we will look at some examples of setting multiple background properties in one declaration.
Here we will set the background image in the body element using the "background" shorthand attribute.
<!DOCTYPE html> <html> <head> <style> body { background: url("https://www.tutorialspoint.com/dip/images/black_and_white.jpg") no-repeat center center fixed; } h3 { text-align: center; } </style> </head> <body> <h3>Setting a background image in the body element</h3> </body> </html>
In the above example, we set the background image and background color of the body element. We also set the background position to center and fix the background image so it doesn't move when scrolling. The "no-repeat" attribute ensures that the background image is not repeated.
Here, we will use the background shorthand attribute to set a gradient background in the body element.
<!DOCTYPE html> <html> <head> <title>Setting the Gradient Background</title> <style> body { background: linear-gradient(to top, #00cfff, #1e40ff); } h1,h3 { text-align: center; } </style> </head> <body> <h1>Welcome to TutorialsPoint</h1> <h3>Setting the Gradient Background in the body element</h3> </body> </html>
In the above example, we used the "linear-gradient" function to set the gradient background. The "to top" parameter specifies that the gradient should go from bottom to top.
Here we will set the background image in the div element using the "background" shorthand attribute.
<!DOCTYPE html> <html> <head> <style> body { text-align: center; } div { background: lightblue url("https://www.tutorialspoint.com/dip/images/einstein.jpg") no-repeat center fixed; height:300px; width:250px; margin:auto; } </style> </head> <body> <h2>Setting the Background image for the div element</h2> <div></div> </body> </html>
In the above example, we set the background image and background color of the body element. We also set the background position to center and fix the background image so it doesn't move when scrolling.
In the above article, we discussed setting background properties in a single declaration. The background attribute is an important part of web page style. We use shorthand properties to set multiple background properties in a single declaration. The background shorthand attribute is useful for saving time and improving code readability.
The above is the detailed content of How to set different background properties in one statement?. For more information, please follow other related articles on the PHP Chinese website!