Styling Pseudo element ::after shows no line-wrap

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

Styling Pseudo element ::after shows no line-wrap

问题

  1. // styles.sass
  2. h1::after,
  3. h2::after
  4. position: absolute
  5. margin-top: -0.45em
  6. margin-left: -10px
  7. display: block
  8. width: 107%
  9. height: 0.5em
  10. background: #EFF239
  11. content: ''
  12. z-index: -1
  13. border-bottom: 2px solid #ddd
  14. h2::after
  15. background: #a5f1b1
英文:

I wanted to create an underline effect for the Headings for my blog using css. For that, I used the Pseudo element ::after for styling and was able to achieve the following results

Styling Pseudo element ::after shows no line-wrap

The problem arises when the length of the Headings exceed the single line length, it doesn't follow the wrap-around effect like the one shown below.

Styling Pseudo element ::after shows no line-wrap

The css style I used to generate this effect is

  1. // styles.sass
  2. h1::after,
  3. h2::after
  4. position: absolute
  5. margin-top: -0.45em
  6. margin-left: -10px
  7. display: block
  8. width: 107%
  9. height: 0.5em
  10. background: #EFF239
  11. content: ''
  12. z-index: -1
  13. border-bottom: 2px solid #ddd
  14. h2::after
  15. background: #a5f1b1

Could someone guide me, how I could also style the ::after element such that the underline follows the Heading text ?

答案1

得分: 2

不要使用伪元素,依赖于阴影效果:

  1. span {
  2. font-size: 30px;
  3. padding: 0 10px;
  4. box-shadow: 0 -.4em 0 #EFF239 inset;
  5. -webkit-box-decoration-break: clone;
  6. }
  1. <span>这里有一些文本 <br> 还有这里</span>
英文:

Don't use pseudo element, rely on box-shadow:

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

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

  1. span {
  2. font-size: 30px;
  3. padding: 0 10px;
  4. box-shadow: 0 -.4em 0 #EFF239 inset;
  5. -webkit-box-decoration-break: clone;
  6. }

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

  1. &lt;span&gt;Some text here &lt;br&gt; and here&lt;/span&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年8月11日 02:52:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76878600.html
匿名

发表评论

匿名网友

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

确定