I have some divs (1, 2 and 3 are wrapped in container):
And I need to do change these divs according responsive design like this:
How can I achieve that? Maybe using grids?
I have some divs (1, 2 and 3 are wrapped in container):
And I need to do change these divs according responsive design like this:
How can I achieve that? Maybe using grids?
CSS-Grid helps us to achieve this without using any other extra wrappers and without hiding un-hiding stuff using media queries. I am using grid-areas
concept of CSS-Grid
in this case. Though the same can be achieved using grid-lines
concept of CSS-Grid
as well.
.main {
display: grid;
grid-template-columns: repeat(7, 1fr);
grid-auto-rows: minmax(100px, auto);
max-width: 960px;
margin: 0 auto;
gap: 1rem;
/* Placing grid-items inside our grid */
grid-template-areas:
"one one one one four four four"
"one one one one four four four"
"two two two two four four four"
"two two two two four four four"
"three three three three four four four";
}
.one,
.two,
.three,
.four {
border: 1px solid #777;
text-align: center;
line-height: 5.5;
font-size: 2rem;
font-weight: 700;
}
.one {
grid-area: one;
}
.two {
grid-area: two;
}
.three {
grid-area: three;
}
.four {
grid-area: four;
}
@media (max-width: 768px) {
.main {
width: 70%;
grid-template-columns: repeat(3, 1fr);
grid-template-areas:
"one one one"
"one one one"
"two two two"
"two two two"
"four four four"
"four four four"
"four four four"
"four four four"
"three three three";
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Todo | Asynchronous Dynamaic Data</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main class="main">
<div class="one"> 1</div>
<div class="two"> 2</div>
<div class="three">3</div>
<div class="four">4</div>
<main>
<script src="./script.js"></script>
</body>
</html>
NOTE: The same can be achieved using flexbox as well however, we will need additional code. Some content simply needs to be hidden in small screens and other shown along with the order
property of flexbox.