如何使 ::before 出现在 div 的每一行包装线之前?

huangapple go评论62阅读模式
英文:

How to make ::before appear before every wrapped line of a div?

问题

I have a div element that has a border-left on a ::before. It works perfectly on one line, but when I wrap the line, the border-left only covers the first line. How can I spread that line to the entire height of the div element? I've tried height but that doesn't seem to change it at all.

#chatbox {
  background-color: lightgray;
  border: 1px solid gray;
  padding: 10px;
  height: 500px;
  width: 50%;
  overflow: wrap;
  word-wrap: break-word;
  border-radius: 5px;
  text-align: left;
}

#chatbox div {
  margin-bottom: 5px;
  border-radius: 5px;
  padding: 7px;
}

#chatbox div::before {
  content: '';
  margin-right: 7px;
  margin-left: 3px;
  border-left: 3px solid gray;
  height: 1230px;  /* This line sets the height of the ::before element. */
}
<div id="chatbox">
  <div>
    qwerty: 562243835622438356224383562243835622438356224383562243835622438356224383562243835622438356224383
  </div>
</div>
英文:

I have a div element that has a border-left on a ::before. It works perfectly on one line, but when I wrap the line, the border-left only covers the first line. How can I spread that line to the entire height of the div element? I've tried height but that doesn't seem to change it at all.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#chatbox {
  background-color: lightgray;
  border: 1px solid gray;
  padding: 10px;
  height: 500px;
  width: 50%;
  overflow: wrap;
  word-wrap: break-word;
  border-radius: 5px;
  text-align: left;
}

#chatbox div {
  margin-bottom: 5px;
  border-radius: 5px;
  padding: 7px;
}

#chatbox div::before {
  content: &#39;&#39;;
  margin-right: 7px;
  margin-left: 3px;
  border-left: 3px solid gray;
  height: 1230px;
}

<!-- language: lang-html -->

&lt;div id=&quot;chatbox&quot;&gt;
&lt;div&gt;
qwerty: 562243835622438356224383562243835622438356224383562243835622438356224383562243835622438356224383
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

Make the div position relative so it stays in its original place but be a "positioned element" so its child (the ::before element) can be absolute positioned relatively to it.

Make the ::before top and bottom 0 so it stretches the pseudo-element size.
Add left to make an offset.

英文:

Make the div position relative so it stays in its original place but be a "positioned element" so its child (the ::before element) can be absolute positioned relatively to it.

Make the ::before top and bottom 0 so it stretches the pseudo element size.
add left to make offset.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#chatbox {
  background-color: lightgray;
  border: 1px solid gray;
  padding: 10px;
  height: 500px;
  width: 50%;
  overflow: wrap;
  word-wrap: break-word;
  border-radius: 5px;
  text-align: left;
}

#chatbox div {
  margin-bottom: 5px;
  border-radius: 5px;
  padding: 7px;
  position: relative;
}

#chatbox div::before {
  content: &#39;&#39;;
  border-left: 3px solid gray;
  top:0;
  bottom:0;
  left:-3px; /* or any other offset */
  position: absolute;
  
}

<!-- language: lang-html -->

&lt;div id=&quot;chatbox&quot;&gt;
&lt;div&gt;
qwerty: 562243835622438356224383562243835622438356224383562243835622438356224383562243835622438356224383
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月17日 21:46:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035849.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定