【问题标题】:flex-wrap: wrap not working on mobile iOSflex-wrap: wrap 在移动 iOS 上不起作用
【发布时间】:2019-06-10 02:36:26
【问题描述】:

我有这个在我的笔记本电脑上运行良好,但在移动 iOS 上却不行。

* {
  /* normalize */
  padding: 0;
  margin: 0;
  border: 0;
  color: inherit;
  font-size: inherit;
  font-weight: inherit;
  text-transform: inherit;
  font-family: inherit;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: none;
  background: 0 0;
  min-height: 0;
  user-select: none;
  box-sizing: border-box;
  position: relative;
}

#a {
  display: flex;
  max-width: 100vw;
  min-height: 100vh;
  min-height: calc(var(--vh, 1vh)*100);
}

#b {
  display: flex;
  height: 100%;
  flex: 1;
  flex-direction: column;
  padding: 50px;
}

#c {
  flex-wrap: wrap;
  flex-direction: row;
  max-width: 100%;
  display: flex;
  list-style: none;
}

li {
  flex: 1;
  width: 100%;
  display: flex;
  padding: 10px;
  border: 3px solid red;
  flex-direction: column;
}
<div id="a">
  <div id="b">
    <ul id="c">
      <li>foo</li>
      <li>hello</li>
      <li>world</li>
      <li>bar</li>
      <li>food</li>
      <li>hi</li>
      <li>morning</li>
      <li>something</li>
      <li>beverage</li>
      <li>snack</li>
      <li>drink</li>
      <li>other</li>
      <li>world</li>
      <li>bar</li>
      <li>food</li>
      <li>hi</li>
      <li>morning</li>
      <li>something</li>
      <li>beverage</li>
      <li>snack</li>
      <li>drink</li>
      <li>other</li>
    </ul>
  </div>
</div>

但是,在移动设备上它是 几行 行,但大多数情况下它会向右溢出。就像它将有 2 或 3 行但然后每行 15 或 20 个项目(当只有 2 或 3 个适合屏幕时)。因为我没有一个好的系统可以在移动设备上调试,所以我无法让它工作。

【问题讨论】:

  • 你的 iOS 版本是多少?根据compatibility sheet 的说法,iOS 3.2-6.1 Safari 仅支持完全缺乏包装支持的旧 flexbox 规范。
  • 尝试使用max-widthflex-basis 而不是width。我注意到 Safari 在 flexbox + width 方面存在问题。
  • 我至少有 iOS 10
  • 它正在包裹,但在包裹之前它仍然会越过边缘。
  • @LancePollard 请检查我的回答,如果有帮助请告诉我

标签: javascript html css mobile safari


【解决方案1】:

更新 li 的样式。将flex: 1; 更改为flex: 1 0 auto; 并删除width: 100%

li {
  flex: 1 0 auto;
  /* width: 100%; */
  display: flex;
  padding: 10px;
  border: 3px solid red;
  flex-direction: column;
}

* {
  /* normalize */
  padding: 0;
  margin: 0;
  border: 0;
  color: inherit;
  font-size: inherit;
  font-weight: inherit;
  text-transform: inherit;
  font-family: inherit;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: none;
  background: 0 0;
  min-height: 0;
  user-select: none;
  box-sizing: border-box;
  position: relative;
}

#a {
  display: flex;
  max-width: 100vw;
  min-height: 100vh;
  min-height: calc(var(--vh, 1vh)*100);
}

#b {
  display: flex;
  height: 100%;
  flex: 1;
  flex-direction: column;
  padding: 50px;
}

#c {
  flex-wrap: wrap;
  flex-direction: row;
  max-width: 100%;
  display: flex;
  list-style: none;
}

li {
  flex: 1 0 auto;
  /* width: 100%; */
  display: flex;
  padding: 10px;
  border: 3px solid red;
  flex-direction: column;
}
<div id="a">
  <div id="b">
    <ul id="c">
      <li>foo</li>
      <li>hello</li>
      <li>world</li>
      <li>bar</li>
      <li>food</li>
      <li>hi</li>
      <li>morning</li>
      <li>something</li>
      <li>beverage</li>
      <li>snack</li>
      <li>drink</li>
      <li>other</li>
      <li>world</li>
      <li>bar</li>
      <li>food</li>
      <li>hi</li>
      <li>morning</li>
      <li>something</li>
      <li>beverage</li>
      <li>snack</li>
      <li>drink</li>
      <li>other</li>
    </ul>
  </div>
</div>

查看更新的fiddle

我在 iphone 6、IOS 10.3 中测试过

【讨论】:

  • 基本上你所做的就是复制我的答案并将 flex-shrink 更改为 0。你认为这值得一个新的答案而不是评论或编辑吗?
  • 很抱歉,但您的答案在小提琴本身中不起作用,所以我没有费心把它完全读出来!在我发布我的答案后,我实际上阅读了你写的内容并发现它是相似的(但不完全相同)但是因为你的工作不正常而我的工作正常,所以我决定不删除我的答案。
  • 我向您保证it is working,因为我已经在包括 iOS 10 在内的多个 iOS 设备和版本上对其进行了测试。您的答案与我的相同,只是 flex-shrink 为 0,在这种情况下将 不更可取,因为应该相应地调整长文本的大小。
  • 它在 ios 12 中甚至不适合我。我刚刚在我的 Safari 中检查了你的小提琴。让我截图发给你
  • Try it on codepen instead 如果你的 iPhone 屏幕很小,因为 jsfiddle 在移动屏幕上反应严重。
【解决方案2】:

如果您将 flex 设置为 1 1 auto(允许自动增长、收缩和设置基本宽度的简写),这也适用于早期 iOS 版本(10、11)上的 Safari,并删除 @987654323来自li 的@ 属性。在运行 iOS 10 到 12 的多台设备上进行了测试。JSFiddle sn-p 可用here

更新的 CSS:

* {
  /* normalize */
  padding: 0;
  margin: 0;
  border: 0;
  color: inherit;
  font-size: inherit;
  font-weight: inherit;
  text-transform: inherit;
  font-family: inherit;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: none;
  background: 0 0;
  min-height: 0;
  user-select: none;
  box-sizing: border-box;
  position: relative;
}

#a {
  display: flex;
  min-height: 100vh;
  min-height: calc(var(--vh, 1vh)*100);
  background: cyan;
}

#b {
  display: flex;
  height: 100%;
  flex: 1;
  flex-direction: column;
  padding: 50px;
  background: yellow;
  width: 100%;
}

#c {
  flex-wrap: wrap;
  flex-direction: row;
  max-width: 100%;
  display: flex;
  list-style: none;
  background: green;
}

li {
  flex: 1 1 auto;
  display: flex;
  padding: 10px;
  border: 3px solid red;
  flex-direction: column;
}

* {
  /* normalize */
  padding: 0;
  margin: 0;
  border: 0;
  color: inherit;
  font-size: inherit;
  font-weight: inherit;
  text-transform: inherit;
  font-family: inherit;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: none;
  background: 0 0;
  min-height: 0;
  user-select: none;
  box-sizing: border-box;
  position: relative;
}

#a {
  display: flex;
  /* Use max-width for testing purposes */
  /*max-width: 25%;.*/
  min-height: 100vh;
  min-height: calc(var(--vh, 1vh)*100);
  background: cyan;
}

#b {
  display: flex;
  height: 100%;
  flex: 1;
  flex-direction: column;
  padding: 50px;
  background: yellow;
  width: 100%;
}

#c {
  flex-wrap: wrap;
  flex-direction: row;
  max-width: 100%;
  display: flex;
  list-style: none;
  background: green;
}

li {
  flex: 1 1 auto;
  display: flex;
  padding: 10px;
  border: 3px solid red;
  flex-direction: column;
}
<div id="a">
  <div id="b">
    <ul id="c">
      <li>foo</li>
      <li>hello</li>
      <li>world</li>
      <li>bar</li>
      <li>food</li>
      <li>hi</li>
      <li>morning</li>
      <li>something</li>
      <li>beverage</li>
      <li>snack</li>
      <li>drink</li>
      <li>other</li>
      <li>world</li>
      <li>bar</li>
      <li>food</li>
      <li>hi</li>
      <li>morning</li>
      <li>something</li>
      <li>beverage</li>
      <li>snack</li>
      <li>drink</li>
      <li>other</li>
    </ul>
  </div>
</div>

【讨论】:

  • OP 你试过这种方法吗?我对其进行了测试并确认可以在多个 iOS / Safari 移动版本上运行,因此应该没有问题。
猜你喜欢
  • 2019-04-05
  • 2018-12-20
  • 2017-05-10
  • 2018-08-05
  • 2017-07-16
  • 2013-12-22
  • 2021-07-10
  • 2017-08-30
  • 1970-01-01
相关资源
最近更新 更多