Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
516 views
in Technique[技术] by (71.8m points)

html - space-evenly (justify-content) not working on Chrome mobile

I have an issue with the space-evenly value for justify-content on Chrome mobile (it works fine on Desktop and on Firefox mobile).

I managed to produce a minimal example: Example

I have a flex container in a row direction and I want the elements to be spaced evenly, as intended when using space-evenly. It works on desktop, but on Chrome mobile (version 59 on Android), however, the elements are aligned on the left. Here is a comparison of the two: Comparison

center and space-around values work as intended, however, but I really want to use space-evenly or an equivalent (the flex-wrap: wrap behavior is also important to me).

Here is my HTML:

<div class="container">
  <div class="element"></div>
  <div class="element"></div>
  <div class="element"></div>
</div>

And here is my CSS:

.container {
  display: flex;
  flex-direction: row;
  justify-content: space-evenly;
  flex-wrap: wrap;
  background: lightgrey;
}

.element {
  margin: 8px;
  width: 42px;
  height: 42px;
  background: black;
}

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is no solution yet for multi-line/wrapped space-evenly for browsers that doesn't support it.

When using flex-wrap: wrap you will need to i.e. set a fixed padding on the flex container, margin on the items or use a script.

If it is possible to wrap each row you can use the pseudo elements in combination with space-between to achieve the exact same result as space-evenly give.

In below sample the space-between will take the zero width pseudo elements into account when calculating the space between, hence producing the same result.

.container {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  /* flex-wrap: wrap; */
  /* align-items: flex-start;        if "img", this will prevent it from stretching  */
  background: lightgrey;
}

.element {
  margin: 8px;
  width: 42px;
  height: 42px;
  background: black;
}

.container::before,
.container::after {
  content: '';
}
<div class="container">
  <div class="element"></div>
  <div class="element"></div>
  <div class="element"></div>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...