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

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

内容简介:翻译自: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伪元素:在与主元素相同的高度之前》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

Java 语言导学

Java 语言导学

Mary Campione Kalrath Alison Huml / 机械工业 / 2003-1 / 39.00元

《Java 语言导学(原书第3版)》既适合初学者,也适合有经验的程序员:新程序员通过从头到尾阅读《Java 语言导学(原书第3版)》可以得到最大的收获,包括按照第1章“起步”中的步骤说明编译和运行自己的第一个程序。有过程式语言(比如C)经验的程序员可能希望从Java编程语言的面向对象概念和特性开始学习。 有面向对象编程经验的程序员可能希望先学习更高级的内容。一起来看看 《Java 语言导学》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

html转js在线工具
html转js在线工具

html转js在线工具