html/css 如何让正方形在移动视图中连续滚动?
How to make the squares scroll in a row in a mobile view in html/css?
我在 fiddle 中复制了下面的屏幕截图。在 fiddle 中,我创建了一个父项 div,其中我提到了所有 2 行:
<div class="product-all-contents">
<div class="product-contents">
</div>
<div class="product-contents">
</div>
</div>
问题陈述:
我想在mobile/tablet视图中div双行滚动。 CSS 我为了让它滚动而尝试的代码如下,但它似乎不起作用。我是否还需要设置最小宽度以使行在 mobile/tablet 视图中滚动?
@media only screen and (max-width: 767px)
{
.product-all-contents
{
overflow-x: auto;
}
}
- 1) 您需要将
overflow-x
设置为.product-contents
,这样才能显示
在较小的屏幕上滚动
- 2) 将
min-width
设置为.product
这样就不会变小了
设备
- 3) 在
.product
中使用 :not
选择器,设置 margin-right
以便 space
每个项目之间将保留
- 4) 从
@media only
screen and (max-width: 767px)
中的 .product-all-contents
中删除 white-space
,因为现在不需要它了
.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>
已更新 fiddle Here
固定框的高度和宽度而不是使用百分比宽度是个好主意,因为这种页面布局在所有屏幕上都是一致的,并且用户还可以水平滚动单个产品行
只需如下更改您的 CSS 规则 我已经更改了 .product
class 的 CSS 规则,并为产品中的水平滚动添加了一个额外的媒体查询行。
.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;
}
}
希望这对你有用:)
我在 fiddle 中复制了下面的屏幕截图。在 fiddle 中,我创建了一个父项 div,其中我提到了所有 2 行:
<div class="product-all-contents">
<div class="product-contents">
</div>
<div class="product-contents">
</div>
</div>
问题陈述:
我想在mobile/tablet视图中div双行滚动。 CSS 我为了让它滚动而尝试的代码如下,但它似乎不起作用。我是否还需要设置最小宽度以使行在 mobile/tablet 视图中滚动?
@media only screen and (max-width: 767px)
{
.product-all-contents
{
overflow-x: auto;
}
}
- 1) 您需要将
overflow-x
设置为.product-contents
,这样才能显示 在较小的屏幕上滚动 - 2) 将
min-width
设置为.product
这样就不会变小了 设备 - 3) 在
.product
中使用:not
选择器,设置margin-right
以便 space 每个项目之间将保留 - 4) 从
@media only screen and (max-width: 767px)
中的.product-all-contents
中删除white-space
,因为现在不需要它了
.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>
已更新 fiddle Here
固定框的高度和宽度而不是使用百分比宽度是个好主意,因为这种页面布局在所有屏幕上都是一致的,并且用户还可以水平滚动单个产品行
只需如下更改您的 CSS 规则 我已经更改了 .product
class 的 CSS 规则,并为产品中的水平滚动添加了一个额外的媒体查询行。
.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;
}
}
希望这对你有用:)