CSS - 包裹

时间:2018-03-02 17:42:00

标签: html css padding

我有这个简单的html模型:

<div class="grey wrap-text">
    <div id="content">
      <span contenteditable="true">
        <span class="bg">this is some text that that wraps around when there is too much text</span>
      </span>
    </div>
  </div>

它产生了这个结果:

enter image description here

CSS:

.bg {
  background-color: black;
  opacity: 0.8;
  padding: 30px 20px 30px 30px;
}

.wrap-text {
  color: white;
  line-height: 130px;
  text-align: right;
  margin: 0 auto;
  font-size: 60px;
  padding: 0px 100px 0px 600px;
}

Ofc,这种行为不是一个bug或任何东西,它只是在没有在下一行添加任何左边距的情况下环绕。

但有没有一种方法,只要跨度中的文字环绕,我就可以在'is'左侧使用与'this'之前相同的填充?

或者,如何才能让模型实现这种效果?

谢谢!

3 个答案:

答案 0 :(得分:1)

如果删除wrap-text类上的填充,则应删除由跨距和填充过多引起的间距过大。

你可以强制这个(虽然有点hacky)使用box-shadow在线条的左边和右边添加空间而不是左右边距。以下是CSS Tricks关于盒子阴影的精彩文章。

.bg {
  background-color: black;
  opacity: 0.8;
  padding: 30px 0;
  box-shadow: 20px 0 0 black, -20px 0 0 black;
}

.wrap-text {
  color: white;
  line-height: 130px;
  text-align: right;
  margin: 0 auto;
  font-size: 60px;
/*   padding: 0px 100px 0px 600px;
   */}
<div class="grey wrap-text">
    <div id="content">
      <span contenteditable="true">
        <span class="bg">this is some text that that wraps around when there is too much text</span>
      </span>
    </div>
  </div>
  
  

答案 1 :(得分:1)

使用box shadow method

&#13;
&#13;
.bg {
  color: white;
  background-color: black;
  opacity: 0.8;
  padding: 30px 0px 30px 0px;
  box-shadow: 30px 0 0 black, -30px 0 0 black;
  line-height: 100px;
  
}
&#13;
<div class="grey">
    <div id="content" class='container'>
      <span contenteditable="true" class='container'>
        <span class="bg">this is some teeeeeeeeeeeext that wraps around when there is tooasdfasdfasd much text</span>
      </span>
    </div>
  </div>
&#13;
&#13;
&#13;

答案 2 :(得分:-1)

display: inline-block上的{p> .bg可以解决问题。

<强> Problems with inline elements

span是一个内联元素。内联元素的填充边距宽度高度等属性仅部分应用。

因此,在这里,当你将填充应用于span时,这是一个内联元素,这意味着该元素保持内联而不是环绕。但是在你的图像中我可以看到它因为同一行(内联)上的不可用空间而被缠绕。因此,通常情况下,填充应用于元素的起始而不是下面,因为它是内联不是块

希望这有帮助。

&#13;
&#13;
.grey{
  background:#ddd;
}
.bg{
	padding-left:20px;
	display:inline-block;
  font-size:30px;
}
.abg{
  background:red;
}
&#13;
<div class="grey">
    <div id="content" >
      <span contenteditable="true">
        <span class="bg">
          <span class='abg'>this is some text that that wraps around when there is too much text
          </span>
         </span>
      </span>
    </div>
  </div>
&#13;
&#13;
&#13;

您可以在inline-block上使用span。不用担心。