I have a screenshot below which I have replicated in the fiddle. In the fiddle I have a made a parent div in which I have mentioned all the 2 rows:
<div class="product-all-contents">
<div class="product-contents">
</div>
<div class="product-contents">
</div>
</div>
Problem Statement:
I want to make the individual row scroll in the mobile/tablet view. The CSS codes which I have tried in order to make it scroll are as follows be it doesn't seem to work. Do I need to set min-width as well in order to make the rows scroll in mobile/tablet view?
@media only screen and (max-width: 767px)
{
.product-all-contents
{
overflow-x: auto;
}
}
To enable horizontal scrolling, we can use the CSS property overflow-x. If we assign the value scroll to the overflow-x property of the container element, the browser will hide horizontally overflowing content and make it accessible via horizontal scrolling.
To implement this, we can use overflow:scroll on mobile, it will display scrollbars and make the content inside the box scrollable, preventing the content rendering outside the box. Use flex:none on mobile to “fix” the tags height, and max-width: fit-content on desktop to “fix” the tags width.
For horizontal scrollable bar use the x and y-axis. Set the overflow-y: hidden; and overflow-x: auto; that will automatically hide the vertical scroll bar and present only the horizontal scrollbar. The white-space: nowrap; property is used to wrap text in a single line.
overflow-x
to .product-contents
so that it shows
scroll on smaller screenmin-width
to .product
so that it will not get smaller on small
device:not
selector in .product
, set margin-right
so that space
between each item will remainwhite-space
from the .product-all-contents
in @media only
screen and (max-width: 767px)
as there is no need of it now.product-contents {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem;
overflow-x: auto;
}
.product-contents .product {
width: 10%;
min-width: 90px;
text-align: center;
height: 150px;
padding-top: 1%;
padding-left: 1%;
padding-right: 1%;
border-style: solid;
border-width: 3px;
border-color: rgb(145, 147, 150);
background-color: white;
border-radius: 10px
}
.product-contents .product:not(:last-child) {
margin-right: 15px;
}
.product-contents .product img {
max-width: 100%;
}
@media only screen and (max-width: 767px)
{
.product-all-contents
{
overflow-x: auto;
}
}
<div class="product-all-contents">
<div class="product-contents">
<div class="product"><img src="http://via.placeholder.com/150x150" alt="" /></div>
<div class="product"><img src="http://via.placeholder.com/150x150" alt="" /></div>
<div class="product"><img src="http://via.placeholder.com/150x150" alt="" /></div>
<div class="product"><img src="http://via.placeholder.com/150x150" alt="" /></div>
<div class="product"><img src="http://via.placeholder.com/150x150" alt="" /></div>
<div class="product"><img src="http://via.placeholder.com/150x150" alt="" /></div>
<div class="product"><img src="http://via.placeholder.com/150x150" alt="" /></div>
<div class="product"><img src="http://via.placeholder.com/150x150" alt="" /></div>
</div>
<div class="product-contents">
<div class="product"><img src="http://via.placeholder.com/150x150" alt="" /></div>
<div class="product"><img src="http://via.placeholder.com/150x150" alt="" /></div>
<div class="product"><img src="http://via.placeholder.com/150x150" alt="" /></div>
<div class="product"><img src="http://via.placeholder.com/150x150" alt="" /></div>
<div class="product"><img src="http://via.placeholder.com/150x150" alt="" /></div>
<div class="product"><img src="http://via.placeholder.com/150x150" alt="" /></div>
<div class="product"><img src="http://via.placeholder.com/150x150" alt="" /></div>
<div class="product"><img src="http://via.placeholder.com/150x150" alt="" /></div>
</div>
</div>
Updated fiddle Here
it's a good idea to fix height and width of a box instead of using width in percentage because of this layout of the page will be consistent on all screens and a user can also able to scroll individual product row horizontally
Just change your CSS rules as below I have changed your CSS rules of .product
class and added one extra media query for the horizontal scroll in a product row.
.product-contents .product {
min-width: 160px;
width:160px;
text-align: center;
height: 150px;
border-style: solid;
border-width: 3px;
border-color: rgb(145,147,150);
background-color: white;
border-radius: 10px;
padding: 20px;
margin-left: 10px;
}
@media only screen and (max-width: 767px)
{
.product-contents{
overflow-x: auto;
}
}
Hope this work for you :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With