如何在CSS中使高度响应?

时间:2016-07-05 04:42:16

标签: html css

我有一个类似

的HTML
<div class="container">
   <div class="content-1"></div>
   <div class="content-2"></div>
</div>

我希望它能够像content-1的高度480px一样工作,content-2将跟随content-1的高度。我的宽度工作得很好,但我的身高并没有跟着。

如何在css中执行此操作?

我的css代码是

.container{
 clear: both;
}
.content-1{
 width:66.66%;
 position: static;
 background-image: url("../images/path1");
 background-repeat: no-repeat;
 background-size: cover;
}

.content-2{
 width:33.33%;
 position: static;
 background-image: url("../images/path2");
 background-repeat: no-repeat;
 background-size: cover;
}

6 个答案:

答案 0 :(得分:0)

你可以尝试这段代码:

.container {
    display: table;
}
.content-1, .content-2 {
    display: table-cell;
}

答案 1 :(得分:0)

在这种情况下,您可以使用CSS3 Flexbox。 <{1}}课程将container,您的所有子课程将设置为相同的高度。

&#13;
&#13;
display:flex
&#13;
.container {
  display:flex; 
 
}

.content-1 {
  border:1px solid #000000;
  width:50%;
   height:480px;
  }

.content-2 {
  border:1px solid #ff0000;
  width:50%;
  }
&#13;
&#13;
&#13;

答案 2 :(得分:0)

关闭internal div tags

<div class="container">
   <div class="content-1"></div>
   <div class="content-2"></div>
</div>

您可以为parent div设置身高,例如.container:480pxchild element自动设为100%

.container{
width:100%;
height:480px;
}

.container > .content-1{
width:100%;
height:100%;
}

.container > .content-2{
width:100%;
height:100%;
}

(或)使用jQuery获取.content-1的高度并将其设置为.content-2

$(document).ready(function(){
var height = $(".container > .content-1").css("height");
$(".container > .content-2").css({
height : height,
background : "#f22"
 });
});

答案 3 :(得分:0)

尝试使用显示表:

.container { 
    display: table;
    height: 480px;
 }
.content-1 {
    display:table-cell;
    float: none;
    width: 50%;
    height: 100%;
}
.content-2 {
    display: table-cell;
    float: none;
    width: 50%;
    height: 100%;
}

答案 4 :(得分:-1)

&#13;
&#13;
	
.flex-container {
  padding: 0;
  margin: 0;
  list-style: none;
  
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  
  -webkit-flex-flow: row wrap;
  justify-content: space-around;
}

.flex-item {
  background: tomato;
  padding: 5px;
  width: 200px;
 
  margin-top: 10px;
 
  color: white;
  font-weight: bold;
  font-size: 3em;
  text-align: center;
 word-break: break-all;
}
&#13;
<div class="container flex-container">
   <div class="content-1 flex-item">orem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</div>
   <div class="content-2 flex-item">1</div>
</div>
&#13;
&#13;
&#13;

答案 5 :(得分:-3)

你需要添加更多1个类:

.height{
  height: 100px;  
}
<div class="container">
   <div class="content-1 height">
   <div class="content-2 height">
</div>