Detailed CSS Background-attachment Property advanced

Source: Internet
Author: User
Tags set background
This article details CSS Background-attachment property advanced

The premise is to define the Background-image property, and then use Background-attachment to indicate whether the position of the background map is fixed to the viewport or as it moves with the containing block. It can be simply understood to define how the background picture moves with the scroll axis.


Value:

Scroll: Default, the background image is fixed with respect to the element, and the background moves with the page scrolling, that is, background and content bindings.
Fixed: The background image with respect to the viewport fixation, so with the page scrolling background is not moving, equivalent to the background is set on the body.
Local: The background image is fixed with respect to the element content,
Inhert: Inheritance, nothing to say.

This property can be applied to any element.

First, scroll "background graph scrolling"
Setting the Background-attachment:scroll, the background map is fixed relative to the element itself, and the content moves when the background is moved. The border attached to the element.

Local

Note:

For scroll, the general context scrolls with the content, but there is one case exception.

For elements that can be scrolled (elements set to Overflow:scroll). When Background-attachment is set to scroll, the background map does not scroll with the content of the element scrolling.

second, local "scrolling element background graph scrolling"
For elements that can be scrolled (elements set to Overflow:scroll), set background-attachment:local, the background scrolls with the content scrolling.

Because the background map is positioned relative to the element's own content, it begins to pin, and the element scrolls with the content after the scrollbar appears.


<style>p{    width:200px;    height:350px;    border:1px solid red;    Background-image:url (img/img_tree.png);    Background-repeat:no-repeat;    background-attachment:local;    Overflow:scroll;    line-height:1.5;} </style><body> <p> 1 content beyond the scroll bar 2 content will appear scroll bar 3 content exceeds the scroll bar    4 content will appear scroll bar    5 content beyond the scroll bar 6 content will appear scroll bar 7 content exceeds the scrollbar will appear scroll bar 8 content exceeds the scroll bar 9 content will appear scroll bar    10 content    beyond the scroll bar appears 11 content beyond the scroll bar 12 content will appear scroll bar 13 content exceeds the scrollbar will appear scroll bar 14 content exceeds the scroll bar 15 content will appear scroll bar    16 content    beyond the scroll bar appears 17 content out of the scrollbar will appear scroll bar 18 content exceeds the scroll bar 19 content will appear scroll bar    20 content beyond will appear scroll bar    </p></body>

Third, fixed: "The background chart is still"
The background image is fixed with respect to the viewport, and the background image does not move with the content, even if the element has a scroll bar.

The fixed usage is as follows:


<style>body{    Background-image:url (img/cartooncat.png);    Background-position:bottom left;    background-attachment:fixed;    Background-repeat:no-repeat;    height:1000px;} </style>

Or look at Mozilla's demo.

Here I would like to highlight my point of view:

Set the background of any element background-attachment:fixed; the effect is the same, is relative to the viewport, because a Web page has only one viewport, the background and elements have no matter, to say about the only element is not visible, the background map is not visible.

Four, multi-background map background-attachment
You can also set background-attachment for multiple background maps


Body {  Background-image:url ("Img1.png"), url ("Img2.png");  Background-attachment:scroll, fixed;}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.