html - 使用负边距 % 将元素堆叠在一起

标签 html css

一直试图让线条显示堆叠并占据父级的整个宽度,但有些东西抵消了后续线条,只有第一行(对齐到底部)正确显示:

.sp-line{
  display: inline-block;
  width: 100%;
  margin-right: -100%;
  position: relative;
  z-index: 10;
  border-top: 1px dotted #000;
}
.sp_baseline{
  border-color: blue;
  vertical-align: baseline;
}
.sp_text-bottom{
  border-color: green;
  vertical-align: text-bottom;
}
.sp_bottom{
  border-color: red;
  vertical-align: bottom;
}
.sp_text-top{
  border-color: green;
  vertical-align: text-top;
}
.line{
  border: 1px solid cyan;
}

span.text {
  background-color: papayawhip;
  line-height: 1.96; 
}
<div class="line">
  <span class="sp-line sp_bottom"></span>
  <span class="sp-line sp_baseline"></span>
  <span class="sp-line sp_text-bottom"></span>
  <span class="sp-line sp_text-top"></span>
  <span class="text1 text">SINGLE line</span>
</div>

试图到达:

enter image description here

我只是想重新创建 https://christopheraue.net/design/vertical-align 中的行我没有看到我在这里遗漏了什么。

最佳答案

无需使元素 width:100% 并使用负边距。相反,您可以将元素保持在较小的水平以将它们排成一行,并依靠溢出来让虚线覆盖所有区域:

.line {
  overflow-x:hidden;
}

.sp-line{
  display: inline-block;
  width: 2px;
  position: relative;
  z-index: 10;
}
.sp-line:before {
  content:"";
  position:absolute;
  border-top: 1px dotted;
  bottom:0;
  left:-100vw; /*big value here*/
  right:-100vw;/*big value here*/
}
.sp_baseline{
  color: blue;
  vertical-align: baseline;
}
.sp_text-bottom{
  color: green;
  vertical-align: text-bottom;
}
.sp_bottom{
  color: purple;
  vertical-align: bottom;
}
.sp_top{
  color: red;
  vertical-align: top;
  transform:translateY(1px); /*we move the top one by 1px so we can see it*/
}
.sp_text-top{
  color: green;
  vertical-align: text-top;
}

span.text {
  background-color: papayawhip;
  line-height: 1.96; 
  font-size:40px;
}
<div class="line">
  <span class="text1 text">SINGLE line</span>
  <span class="sp-line sp_bottom"></span>
  <span class="sp-line sp_top"></span>
  <span class="sp-line sp_baseline"></span>
  <span class="sp-line sp_text-bottom"></span>
  <span class="sp-line sp_text-top"></span>
</div>

关于html - 使用负边距 % 将元素堆叠在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55226693/

相关文章:

html - 对齐 asp :Button above first column of the gridview below

javascript - 从另一个页面追加元素?

java - 如何在selenium中读取PDF内容

javascript - 如何通过单击按钮显示隐藏的 div 部分

html - 覆盖动画的 CSS 类不起作用

css - 我可以使用 calc 和 var 根据宽度更改我的 div 高度吗?

html - 关于我可以获得好的 css 设计/web 2.0ish 按钮、表格等的地方的建议

html - 带有背景图像的倾斜 div 上的文本

javascript - 在选择类上使用 jQuery 不是所有具有相同类的 div

html - chrome 可以播放 html5 mp4 视频,但 html5test 说 chrome 不支持 mp4 视频编解码器