简体   繁体   English

将子元素绝对定位到页面,即使绝对定位的父元素被移动

[英]Postion a child element absolute to the page, even when the absolute positioned parent is moved

I position a container absolute to the page.我 position 一个容器绝对到页面。 In this container there is a span that I need to position absolute to the page as well, because I want to hide it if it overflows the parent container after it's moved.在这个容器中有一个跨度,我需要 position 绝对到页面,因为如果它在移动后溢出父容器,我想隐藏它。

It should look like this .它应该看起来像这样

I can only hide the span, if it is a child and the parent has its overflow hidden.我只能隐藏跨度,如果它是一个孩子并且父母隐藏了它的溢出。 But then I can't position both absolute.但是我不能 position 两者都是绝对的。

My current code looks like this:我当前的代码如下所示:

 .container { overflow: hidden; position: absolute; width: 150px; height: 50px; background-color: black; animation: move 2s ease-in-out alternate infinite; }.container span { position: absolute; left: 20px; top: 20px; color: white; font-size: 16px; } @keyframes move { 0% { top: 0; } 100% { top: 200px; } }
 <div class="container"> <span>Text</span> </div>

 .container { overflow: hidden; position: absolute; width: 150px; height: 50px; background-color: black; animation: move 2s ease-in-out alternate infinite; }.container span { position: absolute; left: 20px; top: 20px; color: white; font-size: 16px; animation: move2 2s ease-in-out alternate infinite; } @keyframes move { 0% { top: 0; } 100% { top: 200px; } } @keyframes move2 { 0% { top: 0; } 100% { top: -200px; } }
 <div class="container"> <span>Text</span> </div>

Use position:fixed with the child and consider clip-path instead of overflow to hide the overflow:使用position:fixed与孩子并考虑clip-path而不是溢出来隐藏溢出:

 .container { clip-path:polygon(0 0,100% 0,100% 100%,0 100%); position: absolute; width: 150px; height: 50px; background-color: black; animation: move 2s ease-in-out alternate infinite; }.container span { position: fixed; left: 20px; top: 20px; color: white; font-size: 16px; } @keyframes move { 0% { top: 0; } 100% { top: 200px; } } body { background:blue; }
 <div class="container"> <span>Text</span> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM