I have a bunch of images inside bootstrap's "row" and "col" div. On mobile devices, I'd like to switch this layout to a flex-row with a horizontal scroll bar.
The challenge is maintaining the "col" and "row" structure for desktop devices.
Here's an example code snippet for desktop devices Desktop columns
<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 columns 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 a CSS solution available?
The first option is to have two separate "divs" for mobile and desktop, but I'm unsure if both will download the images twice.
The second option is to use JavaScript to detect the device type and swap the "row" class to "d-flex flex-row flex-nowrap" on load if a CSS solution isn't feasible.
Here's a snippet:
.setwidth {
min-height: 100px;
min-width: 100px;
margin : 15px;
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6c0e0303181f181e0d1c2c59425f425f">[email protected]</a>/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">Adjusting 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>