부모 요소를 자식 위에 표시하는 방법
이 질문에 이미 답변이 있습니다.
두 개의 중첩 된 CSS 요소가 있습니다. 부모가 자식 요소의 맨 위에, 즉 z 축 위에 있어야합니다. Z- 색인 설정만으로는 충분하지 않습니다.
자식에 대해 음수 Z- 색인을 설정할 수 없습니다. 그러면 실제 페이지의 페이지 컨테이너 아래에 설정됩니다. 이것이 유일한 방법입니까?
.parent {
position: relative;
width: 750px;
height: 7150px;
background: red;
border: solid 1px #000;
z-index: 1;
}
.child {
position: absolute;
background-color: blue;
z-index: 0;
color: white;
top: 0;
}
.wrapper
{
position: relative;
background: green;
z-index: 10;
}
<div class="wrapper">
<div class="parent">
parent parent
<div class="child">
child child child
</div>
</div>
</div>
z-index
자녀에 대해 부정 을 설정하고 부모에 대해 한 세트를 제거하십시오.
.parent {
position: relative;
width: 350px;
height: 150px;
background: red;
border: solid 1px #000;
}
.parent2 {
position: relative;
width: 350px;
height: 40px;
background: red;
border: solid 1px #000;
}
.child {
position: relative;
background-color: blue;
height: 200px;
}
.wrapper {
position: relative;
background: green;
height: 350px;
}
<div class="wrapper">
<div class="parent">parent 1 parent 1
<div class="child">child child child</div>
</div>
<div class="parent2">parent 2 parent 2
</div>
</div>
https://jsfiddle.net/uov5h84f/
다행히 해결책이 있습니다. 부모 용 래퍼를 추가하고이 래퍼의 Z- 색인 (예 : 10)을 변경하고 하위의 Z- 색인을 -1로 설정해야합니다.
.parent {
position: relative;
width: 750px;
height: 7150px;
background: red;
border: solid 1px #000;
z-index: initial;
}
.child {
position: relative;
background-color: blue;
z-index: -1;
color: white;
}
.wrapper {
position: relative;
background: green;
z-index: 10;
}
<div class="wrapper">
<div class="parent">parent parent
<div class="child">child child child</div>
</div>
</div>
Some of these answers do work, but setting position: absolute;
and z-index: 10;
seemed pretty strong just to achieve the required effect. I found the following was all that was required, though unfortunately, I've not been able to reduce it any further.
HTML:
<div class="wrapper">
<div class="parent">
<div class="child">
...
</div>
</div>
</div>
CSS:
.wrapper {
position: relative;
z-index: 0;
}
.child {
position: relative;
z-index: -1;
}
I used this technique to achieve a bordered hover effect for image links. There's a bit more code here but it uses the concept above to show the border over the top of the image.
You would need to use position:relative
or position:absolute
on both the parent and child to use z-index
.
Since your divs are position:absolute
, they're not really nested as far as position is concerned. On your jsbin page I switched the order of the divs in the HTML to:
<div class="child"><div class="parent"></div></div>
and the red box covered the blue box, which I think is what you're looking for.
Cracked it. Basically, what's happening is that when you set the z-index to the negative, it actually ignores the parent element, whether it is positioned or not, and sits behind the next positioned element, which in your case was your main container. Therefore, you have to put your parent element in another, positioned div, and your child div will sit behind that.
Working that out was a life saver for me, as my parent element specifically couldn't be positioned, in order for my code to work.
I found all this incredibly useful to achieve the effect that's instructed on here: Using only CSS, show div on hover over <a>
style:
.parent{
overflow:hidden;
width:100px;
}
.child{
width:200px;
}
body:
<div class="parent">
<div class="child"></div>
</div>
참고URL : https://stackoverflow.com/questions/1806421/how-to-get-a-parent-element-to-appear-above-child
'Programing' 카테고리의 다른 글
브라우저에서 런타임에 실행되는 React 버전을 어떻게 알 수 있습니까? (0) | 2020.10.17 |
---|---|
SQLite에없는 경우 수행하는 방법 (0) | 2020.10.17 |
C #에서 '널 던지기'를 허용하는 이유는 무엇입니까? (0) | 2020.10.17 |
요소의 하단 및 오른쪽 위치 가져 오기 (0) | 2020.10.17 |
C #에서 알 수없는 길이의 배열 (0) | 2020.10.16 |