I have a grid with two columns, and in the columns there are divs with different height and width. It is necessary to know that when we click on a div, its content toggles down (show/hide with jquery), so divs can change their heights also. For showing my contents in importance from screen view top to down, the order is in line (row). My problem is the mobile version because when I resize the page, the order will be by columns, and not as in line.
.grid {position:relative;display:flex;flex-flow:row wrap;justify-content:space-between;width:100%;max-width:1200px;margin:0 auto;padding:20px;background:grey}
.column {position:relative;display:flex;flex-flow:row wrap;align-content:start;width:49.5%;background:white}
.column1 {justify-content:right;}
.column2 {justify-content:left;}
.item {width:97.5%;max-width:720px;max-height:720px;padding:10px;color:#fff;font-size:30px}
.item1 {width:80%;height:100px;background:red}
.item2 {width:50%;height:190px;background:blue}
.item3 {width:40%;height:80px;background:green}
.item4 {width:100%;height:50px;background:yellow}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<main class="grid">
<div class="column column1">
<div class="item item1">1</div>
<div class="item item3">3</div>
</div>
<div class="column column2">
<div class="item item2">2</div>
<div class="item item4">4</div>
</div>
</main>
I found a lot of questions/answers about grid and divs order but all of them are in the same columns or rows. I am still looking for a CSS or JS solution.
3
Answers
I accepted Kameron's answer but in the meantime I found a more simple way... First the items of the column 2. must be appended to the column 1., then I put the ex column 2. items between the column 1. items. At the end I do not display the empty column 2.
You can use
@media only screen
to set divs in one column from or to a certain width and usegrid
but then you will have rows same height and If you don’t want this you can use js to replace childs. like this.script.js
style.css
index.html
This is a bit of an odd request that cannot be handled with modern CSS.
I created a function that swaps the elements by inserting a marker at the desired location of object 1. There are more details on this function located in the comments of the JS.
I then created a
MediaQueryList
by calling thematchMedia
method on thewindow
object.So with this said, I used an event-driven listener that is invoked by a conditional statement whenever the screen is below or equal to
1000px
.