I have three columns; left, middle and right and they are in a div that is 100% width of the screen. How in a media query, when the screen size is reduced, can I get the middle column to stay on the top, the left column to go underneath and then the right column to go underneath that? I've attached a CodePen as well as displaying the HTML and CSS below: https://codepen.io/Macast/pen/jZworE . Any help will be greatly appreciated! I have no idea how to go about this.
HTML:
<body>
<div class="columnContainer">
<div class="leftColumn" style="background-color:#aaa;">
<h2>Left Column</h2>
</div>
<div class="middleColumn" style="background-color:#bbb;">
<h2>Middle Column</h2>
</div>
<div class="rightColumn" style="background-color:#ccc;">
<h2>Right Column</h2>
</div>
</div>
</body>
</html>
CSS:
* {
box-sizing: border-box;
}
body {
margin: 0;
}
.columnContainer {
width: 100%;
}
.leftColumn {
float: left;
width: 33.33%;
padding: 10px;
height: 200px;
text-align: center;
}
.middleColumn {
float: left;
width: 33.33%;
padding: 10px;
height: 200px;
text-align: center;
}
.rightColumn {
float: left;
width: 33.33%;
padding: 10px;
height: 200px;
text-align: center;
}
.columnContainer:after {
content: "";
display: table;
clear: both;
}
/* Media Query */
@media (min-width: 320px) and (max-width: 480px) {
/* Column Stacking Here */
}
You can use flex
+ order
;
* {
box-sizing: border-box;
}
body {
margin: 0;
}
.columnContainer {
width: 100%;
display: flex;
}
.leftColumn {
width: 33.33%;
padding: 10px;
height: 200px;
margin: 0 auto;
text-align: center;
}
.middleColumn {
width: 33.33%;
padding: 10px;
height: 200px;
margin: 0 auto;
text-align: center;
}
.rightColumn {
width: 33.33%;
padding: 10px;
height: 200px;
margin: 0 auto;
text-align: center;
}
/* Media Query */
@media (min-width: 320px) and (max-width: 480px) {
/* Column Stacking Here */
.columnContainer {
flex-direction: column;
}
.leftColumn {
order: 2;
}
.middleColumn {
order: 1;
}
.rightColumn {
order: 3;
}
}
<body>
<div class="columnContainer">
<div class="leftColumn" style="background-color:#aaa;">
<h2>Left Column</h2>
</div>
<div class="middleColumn" style="background-color:#bbb;">
<h2>Middle Column</h2>
</div>
<div class="rightColumn" style="background-color:#ccc;">
<h2>Right Column</h2>
</div>
</div>
</body>
</html>