这可以仅使用CSS来完成吗? (没有JS)

时间:2016-08-06 18:05:54

标签: html css css3 flexbox

一个带有文本背景的容器,容器可以增长到适合内容或文本背景中最大的容量。

我最接近的是使用带有两个内部DIV的flexbox容器,一个用于内容,一个用于文本背景。柔性基础为100%,背景为-100%边距,因此内容可以重叠。

这种布局的问题在于背景文本中的填充来自哪里不太明显。我希望蓝色和红色的虚线相互接触,但是有填充。



.test1 {
  position: absolute;
  left: 50px;
  top: 50px;
}

.test2 {
  position: absolute;
  left: 500px;
  top: 50px;
}

.test3 {
  position: absolute;
  left: 50px;
  top: 250px;
}

.test4 {
  position: absolute;
  left: 500px;
  top: 250px;
}

.test5 {
  position: absolute;
  left: 50px;
  top: 450px;
}

.test6 {
  position: absolute;
  left: 500px;
  top: 450px;
}

.outer {
  display: flex;
  min-width: 150px;
  min-height: 150px;
  outline: dashed 3px green;
  padding: 3px;
}

.title {
  background: red;
  text-align: center;
}

.foreground {
  flex: 1 0 50%;
  
  display: flex;
  justify-content: space-between;
  align-items: center;
  outline: dashed 3px blue;
}

.left {
  background: rgba(255,255,0,.5);
}

.right {
  background: rgba(0,255,255,.5);
  text-align: right;
}

.background {
  flex: 1 0 50%;
  margin-right: -100%;

  font-size: 30px;
  outline: dashed 3px red;
  
  display: flex;
  justify-content: center;
  align-items: center;
}

.background>div {
  outline: dashed 3px red;
}

<div class="outer test1">
  <div class="background">
     <div>1 some very long text!!!</div>
  </div>

  <div class="foreground">
    <div class="left">
	  <div>A</div>
	  <div>AB</div>
	  <div>ABC</div>
	  <div>ABCD</div>
	</div>
		
    <div class="right">
	  <div>A</div>
	  <div>AB</div>
    </div>
  </div>
</div>

<div class="outer test2">  
  <div class="background">
	<div>2 text!!!</div>
  </div>

  <div class="foreground">
	<div class="left">
	  <div>A</div>
	  <div>AB</div>
	  <div>ABC</div>
	  <div>ABCD</div>
	</div>
		
    <div class="right">
	  <div>A</div>
	  <div>AB</div>
	</div>
  </div>
</div>

<div class="outer test3">  
  <div class="background">
	<div>3 some very long text!!!</div>
  </div>

  <div class="foreground">
	<div class="left">
	  <div>A</div>
	  <div>AB</div>
	  <div>ABC</div>
	  <div>ABCD</div>
	  <div>ABCD</div>
	  <div>ABCD</div>
	  <div>ABCD</div>
	  <div>ABCD</div>
	</div>
		
    <div class="right">
	  <div>A</div>
	  <div>AB</div>
	</div>
  </div>
</div>

<div class="outer test4">  
  <div class="background">
	<div>4 text!!!</div>
  </div>

  <div class="foreground">
	<div class="left">
	  <div>A</div>
	  <div>AB</div>
	  <div>ABC</div>
	  <div>ABCD</div>
	  <div>ABCD</div>
	  <div>ABCD</div>
	  <div>ABCD</div>
	  <div>ABCD</div>
	</div>
		
	<div class="right">
	  <div>A</div>
	  <div>AB</div>
	</div>
  </div>
</div>

<div class="outer test5">  
  <div class="background">
	<div>5 some very long text!!!</div>
  </div>

  <div class="foreground">
	<div class="left">
	  <div>A</div>
	  <div>AB</div>
	  <div>ABC</div>
	  <div>ABCD</div>
	  <div>ABCD</div>
	  <div>ABCD</div>
	  <div>ABCD</div>
	  <div>ABCDxxxxx</div>
	</div>
		
    <div class="right">
	  <div>A</div>
	  <div>AB</div>
	</div>
  </div>
</div>

<div class="outer test6">  
  <div class="background">
	<div>6 text!!!</div>
  </div>

  <div class="foreground">
	<div class="left">
	  <div>A</div>
	  <div>AB</div>
	  <div>ABC</div>
	  <div>ABCD</div>
	  <div>ABCD</div>
	  <div>ABCD</div>
	  <div>ABCD</div>
	  <div>ABCDxxxxx</div>
	</div>
		
	<div class="right">
	  <div>A</div>
	  <div>AB</div>
	</div>
  </div>
