【问题标题】:Centering flexbox wrap on mobile viewport在移动视口上居中 flexbox 包装
【发布时间】:2019-03-20 05:00:41
【问题描述】:

如何将堆叠的项目置于移动视口的中心?

这是我的 sn-p:

.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>

【问题讨论】:

  • 如果您愿意使用space-around 而不是space-between,项目将自然居中(无需媒体查询)。 jsfiddle.net/upnv69Lt

标签: html css responsive-design flexbox


【解决方案1】:

您应该将@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>

【讨论】:

    猜你喜欢
    • 2021-08-30
    • 1970-01-01
    • 2022-11-24
    • 1970-01-01
    • 2017-11-15
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多