使用CSS查询排列元素

时间:2016-10-21 08:10:39

标签: html css

我有一个非常简单的网站,我正在尝试为元素编写CSS查询,以便在调整屏幕大小时正确排列。

您可以查看我拥有的布局以及我想要实现的目标。

enter image description here

这是我想要得到的结果(问题是移动#6):

enter image description here

这是INDEX.html:

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="css/normalize.css">
    <link rel="stylesheet" type="text/css" href="css/grid.css">
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="css/style.css"> 
    <link rel="stylesheet" type="text/css" href="css/queries.css"> 
    <title>Document</title>
</head>
<body>
    <header>
       <div class="row">
            <img src="img/header.png" alt="Voltaren" class="header_image">
       </div>
    </header>
    <section class="middle">
       <div class="row_2">
            <div class="col span-1-of-3 middle_1">
                <img src="img/Middle_1.png" alt="">
            </div>
            <div class="col span-1-of-3 middle_2">
                <img src="img/Middle_2.png" alt="">
            </div>
            <div class="col span-1-of-3">
                <img src="img/Middle_3.png" alt="">
            </div>
        </div>
    </section>
    <section class="bottom">
       <div class="row_2">
            <div class="col span-2-of-3 bot_img">
                <img src="img/Bottom_1.png" alt="">
            </div>

            <div class="col span-1-of-3 ">
                <img src="img/Bottom_2.png" alt="">
            </div>
        </div>
    </section>
    <footer>
        <div class="row_2">
            <img src="img/Footer.png" alt="">
        </div>
    </footer>
</body>
</html>

和queries.css

    /*-----------Small tablet to big tablet from 768 px - 1023px---------------*/


/*-----------Average phones from 481 px - 767px---------------*/
@media only screen and (max-width: 850px){
    .middle{
        padding: 0 2%;
    }

}

/*-----------Average phones from 481 px - 767px---------------*/
@media only screen and (max-width: 767px){
    .middle_1{
        width: 49%;
    }
    .middle_1 img{
        width: 95%;
    }

    .middle_2{
        width: 49%;
        margin-left: 2%;
    }  
    .middle_2 img{
        width: 95%;
    }

}

2 个答案:

答案 0 :(得分:2)

如果你将flexbox用于你的布局,你可以使用order属性来重新排列块,如果你没有使用flexbox,你真的应该。 Demo fiddle

<div class="container">
  <div class="block block-1 width-100">1</div>
  <div class="block block-2 width-33">2</div>
  <div class="block block-3 width-33">3</div>
  <div class="block block-4 width-33">4</div>
  <div class="block block-5 width-66">5</div>
  <div class="block block-6 width-33">6</div>
</div>


.container {
  display: flex;
  flex-wrap: wrap;
  width: 100%;
}

.block {
  height: 200px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 60px;
}

.block-1 {
  background: green;
}
.block-2 {
  background: red;
}
.block-3 {
  background: yellow;
}
.block-4 {
  background: blue;
}
.block-5 {
  background: purple;
}

.block-6 {
  background: brown;
}

.width-100 {
  width: 100%;
}
.width-66 {
  width: 66%;
}
.width-33 {
  width: 33%;
}

@media all and (max-width: 560px) {
  .width-33 {
    width: 50%;
  }
  .block-1 {
    order: 1;
  }
  .block-2 {
    order: 2;
  }
  .block-3 {
    order: 3;
  }
  .block-4 {
    order: 4;
  }
  .block-5 {
    order: 6;
    width: 100%;
  }
  .block-6 {
    order: 5;
    }
}

答案 1 :(得分:0)

在代码中Div 6Div 4之间移动Div 5。给它样式float: right。在小视图中,移除float: right并将Div 6旁边的Div 4放置为inline-block