This time to bring you CSS Background-attachment advanced use method, the use of CSS background-attachment Note what, the following is the actual case, together to see.
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: "Background picture static" background image is fixed relative to the viewport, even if the element has a scroll bar, the background image does not move with the content.
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.
And what is the viewport? Here is an article on "Pixel and browser viewport details"
four, multi-background map background-attachment can also be set for multiple background maps background-attachment
Body { Background-image:url ("Img1.png"), url ("Img2.png"); Background-attachment:scroll, fixed;}
V. Resource Links
Background-attachment
Pixel and browser viewport details
A table of Viewports
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Recommended reading:
CSS background-attachment use of the detailed
Bootstrap Realization Price List
The use of PX, EM and rem in CSS