I have some images in bootstrap "row" & "col" div. For Mobile devices I want to turn this into flex-row with horizontal scroll bar.
The caveat is it need to preserve "col" & "row" for desktop devices
Here is sample code for desktop devices
Desktop cols
<div class="row">
<div class="col-2 setwidth bg-success">Card</div>
<div class="col-2 setwidth bg-danger">Card</div>
<div class="col-2 setwidth bg-info">Card</div>
<div class="col-2 setwidth bg-warning">Card</div>
<div class="col-2 setwidth ">Card</div>
</div>
For mobile devices (with horizontal scrollbar)
Mobile cols with horizontal scrollbar
<div class="d-flex flex-row flex-nowrap">
<div class="col-2 setwidth bg-success">Card</div>
<div class="col-2 setwidth bg-danger">Card</div>
<div class="col-2 setwidth bg-info">Card</div>
<div class="col-2 setwidth bg-warning">Card</div>
<div class="col-2 setwidth ">Card</div>
</div>
Is there any css solution available.
First option is two separate "div" for mobile & desktop, It contains images and I don’t know if both the "div" download images twice ?
Second option is to use javascript to detect device type and swap "row" class to "d-flex flex-row flex-nowrap" on load, if there is no css solution.
Here is snippet
.setwidth {
min-height: 100px;
min-width: 100px;
margin : 15px;
}
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container pb-5">
<h2 class="font-weight-light">For PC/wider screen</h2>
<div class="row">
<div class="col-2 setwidth bg-success">Card</div>
<div class="col-2 setwidth bg-danger">Card</div>
<div class="col-2 setwidth bg-info">Card</div>
<div class="col-2 setwidth bg-warning">Card</div>
<div class="col-2 setwidth ">Card</div>
<div class="col-2 setwidth bg-success">Card</div>
<div class="col-2 setwidth bg-danger">Card</div>
<div class="col-2 setwidth bg-info">Card</div>
<div class="col-2 setwidth bg-warning">Card</div>
<div class="col-2 setwidth ">Card</div>
</div>
</div>
<div class="container pb-5">
<div class="position-relative overflow-auto py-2">
<h2 class="font-weight-light">Turn above code for Mobile/small devices</h2>
<div class="d-flex flex-row flex-nowrap">
<div class="col-2 setwidth bg-success">Card</div>
<div class="col-2 setwidth bg-danger">Card</div>
<div class="col-2 setwidth bg-info">Card</div>
<div class="col-2 setwidth bg-warning">Card</div>
<div class="col-2 setwidth ">Card</div>
<div class="col-2 setwidth bg-success">Card</div>
<div class="col-2 setwidth bg-danger">Card</div>
<div class="col-2 setwidth bg-info">Card</div>
<div class="col-2 setwidth bg-warning">Card</div>
<div class="col-2 setwidth ">Card</div>
</div>
</div>
</div>
3
Answers
I got "css only" solution after some findings. Adding media query for small device solved my problem.
Here is the snippet
Thanks all
The above one is css and the bottom is html in small screen
The bottom is in large screen
To achieve the desired layout for both desktop and mobile devices using a single set of HTML elements, you can utilize CSS media queries to change the layout based on the screen size. This approach avoids duplicating HTML content and ensures that images are only loaded once.