使CSS伪元素:在与主元素相同的高度之前

栏目: CSS · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/26449590/make-css-pseudoelement-before-same-height-as-main-element
我一直在寻找并且未能自己找到解决方案.我正在尝试创建一个基本的可满足代码编辑器,对于行号,我选择在CSS伪元素中使用一个计数器集的每一行使用一个段落.这是小提琴: http://jsfiddle.net/zppb29jw/

问题是,如果段落稍微长一点,文本的其余部分将在我的计数器伪元素下面.我想拉伸:在计数器之前与段落的高度相同.

我试过在段落和位置上使用position:relative:absolute;高度:p = 100%之前的伪元素,如下所述: How can the pseudo element detect the height of the non-pseudo element?

这在我的情况下不起作用,因为我不希望p:before元素重复并覆盖段落,我只想要与现在相同的行为,只是希望p:before元素拉伸到与之相同的高度主要的

我也不希望线条拉伸超过包装容器的宽度.我一直在尝试很多方法,但未能找到解决方案.

而不是高度使用位置:p的相对值和之前的绝对值

Js Fiddle

在css中添加了新属性

.editor p {
    position:relative;
    padding-left:3.5em;
}

.editor p:before {
    position:absolute;
    top:0;
    bottom:0;
    left:0;
 }

编辑

这应该是第二个问题:D

按下输入即不创建br而在现代浏览器中它使用以下方法创建br:修复问题以便p标记不会保持为空

Js Fiddle

.editor {
  display: inline-block;
  border: 1px black solid;
  font-family: "Consolas", "Monaco", "Courier New", monospace;
  counter-reset: line;
  width: 90%;
  height: 350px;
  overflow: scroll;
  padding-left: 0;
  margin-left: 0;
  z-index: 1;
}
.editor p {
  display: block;
  counter-increment: line;
  background-color: #FFF;
  text-align: left;
  margin: 0px;
  z-index: 2;
  outline: none;
  position: relative;
  padding-left: 3.5em;
}
.editor p:before {
  display: inline-block;
  width: 2em;
  height: 100%;
  border-right: 1px black solid;
  padding-right: 1em;
  margin-right: 1em;
  content: counter(line);
  color: #FFF;
  background-color: #006;
  text-align: right;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  /*-webkit-user-select: none;
    user-select: none;*/
}
.editor p:after {
  content: " "
}
<div class="editor" contenteditable="true" spellcheck="false">
  <p>Some paragraph</p>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas aliquet nunc non pulvinar luctus. Cras finibus turpis at arcu mollis, nec fermentum mi pretium. Aliquam suscipit lacus sapien, eu fringilla enim malesuada quis. Sed ut tincidunt erat.
    In posuere vulputate venenatis. Mauris quis porta magna. Phasellus pharetra in nisl et luctus. Etiam in ultrices risus. Morbi vel dapibus ex. Suspendisse gravida libero non malesuada congue. Pellentesque ut nunc diam.</p>
  <p>one</p>
  <p>two</p>
  <p>three</p>
</div>

翻译自:https://stackoverflow.com/questions/26449590/make-css-pseudoelement-before-same-height-as-main-element


以上所述就是小编给大家介绍的《使CSS伪元素:在与主元素相同的高度之前》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

新零售:低价高效的数据赋能之路

新零售:低价高效的数据赋能之路

刘润 / 中信出版集团 / 2018-9 / 65.00元

小米新零售,如何做到20倍坪效? 天猫小店,如何利用大数据助力线下零售? 盒马鲜生,为什么坚持必须用App才能买单? 名创优品,实体小店在电商冲击下,如何拥抱春天? 新零售的未来在何方?什么样的思维模式才可应对? 新零售,不是商界大佬的专用名词,它就在我们生活触手可及的各个角落——小到便利店的酸奶,大到京东商城的冰箱,都蕴含着消费者、货物、经营场所三者共同作用的经济逻......一起来看看 《新零售:低价高效的数据赋能之路》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

MD5 加密
MD5 加密

MD5 加密工具