首页 > 解决方案 > 在移动视口上居中 flexbox 包装

问题描述

如何在移动视口上居中堆叠项目?

这是我的片段:

.flex-container {
  padding: 0;
  margin: 0;
  list-style: none;
  display: flex;
  flex-wrap: wrap;
}

.space-between {
  justify-content: space-between;
}

.space-between div {
  background: pink;
}

.flex-item {
  line-height: 50px;
  color: white;
  font-weight: bold;
  font-size: 2em;
  text-align: center;
}
<div class="flex-container space-between ">
  <div class="flex-item">very long text 1</div>
  <div class="flex-item">very long text 2</div>
  <div class="flex-item">very long text 3</div>
</div>

标签: htmlcssresponsive-designflexbox

解决方案


您应该@media-query用于移动视图。重构您的 HTML 和 CSS 后,这是您更新的代码

.flex-container {
  padding: 0;
  margin: 0;
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between; 

}

@media only screen and (max-width: 768px) {
  .flex-container {
    justify-content: center; 
 }
}

.flex-container div {
  background: pink;
}

.flex-item {
  line-height: 50px;
  color: white;
  font-weight: bold;
  font-size: 2em;
  text-align: center;
}

HTML

<div class="flex-container space-between ">
  <div class="flex-item">very long text 1</div>
  <div class="flex-item">very long text 2</div>
  <div class="flex-item">very long text 3</div>
</div>

推荐阅读