英文:
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: '';
  margin-right: 7px;
  margin-left: 3px;
  border-left: 3px solid gray;
  height: 1230px;
}
<!-- language: lang-html -->
<div id="chatbox">
<div>
qwerty: 562243835622438356224383562243835622438356224383562243835622438356224383562243835622438356224383
</div>
</div>
<!-- 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: '';
  border-left: 3px solid gray;
  top:0;
  bottom:0;
  left:-3px; /* or any other offset */
  position: absolute;
  
}
<!-- language: lang-html -->
<div id="chatbox">
<div>
qwerty: 562243835622438356224383562243835622438356224383562243835622438356224383562243835622438356224383
</div>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论