响应页面:在一列中使正确的列保持在顶部

cur*_*us1 5 css css3

我有以下html:

<div class="left">
this is left side. it should go to the bottom when in one column
</div>
<div class="right">
this is right side. it should go to the top when in one column
</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

.left {
    width: 70%;
    float: left;
    background-color: red;
}
.right {
    width: 30%;
    float: left;
    background-color: yellow;
}

 @media only screen and (max-width : 768px) {
   .left, .right {
        width: 100%;
     }
}
Run Code Online (Sandbox Code Playgroud)

我希望当显示为一列时,使右列保持在顶部.

这是jsfiddle链接:https://jsfiddle.net/m7zpoc30/1/

请随时建议我应该做什么,包括改变我目前的CSS.

谢谢!

sol*_*sol 6

这是使用flex的解决方案,这可能是有意义的.您可以将左右div放入容器中display:flex,然后在调整大小时使用flex-direction.请参阅下面的代码段.(jsfiddle)

.container {
  display: flex;
  
}

.left {
  width: 70%;
  background-color: red;
}

.right {
  width: 30%;
  background-color: yellow;
}

@media only screen and (max-width: 768px) {
  .container {
    flex-direction: column-reverse;
  }
  .left, .right {
    width: 100%;
  }
  
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
  <div class="left">
    this is left side. it should go to the bottom when in one column
  </div>
  <div class="right">
    this is right side. it should go to the top when in one column
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)


小智 5

更改div的顺序,以使右对齐在html中排在最前面,然后将class="right"div 更新为float:right;

https://jsfiddle.net/m7zpoc30/3/