I have two div's on the desktop, it is fine to show first div(#div1) and then second div(#div2), but on mobile, I want to interchange the position of div's, means div2 should be placed before div1.
Here is my HTML code
<div class="col-lg-8 col-md-8 col-sm-8 col-xs-12 " id="div1" style="margin-top:15px"> @{ Html.RenderPartial("_RelatedInformationLeftsection", Model);} </div> <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12 " id="div2" style="border-left:1px solid #eeeeee;margin-top:15px"> @{ Html.RenderPartial("_RelatedInformationRightsection", Model);} </div>
how can i interchange it's position without adding any extra html code? i want to do it with CSS if possible.
Yes, you can do it.
You need to create a class
with the following code:
/*interchangeDiv locationCSS below 768px*/
@media only screen and (max-width: 767px) {
.col-interchange-12 {
display: flex;
flex-direction: column-reverse;
}
}
and your HTML must be like this
<div class="col-interchange-12">
<div class="col-lg-8 col-md-8 col-sm-8 col-xs-12" id="div1" style="margin-top:15px">
@{ Html.RenderPartial("_RelatedInformationLeftsection", Model);}
</div>
<div class="col-lg-4 col-md-4 col-sm-4 col-xs-12" id="div2" style="border-left:1px solid #eeeeee;margin-top:15px">
@{ Html.RenderPartial("_RelatedInformationRightsection", Model);}
</div>
</div>
that's it, you can check it, below 768px, div2 would be above div1
Swap div positions on mobile using CSS media query, use below CSS
@media (max-width: 600px) {
.container {
display:flex;
flex-direction: column-reverse;
}
.section1,
.section2 {
height: auto;
}
}
Here is fiddle example: https://jsfiddle.net/y4a1fo92/
Thanks
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly