【问题标题】:HTML/CSS Background color behind text word wrap文本自动换行后的 HTML/CSS 背景颜色
【发布时间】:2014-07-15 11:38:14
【问题描述】:

我不完全确定如何解释这一点,但我创建了这个 JSFiddle 来显示这个问题。

.news {
    float: left;
    width: 465px;
    height: 260px;
    padding: 0px 7px;
    position: relative;
}
.news .news-title {
    float: left;
    position: relative;
    z-index: 10;
    margin: 10px 20px;
    padding: 10px;
}
.news .news-title h2 {
    color: #FFF;
    text-transform: uppercase;
    font-weight: bold;
    background: #010101;
    font-family: 'Open Sans', sans-serif;
    padding: 10px;
    display: inline;
    word-wrap: break-word;
}
.news img {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}
<div class="news">
    <div class="news-title"><h2>Lorem ipsum dolor sit amet, consectetur</h2></div>
    <img src="https://dl.dropboxusercontent.com/u/22433338/news_mc.jpg" alt="" />
</div>

<div class="news">
    <div class="news-title"><h2>Lorem ipsum dolor sit amet, consectetur</h2></div>
    <img src="https://dl.dropboxusercontent.com/u/22433338/gmod_news.jpg" alt="" />
</div>

基本上,在第一行的最后,它不会在右侧进行任何填充,而在第二行,左侧根本没有填充。

我尝试使用自动换行和空格之类的东西,但我似乎不知道如何解决这个问题。

【问题讨论】:

  • 期望的结果是什么?一个块作为背景,还是应该只覆盖文本的宽度?
  • 也许你需要添加 line-height: 3;
  • 这一切都取决于原始设计
  • remove display: inline in .news .news-title h2 { class 你的代码可以正常工作我认为不需要额外的努力
  • 这是预期的设计。 dl.dropboxusercontent.com/u/22433338/intendeddesign.jpg 这几乎是我想要的,但是从第一行的末尾和第二行的开头都缺少填充。

标签: html css text word-wrap


【解决方案1】:

看看这个

http://jsfiddle.net/mGQG4/18/

基本上你在 h2 CSS 中做了两件事

padding: 10px 0; /* change horizontal padding to 0 */
box-shadow: 10px 0 0 red, -10px 0 0 red; /* add this line */

应该按要求工作。当然,也可以将颜色从红色更改为 #010101。

【讨论】:

  • 正是我想要的。谢谢!
【解决方案2】:

这里有更新,
检查,Updated

添加

box-shadow: 10px 0 0 black, -10px 0 0 black;

.news .news-title h2

【讨论】:

  • 修复了填充问题,但第二行的文本比第一行更靠左。任何想法如何解决这个问题?
  • 将第 23 行更改为 padding-top:10px;padding-bottom:10px; (删除行填充:10px;并更改)
【解决方案3】:

对于 HTML / markdown,您可以使用:

&lt;mark style="background-color: blue; color:red;"&gt;Test-Driven Development (TDD)&lt;/mark&gt;

【讨论】:

    【解决方案4】:

    也许你需要添加line-height: 3;

    demo

    【讨论】:

      【解决方案5】:

      将背景从“.news .news-title h2”移动到“.news .news-title”;-)

      【讨论】:

      • 这只是让整个背景变黑,甚至没有文字的空间。
      • RevanProdigalKnight 在stackoverflow.com/a/24757590/3022387 .. 中有另一个解决方案,基本上是一样的。使第二行容器 NOT 与第一行一样宽的唯一方法是您必须在语义上将它们分开。或者你使用 NBSPs 来填充它.. :-( .. 像这样:“sit 
         amet”
      【解决方案6】:

      Check this fiddle

      I can see padding between images and texts on those images.
      line-height: 40px;
      

      【讨论】:

      • 这不是 OP 所要求的。
      【解决方案7】:

      正如Yogesh Sharma 在评论中指出的那样,问题在于您的.news .news-title h2 类中有display:inline。如果你删除它,它工作正常。

      JSFiddle

      【讨论】:

      【解决方案8】:

      您可以使用 box-shadow 来达到预期的效果。 记得修改 h2 上的 padding 并移除那里的水平 padding。

      CSS

      .news {
          float: left;
          width: 465px;
          height: 260px;
          padding: 0px 7px;
          position: relative;
      }
      .news .news-title {
          float: left;
          position: relative;
          z-index: 10;
          margin: 10px 20px;
      }
      .news .news-title h2 {
          color: #FFF;
          text-transform: uppercase;
          font-weight: bold;
          background: #010101;
          font-family: 'Open Sans', sans-serif;
          padding: 10px 0 10px 0;
          display: inline;
          line-height:220%;
          word-wrap: break-word;
          box-shadow: 10px 0 0 black, -10px 0 0 black;
      }
      .news img {
          position: absolute;
          top: 0;
          left: 0;
          right: 0;
          bottom: 0;
      }
      

      示例

      http://jsfiddle.net/mGQG4/27/

      【讨论】:

      猜你喜欢
      • 2012-03-23
      • 1970-01-01
      • 1970-01-01
      • 2019-02-06
      • 2019-04-22
      • 2012-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多