<view class="rega" v-if="isActive==1"> <text style="margin: 10px;">热门城市</text> <block class="cities row-item flexbox" v-for="(item,index) in hotcities" key="index"> <view class="hotcities—item"> <text>{{item.names}}</text> </view> </block> </view>.row-item { display: flex; flex-direction: row; //设置布局方向为水平 } .bottom_title { text-align: center; margin-left: 20px; } .active { font-weight: bold; border-bottom: 2px solid orange; } .flexbox { display: flex; flex-wrap: wrap; justify-content: space-between; } .hotcities—item { width: 23%; margin-bottom: 10px; border: 1px solid #ccc; padding: 5px; }让block标签里面显示的东西横向布局,每行显示四个,多的显示在下一行
时间: 2024-02-19 18:03:57 浏览: 26
可以在`.flexbox`样式中添加`flex-basis: 23%;`来设置每个`.hotcities—item`元素的宽度为23%,并在`.row-item`样式中添加`flex-wrap: wrap;`来设置元素换行,然后使用`justify-content: space-between;`来设置每行元素之间的间距。最终代码如下:
```
.row-item {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.hotcities—item {
flex-basis: 23%;
margin-bottom: 10px;
border: 1px solid #ccc;
padding: 5px;
}
```
相关问题
<view class="rega" v-if="isActive==1"> <text style="margin: 10px;">热门城市</text> <block class="cities row-item" v-for="(item,index) in hotcities" key="index"> <view class="hotcities—item"> <text>{{item.names}}</text> </view> </block> </view>让这个block里面显示的内容横向排列,每行显示4个,显示的每个元素都要有边框
可以将这个block的class属性改为"cities row-item flexbox",并在CSS样式中添加以下代码:
```
.flexbox {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.hotcities—item {
width: 23%;
margin-bottom: 10px;
border: 1px solid #ccc;
padding: 5px;
}
```
这样就可以让block里面的内容横向排列,每行显示4个,并且每个元素都有边框了。
<view class="rega" v-if="isActive==1"> <text style="margin: 10px;">热门城市</text> <view class="cities" v-for="(item,index) in hotcities" :key="index"> <view class="hotcitiesitem" >{{item.names}}</view> </view> </view> .rega { height: 100%; width: 100%; } .hotcitiesitem { width: 23%; margin: 5px; border: 1px solid #ccc; border-radius: 20rpx; text-align: center; }要求在显示的页面上每一行能显示四个元素,以贡献是两行,
可以将`.hotcitiesitem`的宽度设为`calc(25% - 10px)`,同时将`.cities`的宽度设为`100%`,并给`.cities`添加`display:flex`和`flex-wrap:wrap`属性,这样每一行就能显示四个元素了,如下所示:
```
.rega {
height: 100%;
width: 100%;
}
.cities {
width: 100%;
display: flex;
flex-wrap: wrap;
}
.hotcitiesitem {
width: calc(25% - 10px);
margin: 5px;
border: 1px solid #ccc;
border-radius: 20rpx;
text-align: center;
}
```