html - 将一个 div 元素均匀地覆盖在另外两个元素之上

标签 html css sass

enter image description here

在当前的实现中,当页面调整大小时或在移动 View 中,圆形元素会在页面上移动。我需要将圆圈永久固定在绿色和白色 div 元素之间。

.circle {
  border-radius: 50%;
  background-color: white;
  border: 4px solid pink;
  width: 40px;
  height: 40px;
  position: absolute;
  left: 46%;
  vertical-align: middle;
  top: 26px;
  z-index: 999;
}

.green {
  width: 100%;
  position: relative;
  background-color: green;
  padding: 20px;
}

.white {
  width: 100%;
  background-color: white;
  position: relative;
  padding: 20px;
}
<div>
  <div class="green"></div>
  <div class="circle"></div>
  <div class="white"></div>
<div>

最佳答案

我建议将圆移到绿色部分内并对齐到底部,或者使用::after。这种方法保证圆总是居中。 这里我使用了 `::after' 因为空元素不好,为此目的存在伪元素。

* {
box-sizing: border-box;
}
html,
body {
  margin: 0;
  padding: 0;
}
.green {
  width: 100%;
  position: relative;
  background-color: green;
  padding: 20px 20px 40px;
}
.green::after {
  content: '';
  display: block;
  border-radius: 50%;
  background-color: white;
  border: 4px solid pink;
  width: 40px;
  height: 40px;
  position: absolute;
  left: 50%;
  bottom: 0;
  transform: translate3d(-50%, 50%, 0);
  z-index: 100;
}

.white {
  width: 100%;
  background-color: white;
  position: relative;
  padding: 40px 20px 20px;
}
<div>
  <div class="green">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>
  <div class="white">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>
<div>

关于html - 将一个 div 元素均匀地覆盖在另外两个元素之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58502544/

相关文章:

html - 使用不同的选择器时,工作 CSS 不适用于元素?

html - css 定位图像和文本在顶部?

javascript - IndexedDb 的范围是什么

javascript - 单击放大 SVG 组,然后最小化

css - Jekyll 站点中未呈现的样式

twitter-bootstrap - 如何用另一个变量覆盖 Bootstrap 4 中的变量?

html - 列表样式类型 : none does not work in the removal of bullets(dots) attached with items in the navigation bar

html - 移动设备上的响应式表格缩小

html - 如何规范化 Firefox 和 Safari 中的跨浏览器 Flexbox 错误

html - 结合 Foundation css 网格列定义以供重用