Table of Contents
Understanding background attributes
Set different background properties in one statement
grammar
Example 1: Set background image
Setting a background image in the body element
Example 2: Set gradient background
Welcome to TutorialsPoint
Setting the Gradient Background in the body element
Example 3 - Setting a background image in a div element
Setting the Background image for the div element
in conclusion
Home Web Front-end HTML Tutorial How to set different background properties in one statement?

How to set different background properties in one statement?

Sep 15, 2023 am 09:45 AM
Attributes statement Background settings

How to set different background properties in one statement?

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.

Understanding background attributes

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.

Set different background properties in one statement

Abbreviation attribute 'background' is used to set multiple background attributes, which allows setting background color, image, repeat, position and attachment in one statement.

grammar

1

2

3

selector {

   background: [background-color] [background-image] [background-repeat] [background-position] [background-size] [background-attachment];

}

Copy after login

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.

Example 1: Set background image

Here we will set the background image in the body element using the "background" shorthand attribute.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<!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 id="Setting-a-background-image-in-the-body-element">Setting a background image in the body element</h3>

</body>

</html>

Copy after login

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.

Example 2: Set gradient background

Here, we will use the background shorthand attribute to set a gradient background in the body element.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<!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 id="Welcome-to-TutorialsPoint">Welcome to TutorialsPoint</h1>

   <h3 id="Setting-the-Gradient-Background-in-the-body-element">Setting the Gradient Background in the body element</h3>

</body>

</html>

Copy after login

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.

Example 3 - Setting a background image in a div element

Here we will set the background image in the div element using the "background" shorthand attribute.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<!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 id="Setting-the-Background-image-for-the-div-element">Setting the Background image for the div element</h2>

   <div></div>

</body>

</html>

Copy after login

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 conclusion

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get integer literal properties in Python without SyntaxError? How to get integer literal properties in Python without SyntaxError? Aug 20, 2023 pm 07:13 PM

TogetintliteralattributeinsteadofSyntaxError,useaspaceorparenthesis.TheintliteralisapartifNumericLiteralsinPython.NumericLiteralsalsoincludesthefollowingfourdifferentnumericaltypes−int(signedintegers)−Theyareoftencalledjustintegersorints,arepositiveo

How to rename properties of JSON using Gson in Java? How to rename properties of JSON using Gson in Java? Aug 27, 2023 pm 02:01 PM

The Gson@SerializedName annotation can be serialized to JSON and have the provided name value as its field name. This annotation can override any FieldNamingPolicy, including the default field naming policy that may have been set on the Gson instance. Different naming strategies can be set using the GsonBuilder class. Syntax@Retention(value=RUNTIME)@Target(value={FIELD,METHOD})public@interfaceSerializedNameExample importcom.google.gson.annotations.*;

Python's dir() function: View the properties and methods of an object Python's dir() function: View the properties and methods of an object Nov 18, 2023 pm 01:45 PM

Python's dir() function: View an object's properties and methods, specific code example required Summary: Python is a powerful and flexible programming language, and its built-in functions and tools provide developers with many convenient features. One of the very useful functions is the dir() function, which allows us to view the properties and methods of an object. This article will introduce the usage of the dir() function and demonstrate its functions and uses through specific code examples. Text: Python’s dir() function is a built-in function.

win7 folder background setting tutorial win7 folder background setting tutorial Jan 07, 2024 pm 10:41 PM

The default folder background of win7 system is pure white. If we are not used to it or don’t like it, it can actually be modified. We only need to enter the window color in personalization. Let’s take a look at win7 files. Here’s the background setting tutorial. How to set the folder background in win7 1. First click on a blank space on the desktop and find "Personalization" at the bottom of the drop-down menu. 2. After entering the personalization settings, find and click "Window Color" below, as shown in the picture. 3. After entering the window color settings, go to the bottom of the interface and select "Advanced Appearance Settings" 4. Click the menu below the item and select "Window" 5. Then you can change the folder background color in the color on the right.

What to do if Win11 disk properties are unknown What to do if Win11 disk properties are unknown Jul 03, 2023 pm 04:17 PM

What should I do if the disk properties of Win11 are unknown? Recently, Win11 users found that the system prompted a disk error when using their computers. What is going on? And how to solve it? Many friends don’t know how to operate in detail. The editor has compiled the steps to solve the Win11 disk error below. If you are interested, follow the editor to read below! Steps to solve Win11 disk error 1. First, press the Win+E key combination on the keyboard, or click the File Explorer on the taskbar; 2. In the right sidebar of the File Explorer, find the side and right-click the local disk (C :), in the menu item that opens, select Properties; 3. Local disk (C:) Properties window, switch to Tools

bottom attribute syntax in CSS bottom attribute syntax in CSS Feb 21, 2024 pm 03:30 PM

Bottom attribute syntax and code examples in CSS In CSS, the bottom attribute is used to specify the distance between an element and the bottom of the container. It controls the position of an element relative to the bottom of its parent element. The syntax of the bottom attribute is as follows: element{bottom:value;} where element represents the element to which the style is to be applied, and value represents the bottom value to be set. value can be a specific length value, such as pixels

Introduction to the attributes of Hearthstone's Despair Thread Introduction to the attributes of Hearthstone's Despair Thread Mar 20, 2024 pm 10:36 PM

Thread of Despair is a rare card in Blizzard Entertainment's masterpiece &quot;Hearthstone&quot; and has a chance to be obtained in the &quot;Wizbane's Workshop&quot; card pack. Can consume 100/400 arcane dust points to synthesize the normal/gold version. Introduction to the attributes of Hearthstone's Thread of Despair: It can be obtained in Wizbane's workshop card pack with a chance, or it can also be synthesized through arcane dust. Rarity: Rare Type: Spell Class: Death Knight Mana: 1 Effect: Gives all minions a Deathrattle: Deals 1 damage to all minions

Methods and examples of dynamically adding attributes using the Vue.set function Methods and examples of dynamically adding attributes using the Vue.set function Jul 24, 2023 pm 07:22 PM

Methods and examples of using the Vue.set function to dynamically add properties. In Vue, if we want to dynamically add a property to an existing object, we usually use the Vue.set function. The Vue.set function is a global method provided by Vue.js, which can ensure responsive updates when adding properties. This article will introduce the use of Vue.set and provide a specific example. First of all, in Vue, we usually use the data option to declare responsive data.

See all articles