两个Divs彼此相邻,然后堆叠响应变化

时间:2013-01-21 10:37:31

标签: css html

我正在努力实现一些我确信应该比制作它更容易的东西!

我正在使用Skeleton响应式框架,直到现在一直很好。

这是我想要实现的图表。

Diagram showing what I div layout

这将放在一列中。一旦列的大小减小,我希望它按照图中的第二个示例堆叠div。我尝试了几种不同的方法,但一直都是错的。

我是HTML / CSS的新手,所以任何帮助都表示赞赏!非常感谢!

6 个答案:

答案 0 :(得分:64)

您可以使用CSS3 媒体查询。写得像这样:

<强> CSS

.wrapper { 
  border : 2px solid #000; 
  overflow:hidden;
}

.wrapper div {
   min-height: 200px;
   padding: 10px;
}
#one {
  background-color: gray;
  float:left; 
  margin-right:20px;
  width:140px;
  border-right:2px solid #000;
}
#two { 
  background-color: white;
  overflow:hidden;
  margin:10px;
  border:2px dashed #ccc;
  min-height:170px;
}

@media screen and (max-width: 400px) {
   #one { 
    float: none;
    margin-right:0;
    width:auto;
    border:0;
    border-bottom:2px solid #000;    
  }
}

<强> HTML

<div class="wrapper">
    <div id="one">one</div>
    <div id="two">two</div>
</div>

选中此项以获取更多http://jsfiddle.net/cUCvY/1/

答案 1 :(得分:8)

浮动div会帮助您实现目标。

Example

HTML

<div class="container">
<div class="content1 content">
</div>
<div class="content2 content">
</div>
</div>

CSS

.container{
width:100%;
height:200px;
background-color:grey;
}
.content{
float:left;
height:30px;
}
.content1{
background-color:blue;
width:300px;
}
.content2{
width:200px;
background-color:green;
}

放大页面以查看效果。

希望它有所帮助。

答案 2 :(得分:6)

今天可以使用display:flex;

来完成此类事情

https://jsfiddle.net/suunyz3e/1435/

HTML:

  <div class="container flex-direction">
      <div class="div1">
        <span>Div One</span>
      </div>
      <div class="div2">
        <span>Div Two</span>
      </div>
  </div>

的CSS:

.container{
  display:inline-flex;
  flex-wrap:wrap;
  border:1px solid black;
}
.flex-direction{
  flex-direction:row;
}
.div1{
  border-right:1px solid black;
  background-color:#727272;
  width:165px;
  height:132px;
}

.div2{
  background-color:#fff;
  width:314px;
  height:132px;
}

span{
  font-size:16px;
    font-weight:bold;
    display: block;
    line-height: 132px;
    text-align: center;
}

@media screen and (max-width: 500px) {
  .flex-direction{
  flex-direction:column;
  }
.div1{
  width:202px;
  height:131px;
  border-right:none;
  border-bottom:1px solid black;
  }
  .div2{
    width:202px;
    height:107px;
  }
  .div2 span{
    line-height:107px;
  }

}

答案 3 :(得分:2)

使用基于min-width的媒体查询,您可以实现类似http://jsbin.com/aruyiq/1/edit

的内容

CSS

.wrapper { 
  border : 2px dotted #ccc; padding: 2px; 
}

.wrapper div {
   width: 100%; 
   min-height: 200px;
   padding: 10px;
   -webkit-box-sizing: border-box;
   -moz-box-sizing: border-box;
   box-sizing: border-box;
}
#one { background-color: gray; }
#two { background-color: white; }

@media screen and (min-width: 600px) {
   .wrapper {
      height: auto; overflow: hidden; // clearing 
   }
   #one { width: 200px; float: left; }
   #two { margin-left: 200px; }
}

在我的示例中,断点为600px,但您可以根据需要进行调整。

答案 4 :(得分:1)

这样做:

<强> HTML

<div class="parent">
    <div class="child"></div>
    <div class="child"></div>
</div>

<强> CSS

.parent{
    width: 400px;
    background: red;
}
.child{
    float: left;
    width:200px;
    background:green;
    height: 100px;
}

这是有效的jsfiddle。将孩子width更改为200px以上,然后他们会堆叠。

答案 5 :(得分:1)

迟到总比不到好!

https://getbootstrap.com/docs/4.5/layout/grid/

<div class="container">
  <div class="row">
    <div class="col-sm">
      One of three columns
    </div>
    <div class="col-sm">
      One of three columns
    </div>
    <div class="col-sm">
      One of three columns
    </div>
  </div>
</div>