简体   繁体   中英

CSS forced top z-index with parent overflow:hidden

I can't sort out how to move the element, which is placed under .content-wrapper{ overflow:hidden; .content{position:absolute;} } .content-wrapper{ overflow:hidden; .content{position:absolute;} } , to the very top.

Consider a screenshot below: 在此输入图像描述

An image element with man photo is placed under the .content element. But the part of his head on photo, which is highlighted with yellow (pointed with red arrow) is hidden due to the parent .content-wrapper has an overflow:hidden property. The main problem is that I can't change the hidden overflow to whatever else.

Is that actually real to solve such a problem without using a JavaScript?

==== Supplement 1 ====

To clarify the problem, I've made up a code snippet below:

 .wrapper{ width:100%; overflow:hidden; position:initial; padding:0 10px; background-color:#EEEEEE; box-sizing:border-box; } .content-wrapper{ position:relative; overflow:hidden; background-color:#DDDDDD; margin:10px 0; min-height:350px; } .content{ background-color:white; position:absolute; top:30px; left:10px; right:10px; bottom:10px; } .content.grayed{ background-color:#CCCCCC; } .content.positioned{ top:50px; left:180px; bottom:-50px; //negative positioned parts supposed to be hidden right:-50px; //as .content-wrapper has overflow:hidden; } .content.positioned img{ width:40%; height:auto; margin-top:-40vh; //but that is not supposed to be hidden out of .content-wrapper margin-left:10vw; min-width:250px; } 
 <div class="wrapper"> .wrapper <div class="content-wrapper"> .content-wrapper <div class="content grayed" style="transform: rotate(-35deg); padding:20px;"> <strong>.content</strong> with cut off edges - that is supposed behaviour </div> </div> <div class="content-wrapper"> .content-wrapper <div class="content positioned"> <strong>.content</strong> <img src="//i.imgur.com/DsOdy1V.png"> <br> ...and a man above is with sliced head - that is UNsupposed behaviour </div> </div> </div> 

Is there really no any solution?

I would consider adding the image outside and adjust the position to obtain this. Change the translation to adjust the position:

 .wrapper { width: 100%; overflow: hidden; position: relative; /*change this*/ padding: 0 10px; background-color: #EEEEEE; box-sizing: border-box; } .content-wrapper { position: relative; overflow: hidden; background-color: #DDDDDD; margin: 10px 0; min-height: 350px; } .content { background-color: white; position: absolute; top: 30px; left: 10px; right: 10px; bottom: 10px; } .content.grayed { background-color: #CCCCCC; } .content.positioned { top: 50px; left: 180px; bottom: 0; right: 0; padding: 20px calc(40% - 0.4*148px) 0 20px; /* the space of the image*/ } .content.positioned img { position: absolute; opacity: 0; /*Hide this one*/ } .hack { /*Don't use any top/bottom here !!*/ left: 190px; right: 10px; position: absolute; z-index: 1; } .hack img { width: 40%; position: absolute; right: 0; bottom: 0; transform: translateY(70%); max-width:300px; } 
 <div class="wrapper"> .wrapper <div class="content-wrapper"> .content-wrapper <div class="content grayed" style="transform: rotate(-35deg); padding:20px;"> <strong>.content</strong> with cut off edges - that is supposed behaviour </div> </div> <div class="hack"> <img src="//i.imgur.com/DsOdy1V.png"> </div> <div class="content-wrapper"> .content-wrapper <div class="content positioned"> <strong>.content</strong> <img src="//i.imgur.com/DsOdy1V.png"> <br> ...and a man above is with sliced head - that is UNsupposed behaviour </div> </div> </div> 

尝试使overflow:visible内容的外部div overflow:visible

Add a <div> like content-wrapper-inner and move the height, position from content-wrapper into it.

 .wrapper{ width:100%; overflow:hidden; position:initial; padding:0 10px; background-color:#EEEEEE; box-sizing:border-box; } .content-wrapper{ overflow:hidden; background-color:#DDDDDD; margin:10px 0; } .content{ background-color:white; position:absolute; top:30px; left:10px; right:10px; bottom:10px; } .content-wrapper-inner { min-height:350px; position:relative; background-color: red; } .content.grayed{ background-color:#CCCCCC; } .content.positioned{ top:50px; left:180px; bottom:-50px; //negative positioned parts supposed to be hidden right:-50px; //as .content-wrapper has overflow:hidden; } .content.positioned img{ width:40%; height:auto; margin-top:-40vh; //but that is not supposed to be hidden out of .content-wrapper margin-left:10vw; min-width:250px; } 
 <div class="wrapper"> .wrapper <div class="content-wrapper"> .content-wrapper <div class="content grayed" style="transform: rotate(-35deg); padding:20px;"> <strong>.content</strong> with cut off edges - that is supposed behaviour </div> </div> <div class="content-wrapper"> .content-wrapper <div class="content-wrapper-inner"> <div class="content positioned"> <strong>.content</strong> <img src="//i.imgur.com/DsOdy1V.png"> <br> ...and a man above is with sliced head - that is UNsupposed behaviour </div> </div> <div class="content positioned"> <strong>.content</strong> <img src="//i.imgur.com/DsOdy1V.png"> <br> ...and a man above is with sliced head - that is UNsupposed behaviour </div> </div> </div> 

You can't have content reach out of a parent with overflow: hidden and still find a way to show the head. The question is why you need overflow hidden on the parent.

Perhaps you could use a sibling element for the image container and limit overflow on the content container.

Something like:

HTML

 <div class="parent">
   <div class="child-content-wrapper">
     Content goes here
   </div>
   <div class="child-image">
     Image goes here
   </div>
 </div>

CSS:

 .parent {
    position: relative;
 }

 .child-content-wrapper {
    overflow: hidden;
 }

 .child-image {
    position: absolute;
    right: 0;
    bottom: 0;
    z-index: 1;
 }

That should work and you'll be able to get the cut off effect on the rotated content box and the whole head.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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