html - 在较小的设备中按所需顺序放置 float div?

标签 html css responsive-design

考虑我们有 3 个 float 的 div 并且它们彼此相邻

我们能否在较小的设备中将左右 div 并排放置,中间的 div 在它们上方,如下图所示?

enter image description here

我在下面尝试过但没有成功。 任何想法?谢谢。

<html>
<head>
<style>
#content {
    width: 20%;
    float: left;
    background:purple;      
}    
#middle {
    width: 64.3%; /* Account for margins + border values */
    float: left;
    margin-right:2px;
    margin-left:2px;
    background:yellow;      
}    
#sidebar {
    width: 15%;     
    float: left;
    background:red;
}    
/* for 980px or less */
@media screen and (max-width: 980px) {
 #content {
    width: 20%;
    float: left;
    background:purple;      
 }    
 #middle {
    float:none;
    clear:both;     
 }    
 #sidebar {
    width: 15%;     
    float: left;
    background:red;
 }
}
</style>
</head>
</head>
 <body>
   <div id="content">1</div>
   <div id="middle">2</div>
   <div id="sidebar">3</div>
 </body>
</html>

最佳答案

做到这一点的最佳方式是首先考虑移动设备。在小型设备上按正确的顺序放置 div,在大型设备上使用 css position, left 和 right 来获得正确的顺序。

http://codepen.io/mouhammed/pen/Ieyom

HTML:

<html>
<head>
</head>
</head>
 <body>
   <div id="middle">2</div>
   <div id="content">1</div>
   <div id="sidebar">3</div>
 </body>
</html>

CSS:

#content {
    width: 20%;
    float: left;
    background:purple;  
    position:relative; /*pull this div to the right */
    right:64.3%;
    left:auto;
}    
#middle {
    width: 64.3%; /* Account for margins + border values */
    float: left;
    margin-right:2px;
    margin-left:2px;
    background:yellow;  
    position:relative;  /*push this div to the left */
    left:20%;
    right:auto;
}    
#sidebar {
    width: 15%;     
    float: left;
    background:red;
}    
/* for 980px or less */
@media screen and (max-width: 980px) {
 #content {
    width: 70%;
    background:purple;
    right:auto;
 }    
 #middle {
   width:100%; 
   float:left;  
   left:auto;
 }    
 #sidebar {
    width: 30%;     
    float: left;
    background:red;
 }
}

关于html - 在较小的设备中按所需顺序放置 float div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25093556/

相关文章:

css - Bootstrap 轮播中的导航斗争

CSS Spritesheet 动画一直向左移动

css - 对齐背景图像(:relative) to right

php - 将 while 循环中的数据显示到 html 代码中

javascript - NG-keypress 防止特定 charCode 的默认值

javascript - 折叠侧边栏时增加 div 宽度大小

jquery - 如何向现有灯箱添加功能性滚动箭头?

javascript - 如何创建一个滑动菜单,它将填充窗口宽度,除了拉杆按钮宽度?

html - 使元素粘在它们的父容器上

css - 站点不是 100% 虽然已定义?