</div>
&#13;
&#13;
&#13;

修改

这将是预期的结果:

enter image description here

1 个答案:

答案 0 :(得分:1)

您是否希望红色容器适合其父级并相应地增加大小,此div只需要flex: 1;,并且可选text-align: center;来居中文本。

.test1 {
  position: absolute;
  left: 50px;
  top: 50px;
}

.test2 {
  position: absolute;
  left: 500px;
  top: 50px;
}

.test3 {
  position: absolute;
  left: 50px;
  top: 250px;
}

.test4 {
  position: absolute;
  left: 500px;
  top: 250px;
}

.test5 {
  position: absolute;
  left: 50px;
  top: 450px;
}

.test6 {
  position: absolute;
  left: 500px;
  top: 450px;
}

.outer {
  display: flex;
  min-width: 150px;
  min-height: 150px;
}

.title {
  background: red;
  text-align: center;
}

.foreground {
  flex: 1 0 50%;
  
  display: flex;
  justify-content: space-between;
  align-items: center;
  outline: dashed 3px blue;
}

.left {
  background: rgba(255,255,0,.5);
}

.right {
  background: rgba(0,255,255,.5);
  text-align: right;
}

.background {
  flex: 1 0 50%;
  margin-right: -100%;

  font-size: 30px;
  outline: dashed 3px red;
  
  display: flex;
  justify-content: center;
  align-items: center;
}

.background>div {
  outline: dashed 3px red;
  flex: 1;
  text-align: center;
}
<div class="outer test1">
  <div class="background">
     <div>1 some very long text!!!</div>
  </div>

  <div class="foreground">
    <div class="left">
	  <div>A</div>
	  <div>AB</div>
	  <div>ABC</div>
	  <div>ABCD</div>
	</div>
		
    <div class="right">
	  <div>A</div>
	  <div>AB</div>
    </div>
  </div>
</div>

<div class="outer test2">  
  <div class="background">
	<div>2 text!!!</div>
  </div>

  <div class="foreground">
	<div class="left">
	  <div>A</div>
	  <div>AB</div>
	  <div>ABC</div>
	  <div>ABCD</div>
	</div>
		
    <div class="right">
	  <div>A</div>
	  <div>AB</div>
	</div>
  </div>
</div>

<div class="outer test3">  
  <div class="background">
	<div>3 some very long text!!!</div>
  </div>

  <div class="foreground">
	<div class="left">
	  <div>A</div>
	  <div>AB</div>
	  <div>ABC</div>
	  <div>ABCD</div>
	  <div>ABCD</div>
	  <div>ABCD</div>
	  <div>ABCD</div>
	  <div>ABCD</div>
	</div>
		
    <div class="right">
	  <div>A</div>
	  <div>AB</div>
	</div>
  </div>
</div>

<div class="outer test4">  
  <div class="background">
	<div>4 text!!!</div>
  </div>

  <div class="foreground">
	<div class="left">
	  <div>A</div>
	  <div>AB</div>
	  <div>ABC</div>
	  <div>ABCD</div>
	  <div>ABCD</div>
	  <div>ABCD</div>
	  <div>ABCD</div>
	  <div>ABCD</div>
	</div>
		
	<div class="right">
	  <div>A</div>
	  <div>AB</div>
	</div>
  </div>
</div>

<div class="outer test5">  
  <div class="background">
	<div>5 some very long text!!!</div>
  </div>

  <div class="foreground">
	<div class="left">
	  <div>A</div>
	  <div>AB</div>
	  <div>ABC</div>
	  <div>ABCD</div>
	  <div>ABCD</div>
	  <div>ABCD</div>
	  <div>ABCD</div>
	  <div>ABCDxxxxx</div>
	</div>
		
    <div class="right">
	  <div>A</div>
	  <div>AB</div>
	</div>
  </div>
</div>

<div class="outer test6">  
  <div class="background">
	<div>6 text!!!</div>
  </div>

  <div class="foreground">
	<div class="left">
	  <div>A</div>
	  <div>AB</div>
	  <div>ABC</div>
	  <div>ABCD</div>
	  <div>ABCD</div>
	  <div>ABCD</div>
	  <div>ABCD</div>
	  <div>ABCDxxxxx</div>
	</div>
		
	<div class="right">
	  <div>A</div>
	  <div>AB</div>
	</div>
  </div>
</div>