css 如何将positioned absolute div与position relative重叠[复制]

smdnsysy  于 2023-08-08  发布在  其他
关注(0)|答案(4)|浏览(116)

此问题在此处已有答案

Why can't an element with a z-index value cover its child?(4个答案)
关闭24分钟前
我试图实现一个布局,其中我有一个div的位置:在它里面,我想放置另一个带有position的div:但是,我只希望绝对div中相对div外部的部分可见,而相对div内部的部分应该隐藏。如何使用CSS实现此效果?任何帮助将不胜感激!

  1. .relative-container {
  2. position: relative;
  3. width: 200px;
  4. height: 200px;
  5. background-color: red;
  6. z-index: 2;
  7. }
  8. .absolute-element {
  9. position: absolute;
  10. bottom: -10px;
  11. right: -10px;
  12. z-index: 1;
  13. width: 100px;
  14. height: 100px;
  15. background-color: blue;
  16. }

个字符

bvjveswy

bvjveswy1#

可以使用mix-blend-mode

  1. .relative-container {
  2. position: relative;
  3. width: 200px;
  4. height: 200px;
  5. background-color: red;
  6. }
  7. .absolute-element {
  8. position: absolute;
  9. bottom: -10px;
  10. right: -10px;
  11. width: 100px;
  12. height: 100px;
  13. background-color: blue;
  14. mix-blend-mode: overlay;
  15. }

个字符
或3D变换:(图片来源:How to get a child element to show behind (lower z-index) than its parent?

  1. .relative-container {
  2. position: relative;
  3. width: 200px;
  4. height: 200px;
  5. background-color: red;
  6. transform-style: preserve-3d;
  7. }
  8. .absolute-element {
  9. position: absolute;
  10. bottom: -10px;
  11. right: -10px;
  12. transform: translateZ(-10px);
  13. width: 100px;
  14. height: 100px;
  15. background-color: blue;
  16. }
  1. <div class="relative-container">
  2. <div class="absolute-element"></div>
  3. </div>

的字符串
如果您在父节点中创建堆栈上下文,其他提供的解决方案将不起作用(请参见CSS negative z-index: what does it mean?):

  1. .relative-container {
  2. position: relative;
  3. width: 200px;
  4. height: 200px;
  5. background-color: red;
  6. z-index: 0; /* creates a stacking context, (or any other stacking context creating property https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_positioned_layout/Understanding_z-index/Stacking_context )*/
  7. }
  8. .absolute-element {
  9. position: absolute;
  10. bottom: -10px;
  11. right: -10px;
  12. width: 100px;
  13. height: 100px;
  14. background-color: blue;
  15. z-index: -1;
  16. }
  1. <div class="relative-container">
  2. <div class="absolute-element">hello</div>
  3. </div>

的字符串

展开查看全部
w46czmvw

w46czmvw2#

只需从 relative 容器中删除z-index声明,并将一个负的z-index放入 absolute 容器。

  1. .relative-container {
  2. position: relative;
  3. width: 200px;
  4. height: 200px;
  5. background-color: red;
  6. }
  7. .absolute-element {
  8. position: absolute;
  9. bottom: -10px;
  10. right: -10px;
  11. z-index: -1;
  12. width: 100px;
  13. height: 100px;
  14. background-color: blue;
  15. }

个字符

展开查看全部
30byixjq

30byixjq3#

例如,你可以像z-index一样把absolute-element放在相对div后面,但是相对div本身不能有z-index,否则它将成为绝对div的z-index祖先,不能再放在后面

  1. .relative-container {
  2. position: relative;
  3. width: 200px;
  4. height: 200px;
  5. background-color: red;
  6. }
  7. .absolute-element {
  8. position: absolute;
  9. bottom: -10px;
  10. right: -10px;
  11. width: 100px;
  12. height: 100px;
  13. background-color: blue;
  14. z-index: -1;
  15. }

个字符
如果你需要红色的div有z-index,那么你可以root到另一个(可能是不可见的)父div,并将它们都设置为absolute。

  1. .relative-container {
  2. position: relative;
  3. width: 200px;
  4. height: 200px;
  5. }
  6. .red-element{
  7. position: absolute;
  8. width: 100%;
  9. height: 100%;
  10. background-color: red;
  11. z-index: 2;
  12. }
  13. .blue-element {
  14. position: absolute;
  15. bottom: -10px;
  16. right: -10px;
  17. width: 100px;
  18. height: 100px;
  19. background-color: blue;
  20. z-index: 1;
  21. }
  1. <div class="relative-container">
  2. <div class="red-element"></div>
  3. <div class="blue-element"></div>
  4. </div>

的字符串

展开查看全部
kd3sttzy

kd3sttzy4#

  1. .relative-container {
  2. position: relative;
  3. width: 200px;
  4. height: 200px;
  5. background-color: red;
  6. z-index: 2;
  7. }
  8. .absolute-element {
  9. position: absolute;
  10. bottom: -10px;
  11. right: -10px;
  12. z-index: 1;
  13. width: 100px;
  14. height: 100px;
  15. background-color: blue;
  16. clip-path:polygon(90px 0 , 100px 0, 100px 100px , 0 100px , 0 90px, 90px 90px);
  17. }

个字符

展开查看全部

相关问题