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

标签 html css innertext

我正在尝试将样式应用于 HTML 文本之类的内容。我想要的基本上是:

What I want to get

我得到的基本上是:

What I actually get

如您所见,第一行缩进了,但其他任何行都没有。到目前为止,我的文本在 <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;
}
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>

只要标题或内容都不超过一行,它们就很好用。一旦它们超过两行或更多行,跨度就会失去其内部填充。将内部跨度更改为 display: inline-block;一旦它进入两行就给它一个 block 显示。有什么方法可以达到我想要的效果吗?

最佳答案

CSS 大师 Chris Coyier 有一篇关于 CSS 技巧的文章 listing several methods to solve this .一种方法是使用 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; 
}
<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>

关于html - 如何将 CSS 样式仅应用于文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33636856/

相关文章:

javascript - 检查 = "checked"与检查 = 真

Android点击突出显示始终为粉红色

html - 如何让文本在 CSS 中围绕不规则形状流动

javascript - Puppeteer - 如何根据内部文本选择元素?

html - 在同一表格单元格中垂直排列两个复选框

javascript - 如何在 R Shiny 中查询元素

html - 两个元素的 CSS 类没有正确响应

VBA自动化获取不到innerText(innerHTML,value)

javascript - Mozilla 中的 InnerText 替代品

ASP.NET DropDownList/HTML 选择列表默认选择