html - 文本隐藏在 div 后面

标签 html css

我有一个用 HTML/CSS 制作的云,我想在它的中心包含一些文本。但我无法定位它并让它重叠在云 div 上。

#cloud {
    height: 230px;
    margin: 40px;
    position: relative;
    width: 400px;
}

#cloud div {
    border: solid 5px black;
}

#bottom_c {
    background-color: #fff;
    border-radius: 100px;
    height: 150px;
    position: absolute; 
    top: 100px;
    width: 350px;
    z-index: 0;
}

#right_c{
    background-color: #fff;
    border-radius: 100%;
    height: 150px;
    left: 140px;
    position: absolute; 
    top: 40px; 
    width: 150px;
    z-index: -1;
}

#left_c{
    background-color: #fff;  
    border-radius: 100%;
    height: 100px;
    left: 50px;
    position: absolute; 
    top: 70px; 
    width: 100px;
    z-index: -1;
}

#cloud::before {
    background-color: white;
    border-radius: 50%;
    content: '';
    height: 100px;
    left: 55px;
    position: absolute; 
    top: 75px; 
    width: 100px;
    z-index: 1;
}

#cloud::after {
    position: absolute; top: 45px; left: 145px;
    background-color: white;
    border-radius: 50%;
    content: '';
    width: 150px;
    height: 150px;
    z-index: 1;
}

.text {
  overflow: hidden;
  position: absolute;
  z-index: -2;
}
<div id="cloud">
  <div id="bottom_c"></div>
  <div id="right_c">
    <p class="text">
      this is some text
    </p>
  </div>
  <div id="left_c">

  </div>
</div>

不完全确定如何让它发挥作用,我认为 z-index 会允许文本“优先”并首先显示,但它似乎不起作用。我需要在另一个 div 中插入 p 标签吗?如有任何建议,我们将不胜感激!

最佳答案

从其当前嵌套级别移除文本元素。只需将其设为边框 div 的同级即可。仍然是一个整体容器。

#cloud {
    height: 230px;
    margin: 40px;
    position: relative;
    width: 400px;
}

#cloud div {
    border: solid 5px black;
}

#bottom_c {
    background-color: #fff;
    border-radius: 100px;
    height: 150px;
    position: absolute; 
    top: 100px;
    width: 350px;
    z-index: 0;
}

#right_c{
    background-color: #fff;
    border-radius: 100%;
    height: 150px;
    left: 140px;
    position: absolute; 
    top: 40px; 
    width: 150px;
    z-index: -1;
}

#left_c{
    background-color: #fff;  
    border-radius: 100%;
    height: 100px;
    left: 50px;
    position: absolute; 
    top: 70px; 
    width: 100px;
    z-index: -1;
}

#cloud::before {
    background-color: white;
    border-radius: 50%;
    content: '';
    height: 100px;
    left: 55px;
    position: absolute; 
    top: 75px; 
    width: 100px;
    z-index: 1;
}

#cloud::after {
    position: absolute; top: 45px; left: 145px;
    background-color: white;
    border-radius: 50%;
    content: '';
    width: 150px;
    height: 150px;
    z-index: 1;
}

.text {
  position: absolute;
  top: 40px;
  left: 135px;
  z-index: 2;
}
<div id="cloud">
  <div id="bottom_c"></div>
  <div id="right_c"></div>
  <div id="left_c"></div>
  <p class="text">this is some text</p>  
</div>

关于html - 文本隐藏在 div 后面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48312509/

相关文章:

css - indexhibit 中的响应式水平滚动 div 的宽度太大

jquery - CSS 3D 轮播元素旋转

html - 百分比表盘颜色重叠问题

html - 如何通过 CSS 打破溢出的导航栏

css - 如何更改显示 :inline-block element? 的 z-index

javascript - 下拉菜单不起作用

javascript - 为什么在 alpha 动画之后图形在 IE 中有丑陋的黑色边框?

javascript - jQuery 验证不适用于输入数组

html - 用外部元素中断流

css - Sass 可以识别大小并应用条件吗?