TacoCat TacoCat - 1 month ago 5
HTML Question

Arranging elements using CSS queries

I've got a very simple website, I'm trying to write a CSS query for the elements to arrange correctly when resizing the screen.

You can take a look at the layout that I have and what I want to achieve.

enter image description here

And this is the result that I want to get (The problem is moving #6 up):

enter image description here

Here is the 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>


And the 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%;
}

}

Answer

If You would use flexbox to Your layout, You could use order property, to rearange blocks, If You are not using flexbox, You really should. 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;
    }
}
Comments