CSS Background Attachment Scroll/Fixed and Background Size cover

不打扰是莪最后的温柔 提交于 2019-12-10 19:33:09

问题


Why is background-size:cover different if background-attachment: scroll or background-attachment: fixed used?

Example:

http://jsfiddle.net/enriqg9/Yn43U/


回答1:


The difference isn't really in background-size: cover. The difference between background-attachment: scroll and background-attachment: fixed is that

"...scroll means that the background is fixed with regard to the element itself and does not scroll with its contents. (It is effectively attached to the element's border.)

"...fixed means that the background is fixed with regard to the viewport. Even if an element has a scrolling mechanism, a ‘fixed’ background doesn't move with the element."

as MDN says. So you'll see in your fiddle that the background-attachment: fixed background doesn't remain in its containing element <div id="two"> border. It is, instead taking on the fixed point of absolute positioning 0, 0 in the entire body's background.

In essence, background-attachment: fixed is overwriting background-size: cover and not allowing the latter style to take effect.




回答2:


When you assign background-size:cover to a background-attachment: fixed item its container will be the actual view port the item is sitting in. In your case the cat image is stretched to fit the total width of the fiddle result box. The reason why it is this way might be because since it applies position absolutely to the viewport it also gathers the size required from the viewport.



来源:https://stackoverflow.com/questions/23050326/css-background-attachment-scroll-fixed-and-background-size-cover

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!