减少两个文本块之间的间隙而不重叠背景颜色

时间:2017-04-12 20:24:54

标签: html css text background-color overlap

我想知道是否有人可以提供帮助。我正在尝试获得2块副本,这些副本将作为标题和简短描述相互搭配。它们都设置了背景颜色,但是,当我尝试将较低段落部分移动得更近(带有行高)到它重叠的<h2>时。

这就是我现在所拥有的:https://jsfiddle.net/8L9mn70x/

.container {
  position: relative;
  width: 700px;
  height: 400px;
  background-color: #666;
}

.box {
  position: absolute;
  bottom: 0;
  color: rgba(255, 255, 255, 1.00);
  padding: 50px;
}

.box h2 {
  display: inline-block;
  font-size: 40px;
  margin: 0;
  padding: 15px 15px 0 15px;
  background-color: rgba(0, 47, 95, 1.00)
}

.box p {
  display: inline-block;
  font-size: 16px;
  padding: 15px;
  margin: 0;
  background-color: rgba(0, 47, 95, 1.00);
}
<div class="container">
  <div class="box">
    <h2>What do we do?</h2>
    <p>No where else will you find this range of activities. From flying and gliding, to shooting and rock climbing, there is something for everyone!</p>
  </div>
</div>

我想将.box p移近<h2>,但是一旦超过<h2>区域的末尾,请继续使用其他背景颜色。

我希望实现的结果如图所示(间距夸大):

Desired result

也许是abosulte定位和z-index的?我不确定。任何帮助将不胜感激。

谢谢!

3 个答案:

答案 0 :(得分:1)

line-height这个想法很好,但您还需要重置vertical-align以消除下面的差距;

&#13;
&#13;
body {
	padding:0;
	margin:0;
}

.container {
	position:relative;
	width:700px;
	height:400px;
	background-color:#666;
	}

.box {
	position:absolute;
	bottom:0;
	color: rgba(255,255,255,1.00);
	padding:50px;
}

.box h2 {

  line-height:0.7em;
  vertical-align:top;
  
	display:inline-block;
	font-size:40px;
	margin:0;
	padding:25px 15px 0 15px;/* increase padding-top ? */
	background-color: rgba(0,47,95,1.00);
}

.box p {
	display:inline-block;
	font-size:16px;
	padding:15px;
	margin:0;
	background-color: rgba(0,47,95,1.00);
}
&#13;
<div class="container">
	<div class="box">
		<h2>What do we do?</h2>
   	 	<p>No where else will you find this range of activities. From flying and gliding, to shooting and rock climbing, there is something for everyone!</p>
	</div>
&#13;
&#13;
&#13;

https://jsfiddle.net/8L9mn70x/2/

答案 1 :(得分:0)

如何减少顶部/底部填充?

.box h2 {
    padding:10px 15px 0 15px;
}

.box p {
    padding: 10px 15px;
}

差距缩小,背景匹配。您还可以从此处更改line-height的{​​{1}}以进一步修改间距。

答案 2 :(得分:0)

将这一点CSS添加到h2:

line-height: 30px;
top: 5px;
position: relative;

Fiddle here