background
The background shorthand CSS property sets all background style properties at once, such as color, image, origin and size, or repeat method. Component properties not set in the background shorthand property value declaration are set to their default values.
Try it
Constituent properties
This property is a shorthand for the following CSS properties:
- background-attachment
- background-clip
- background-color
- background-image
- background-origin
- background-position
- background-repeat
- background-size
Syntax
/* Using a */ background: green; /* Using a and */ background: url("test.jpg") repeat-y; /* Using a and */ background: border-box red; /* A single image, centered and scaled */ background: no-repeat center/80% url("../img/image.png"); /* Global values */ background: inherit; background: initial; background: revert; background: revert-layer; background: unset;
The background property is specified as one or more background layers, separated by commas.
The syntax of each layer is as follows:
- Each layer may include zero or one occurrences of any of the following values:
- The value may only be included immediately after , separated with the ‘/’ character, like this: » center/80% «.
- The value may be included zero, one, or two times. If included once, it sets both background-origin and background-clip . If it is included twice, the first occurrence sets background-origin , and the second sets background-clip .
- The value may only be included in the last layer specified.
Values
See background-clip and background-origin . Default: border-box and padding-box respectively.
See background-color . Default: transparent .
The following three lines of CSS are equivalent:
background: none; background: transparent; background: repeat scroll 0% 0% / auto padding-box border-box none transparent;
Accessibility concerns
Browsers do not provide any special information on background images to assistive technology. This is important primarily for screen readers, as a screen reader will not announce its presence and therefore convey nothing to its users. If the image contains information critical to understanding the page’s overall purpose, it is better to describe it semantically in the document.
- MDN Understanding WCAG, Guideline 1.1 explanations
- Understanding Success Criterion 1.1.1 | W3C Understanding WCAG 2.0
Formal definition
- background-image : none
- background-position : 0% 0%
- background-size : auto auto
- background-repeat : repeat
- background-origin : padding-box
- background-clip : border-box
- background-attachment : scroll
- background-color : transparent
- background-position : refer to the size of the background positioning area minus size of background image; size refers to the width for horizontal offsets and to the height for vertical offsets
- background-size : relative to the background positioning area
- background-image : as specified, but with url() values made absolute
- background-position : as each of the properties of the shorthand:
- background-position-x : A list, each item consisting of: an offset given as a combination of an absolute length and a percentage, plus an origin keyword
- background-position-y : A list, each item consisting of: an offset given as a combination of an absolute length and a percentage, plus an origin keyword
- background-color : a color
- background-image : discrete
- background-clip : a repeatable list
- background-position : a repeatable list
- background-size : a repeatable list
- background-repeat : discrete
- background-attachment : discrete
Formal syntax
background =
[ # , ]?=
||
[ / ]? ||
||
||
||
=
||
[ / ]? ||
||
||
||
||
=
|
none=
[ left | center | right | top | bottom | ] |
[ left | center | right | ] [ top | center | bottom | ] |
[ center | [ left | right ] ? ] && [ center | [ top | bottom ] ? ]=
[ | auto ] |
cover |
contain=
repeat-x |
repeat-y |
[ repeat | space | round | no-repeat ]=
scroll |
fixed |
local=
border-box |
padding-box |
content-box=
|=
|=
|
=
url( * ) |=
src( * )Examples
Setting backgrounds with color keywords and images
HTML
p class="topbanner"> Starry skybr /> Twinkle twinklebr /> Starry sky p> p class="warning">Here is a paragraphp> p>p>
CSS
.warning background: pink; > .topbanner background: url("starsolid.gif") #99f repeat-y fixed; >Result
Specifications
Specification CSS Backgrounds and Borders Module Level 3
# the-backgroundBrowser compatibility
BCD tables only load in the browser
See also
- box-decoration-break
- Using gradients
- Using multiple backgrounds
Found a content problem with this page?
- Edit the page on GitHub.
- Report the content issue.
- View the source on GitHub.
This page was last modified on Jul 18, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
- Product help
- Report an issue
Our communities
Developers
- Web Technologies
- Learn Web Development
- MDN Plus
- Hacks Blog
- Website Privacy Notice
- Cookies
- Legal
- Community Participation Guidelines
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.HTML: Set 4 as background
This is my layout, but it should just be the background of the page. Unfortunately if I add text to some other div then «content» the rectangle overlays the others. How can I fix this? Actually I want a menu bar which is the top «layer» and overlays all under it.
467 1 1 gold badge 4 4 silver badges 16 16 bronze badges
asked Nov 21, 2012 at 20:18
4,306 3 3 gold badges 39 39 silver badges 62 62 bronze badges
Why are you using position:relative on elements that have no absolutelty-positioned children?
Nov 21, 2012 at 20:23
position:relative is just some code I added, forget about it.
Nov 21, 2012 at 20:24
@moe2801 Go to other questions you have asked and click the checkmark under the correct answer.
Nov 21, 2012 at 20:24Ok I checked every question with an answer. I will also do it here if someone helps me.. I even would «thumb it up».
Nov 21, 2012 at 20:26
1 Answer 1
Ok, before you look at my jsFiddle-Solution:
- Be aware that using divs for such backgrounds is not a beautiful solution. Best would be using a background-image on your body-tag, which you stretch with background-size. It’s supported in all modern browsers. The only problem would be IE8 and downwards.
- Your CSS is a mess. When styling elements with similar attributes, use a class instead styling every ID by itself.
I basically created a new div with your custom content and a class on your background-divs. I also had to clean up your CSS and deleted unnecessary statements:
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quisquam excepturi laboriosam illum esse voluptas libero aperiam voluptate omnis dolor odio natus tempore sunt doloribus. Suscipit iure vel totam eius reprehenderit.How to make background of div blurred?
Ok, now I’ve been searching StackOverflow for similar issues, but none of them actually fit my case. So I have a background image that covers the entire web page, and in the center, I have a div which contains some other sub-divs (with a login form, some paragraphs, etc.) What I want is to blur only that portion of the background image that overlaps with the div. Image: So I only want the part of the image in the red rectangle to be blurred. If I just add a blur filter to the main div, it will just blur the content of it (the text and the form). Also, keep in mind that the background image is not the background of the div, but the background of the entire page.
4,584 5 5 gold badges 26 26 silver badges 54 54 bronze badges
asked Mar 15, 2017 at 7:22
user6875114 user6875114
Possible duplicate of blur of part of Backgroung image with css
Mar 15, 2017 at 7:27
And this one: stackoverflow.com/questions/27583937/…
Mar 15, 2017 at 7:306 Answers 6
background-color: rgba(0, 0, 0, 0.61); backdrop-filter: blur(5px);backdrop-filter is a CSS property used to blur the background of a specified element or area on a webpage. It can be used on divs, images and other elements. It is supported in Chrome and Firefox 103 and higher. Optional background-color can be used to make the background darker. For more information, see the official documentation. https://developer.mozilla.org/en/docs/Web/CSS/backdrop-filter
-webkit-backdrop-filter: blur(5px);answered Mar 30, 2020 at 17:00
381 4 4 silver badges 8 8 bronze badgesThis is a great solution. However please add some explanation as to how it works and why it is useful. Please see stackoverflow.com/help/how-to-answer
Jul 16, 2020 at 19:42
«Supported for Firefox» is not true (at least not without changing firefox preferences, which you cannot expect from a website visitor).
CSS Backgrounds
The CSS background properties are used to add background effects for elements.
In these chapters, you will learn about the following CSS background properties:
- background-color
- background-image
- background-repeat
- background-attachment
- background-position
- background (shorthand property)
CSS background-color
The background-color property specifies the background color of an element.
Example
The background color of a page is set like this:
body <
background-color: lightblue;
>With CSS, a color is most often specified by:
- a valid color name — like «red»
- a HEX value — like «#ff0000»
- an RGB value — like «rgb(255,0,0)»
Look at CSS Color Values for a complete list of possible color values.
Other Elements
You can set the background color for any HTML elements:
Example
Here, the ,
, and elements will have different background colors:
div background-color: lightblue;
>p background-color: yellow;
>Opacity / Transparency
The opacity property specifies the opacity/transparency of an element. It can take a value from 0.0 — 1.0. The lower value, the more transparent:
Example
div <
background-color: green;
opacity: 0.3;
>Note: When using the opacity property to add transparency to the background of an element, all of its child elements inherit the same transparency. This can make the text inside a fully transparent element hard to read.
Transparency using RGBA
If you do not want to apply opacity to child elements, like in our example above, use RGBA color values. The following example sets the opacity for the background color and not the text:
You learned from our CSS Colors Chapter, that you can use RGB as a color value. In addition to RGB, you can use an RGB color value with an alpha channel (RGBA) — which specifies the opacity for a color.
An RGBA color value is specified with: rgba(red, green, blue, alpha). The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (fully opaque).
Tip: You will learn more about RGBA Colors in our CSS Colors Chapter.
Example
div <
background: rgba(0, 128, 0, 0.3) /* Green background with 30% opacity */
>The CSS Background Color Property
Property Description background-color Sets the background color of an element
