[英]How to position element's background-image relative to document / body with overflow: hidden set by parent?
我想要一个作为div.child
的div.parent
的background-image
相对于body
定位, overflow: hidden
div.parent
overflow: hidden
功能。 另外,我希望div.child
top: 0
适用于body
而不是div.parent
因此div.parent
在 Y 轴上的位置无关紧要(因此, div.child
top:
不需要待调整)。
body { position: relative; margin: auto; height: 1200px; width: 100%; background-image: url("https://images.unsplash.com/photo-1531251445707-1f000e1e87d0?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=562&q=80"); background-size: 100% auto; background-position: top center; } .parent { width: 80%; height: 300px; margin: 0 auto; margin-top: 200px; outline: 2px solid white; /* !!! Not visible anymore !!! */ overflow: hidden; /* !!! Ignored by .child !!! */ } .child { position: absolute; z-index: -1; width: 100%; height: 600px; top: -200px; left: 0; background-image: url("https://images.unsplash.com/photo-1531251445707-1f000e1e87d0?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=562&q=80"); background-size: 100% auto; background-position: top center; filter: invert(1); }
<body> <div class="parent"> <div class="child"></div> </div> </body>
有两个background-images
完美匹配(相同,一个是倒置的)。 尽管如此,应用于div.parent
overflow: hidden
不再起作用。
div.parent
框由白色outline
指示。
我需要一个.parent
框,它包含与body
相同的background-image
,无论这个.parent
位于何处,始终显示应用于body
的底层background-image
的摘录。 重要提示:具有background: transparent
.parent
background: transparent
不起作用。 这是因为,我稍后将使用与应用于body
相同的background-image
,但经过编辑的background-image
(如示例小提琴中的倒置图像)。
如果您知道应用于父元素的不同值,您可以轻松计算背景属性并使子元素相对于其父元素:
body { position: relative; height: 1200px; margin:0; background-image: url("https://images.unsplash.com/photo-1531251445707-1f000e1e87d0?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=562&q=80"); background-size: 100% auto; background-position: top; overflow:auto; } .parent { width: 80%; height: 300px; margin: 0 auto; margin-top: 200px; outline: 2px solid white; position:relative; overflow:hidden; box-sizing:border-box; } .child { position: absolute; z-index: -1; /* same as border */ top:-2px; left: -2px; right:-2px; bottom:-2px; /**/ background-image: url("https://images.unsplash.com/photo-1531251445707-1f000e1e87d0?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=562&q=80"); background-size: calc(100%/0.8) auto; /*0.8 = 80% of the width */ background-position: top -200px center; /* Equal to margin*/ filter: invert(1); }
<body> <div class="parent"> <div class="child"></div> </div> </body>
您需要添加position:relative
到父 div。 检查下面的小提琴。 希望能帮助到你。
body { position: relative; margin: auto; height: 1200px; width: 100%; background-image: url("https://images.unsplash.com/photo-1531251445707-1f000e1e87d0?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=562&q=80"); background-size: 100% auto; background-position: top center; } .parent { width: 80%; height: 300px; margin: 0 auto; margin-top: 200px; outline: 2px solid white; /* !!! Not visible anymore !!! */ overflow: hidden; /* !!! Ignored by .child !!! */ /*Add Relative to parent*/ position: relative; } .child { position: absolute; z-index: -1; width: 100%; height: 600px; top: -200px; left: 0; background-image: url("https://images.unsplash.com/photo-1531251445707-1f000e1e87d0?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=562&q=80"); background-size: 100% auto; background-position: top center; filter: invert(1); }
<body> <div class="parent"> <div class="child"></div> </div> </body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.