问题
I have a website which looks perfect on the desktop view. I have created the fiddle as well so that its easy to make changes.
The snippets of CSS codes which I have used in order to align the squares in a row are:
.squares {
display: flex;
justify-content: space-between;
align-items:center;
padding: 1rem;
flex-wrap: wrap;
}
.squares .square {
width: 13%;
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);
border-radius: 10px;
}
Problem Statement:
I am wondering what CSS codes I should add here or change so that I am able to horizontally scroll the square boxes in a mobile view similar to the following screenshot:
At this moment, in the mobile view it is fit to the screen but not scrolling horizontally.
回答1:
Try also changing:
.squares{
flex-wrap: nowrap;
}
.squares .square{
flex: 1 0 auto;
}
This prevents the squares from going to the second line.
回答2:
Just add the following code as an example:
CSS
@media only screen and (max-width: 767px) {
.squares {
overflow-x: scroll;
overflow-y: hidden;
height: 200px;
white-space:nowrap
}
.squares .square {
width: 30%;
text-align: center;
height: 150px;
display:inline-block; /* Added */
margin-bottom: 11%;
/*padding-top: 1%;
padding-left: 1%;
padding-right: 1%;*/
border-style: solid;
border-width: 3px;
border-color: rgb(145,147,150);
border-radius: 10px;
}
}
HTML
<div class="squares">
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
</div>
来源:https://stackoverflow.com/questions/50499051/how-to-horizontal-scroll-square-boxes-in-a-mobile-view-in-html-css