css - (CSS) 高度拉伸(stretch)与顶部 : 0 and bottom: 0 with Pseudo-Element?

标签 css height pseudo-element stretch

我正在使用 float: left;float: right; 在两个容器上进行两列布局。为了在视觉上将它们与容器分开,我想通过父容器中的伪元素在它们之间画一条垂直线。

我的代码:

.twocol {
  position: relative;
}

.twocol.divided:before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  border-left: 2px dashed #000;
}

.leftcol {
  float: left;
  width: 50%;
  padding-right: 20px;
}

.rightcol {
  float: right;
  width: 50%;
  padding-left: 20px;
}

然后是 html:

<div class="twocol divided">
  <div class="leftcol">...</div>
  <div class="rightcol">...</div>
</div>

使用 top: 0;bottom: 0; 伪元素应该从父 .twocol 的顶部到底部画一条线。但是没有任何反应。

在 .twocol 内的附加 html 元素 div.divider 上使用与伪元素相同的规则效果很好并划清界线。这种技术对伪元素来说是不可能的,还是我错过了什么?

这里是jsfiddle.net/tw1vp2dg使用无效的伪元素和有效的 html 元素示例。

最佳答案

您的 .twocol:before 选择器有 display: table

display:block 或不同于table 的其他值添加到伪元素的样式中,或删除.twocol:before 选择器。

Example

关于css - (CSS) 高度拉伸(stretch)与顶部 : 0 and bottom: 0 with Pseudo-Element?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36165710/

相关文章:

html - 可以用 CSS 重建表格吗?

javascript - 我们如何提供鼠标单击功能以及鼠标悬停功能?

css - DIV 背景不跨越整个 body 的 100%

html - 使用 aria-hidden 从屏幕阅读器中隐藏伪元素?

html - CSS - 在::after 中定位内容时出现问题

html - 倾斜的 Div - 仅右侧

javascript - 由于未知原因,控制台无法识别 clientX

html - 如何防止图片溢出到div中?

html - Bootstrap 高度

html - CSS 阻止 div 高度超出视口(viewport)+滚动时使 topnav 保持不变