html - 在包含元素之外显示文本

标签 html css css-position positioning

我想实现这个目标。请注意顶部的文字 “快乐水果”。当它嵌套在盒子里面时,我想覆盖盒子。 enter image description here

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;

enter image description here

最佳答案

使用绝对定位。

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/

相关文章:

javascript - 如何从内容中获取数字

css - 当容器相对定位并且隐藏了溢出时,不可能流出嵌套的 div?

html - 使用位置 :absolute while keeping it inside the document flow

asp.net - 单击 asp 按钮发送电子邮件后,我的表单移动到页面左侧。不应该发生

javascript - 目标父 div 包含来自 iframe 的 iframe

html - 按最后一个元素填充行中的剩余空间

javascript - 获取光标插入符所在的选定内容可编辑元素的子元素的 ID

javascript - 给定形状列表动态创建的复杂 HTML 网格

javascript - 将 summernote 设置为默认斜体

css - 媒体查询中 "Not"运算符的问题