我连续有6个方块:
在移动和台式机版本中,所有内容均可按我的意愿运行,但是当我将flex-direction
从row
更改为column
时:
在台式机版本中,一切正常,但在移动版本中,块并不在每行中排列2,而是在一列中。我该怎么解决?
CSS:
* {
box-sizing: border-box;
}
img {
width: auto;
max-width: 100%;
height: auto;
max-height: 100%;
}
.picture-box {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
padding: 3rem 1.5rem;
}
.ring {
flex: 0 0 calc(50% - 10px);
padding: 20px;
margin-bottom: 20px;
border: 1px solid green;
text-align: center;
}
.thumb {
display: inline-block;
max-width: 200px;
padding: 10px;
border: 1px solid blue;
}
@media all and (min-width: 1024px) {
.ring {
flex: 0 0 calc((100% / 3) - (40px / 3))
}
}
@media all and (min-width: 1240px) {
.ring {
flex: 0 0 calc((100% / 6) - (100px / 6))
}
}
HTML:
<div class="picture-box">
<div class="ring">
<div class="thumb">
<img src="https://via.placeholder.com/200">
</div>
</div>
<div class="ring">
<div class="thumb">
<img src="https://via.placeholder.com/200">
</div>
</div>
<div class="ring">
<div class="thumb">
<img src="https://via.placeholder.com/200">
</div>
</div>
<div class="ring">
<div class="thumb">
<img src="https://via.placeholder.com/200">
</div>
</div>
<div class="ring">
<div class="thumb">
<img src="https://via.placeholder.com/200">
</div>
</div>
<div class="ring">
<div class="thumb">
<img src="https://via.placeholder.com/200">
</div>
</div>
</div>
答案 0 :(得分:1)
使用桌面媒体查询保留column
布局并在移动设备和平板电脑上使用flex-direction : row
怎么样?
@media all and (min-width: 1024px) {
.ring {
flex: 0 0 calc((100% / 3) - (40px / 3))
}
.picture-box {
flex-direction: column;
}
}
答案 1 :(得分:0)
请使用https://getbootstrap.com/框架。 并使用这样的html结构,
<div class="picture-box">
<div class="row">
<div class="col-sm-2">
<div class="ring">
<div class="thumb">
<img src="https://via.placeholder.com/200">
</div>
</div>
</div>
<div class="col-sm-2">
<div class="ring">
<div class="thumb">
<img src="https://via.placeholder.com/200">
</div>
</div>
</div>
<div class="col-sm-2">
<div class="ring">
<div class="thumb">
<img src="https://via.placeholder.com/200">
</div>
</div>
</div>
<div class="col-sm-2">
<div class="ring">
<div class="thumb">
<img src="https://via.placeholder.com/200">
</div>
</div>
</div>
<div class="col-sm-2">
<div class="ring">
<div class="thumb">
<img src="https://via.placeholder.com/200">
</div>
</div>
</div>
<div class="col-sm-2">
<div class="ring">
<div class="thumb">
<img src="https://via.placeholder.com/200">
</div>
</div>
</div>
<div class="col-sm-2">
<div class="ring">
<div class="thumb">
<img src="https://via.placeholder.com/200">
</div>
</div>
</div>
</div>
</div>
答案 2 :(得分:0)
请尝试一下。...
<style>
body{padding: 0px;margin: 0px;}
*{box-sizing: border-box;}
img{width: auto;max-width: 100%;}
.picture-box{display: flex;flex-direction: row;justify-content:space-around; padding: 3rem 1.5rem;}
.ring{padding: 20px;margin:0px 10px 20px 10px;border: 1px solid green;text-align: center;}
.thumb{display: block;padding: 10px;border: 1px solid blue;}
@media(max-width: 1024px){.picture-box{flex-wrap: wrap;}.ring{width: calc(33.3% - 20px);}}
@media(max-width: 768px){.ring{width: calc(50% - 20px);}}
@media(max-width: 480px){.ring{width: 100%; margin: 0px 0px 20px 0px;}}
</style>