如何仅将CSS样式应用于文本

Nat*_*ate 5 html css innertext

我正在尝试将样式应用于HTML文本.我想要的基本上是:

我想得到什么

我得到的基本上是:

我真正得到的

如您所见,第一行是缩进的,但不是任何其他行.到目前为止,我有一个内部的文本<span>,它嵌套在一个内部<div>.

.slide-text .text {
  background-color: rgba(0, 0, 0, .6);
  color: #FFF;
  padding: 8px 17px;
}
.slide-text .slide-title {
  font-family: "Titillium Web", Arial, Helvetica, sans-serif;
  font-weight: 700;
}
.slide-text .slide-content {
  font-family: "Open Sans", Arial, Helvetica, sans-serif;
  font-weight: 500;
}
Run Code Online (Sandbox Code Playgroud)
My HTML code is:
<div class="slide-text">
  <div class="slide-title"><span class="text">[TITLE]</span>
  </div>
  <div class="slide-content"><span class="text">[TEXT]</span>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

只要标题或内容都不超过一行,它们就能很好地工作.一旦它们越过两条或更多条线,跨度就会失去其内部衬垫.更改内部跨度,display: inline-block;一旦进入两行,就会显示一个块.有没有办法获得我想要的效果?

Jos*_*ost 5

CSS 大师 Chris Coyier 有一篇关于 CSS-Tricks 的文章,列出了解决此问题的几种方法。一种方法是使用box-shadow. 它已经作为答案被提及,但它需要更多的爱才能在现代 Firefox 中工作:)。

.multi-line-padded {
  background: black;
  color: white;
  
  /* For the top and bottom padding */
  padding: 0.5em 0; 
  
  /* Text height (1.0) + compensate for padding (0.5 * 2) */
  line-height: 2;
  
  /* For the left and right padding */
  /* Vendor prefixes FIRST */
  -webkit-box-shadow: 1em 0 0 black, -1em 0 0 black;
  -moz-box-shadow: 1em 0 0 black, -1em 0 0 black;
  box-shadow: 1em 0 0 black, -1em 0 0 black;
  
  /* Firefox defaults to `box-decoration-break: split`, we need `clone` */
  box-decoration-break: clone; 
}
Run Code Online (Sandbox Code Playgroud)
<p><span class="multi-line-padded">You think water moves fast? You should see ice. It moves like it has a mind. Like it knows it killed the world once and got a taste for murder.</span></p>

<p style="text-align: right;"><span class="multi-line-padded"><a href="http://slipsum.com/" style="color: white;">Samuel L. Ipsum</a></span></p>
Run Code Online (Sandbox Code Playgroud)