我想实现这个目标。请注意顶部的文字 “快乐水果”。当它嵌套在盒子里面时,我想覆盖盒子。
body {
background: yellow;
padding: 50px;
}
.slider {
width: 100%;
height: 650px;
margin-top: 40px;
background: orange;
box-shadow: 0 0 78px 11px #F3286B;
}
h1, h2 {
text-transform:uppercase;
color: red;
}
h1 {
font-size: 11vw;
}
h2 {
font-size: 7vw;
}
<body>
<div class="slider">
<h1>
Happy Fruit
</h1>
<h2>
HELLO WORLD
</h2>
</div>
</body>
如果我然后去添加一个 margin-top: -50px;
到 h1 文本将保留在 div 内,但是我怎样才能让它在上面/站在上面 在它仍然嵌套在 (html) 中的时候?我试过使用 z-index
但没用。
position: relative; top: -50px;
最佳答案
使用绝对定位。
body {
background: yellow;
padding: 50px;
}
.slider {
width: 100%;
height: 650px;
margin-top: 40px;
background: orange;
box-shadow: 0 0 78px 11px #F3286B;
position: relative;
padding-top: 1.2em;
}
h1,
h2 {
text-transform: uppercase;
color: red;
}
h1 {
font-size: 11vw;
position: absolute;
top: -1.2em;
}
h2 {
font-size: 7vw;
}
<div class="slider">
<h1>
Happy Fruit
</h1>
<h2>
HELLO WORLD
</h2>
</div>
关于html - 在包含元素之外显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45418991/