缩小包裹 flexbox,使其包裹的 flex 项目可以居中。

huangapple go评论71阅读模式
英文:

Shrink-wrap flexbox with wrapping flex-items so it can be centered

问题

I have a flexbox with wrapping contents. How can I shrinkwrap the flexbox to its contents? The root problem I'm trying to solve is to center the flexbox. This image describes my issue:

缩小包裹 flexbox,使其包裹的 flex 项目可以居中。

How can I achieve this? Is it possible in pure CSS? I don't know beforehand if the flexbox will wrap to 1, 2, or 3 columns. The width of the elements is known. The height would preferably be unknown, but if it makes it a lot easier, I can decide on a constant height.

Neither width:min-content, width:max-content, width:fit-content nor inline-flex appear to solve the problem.

JS Fiddle: https://jsfiddle.net/oznts5bv/

.container {
  display:flex;
  flex-wrap:wrap;
  border:1px dashed magenta;
}

.container div {
  width:100px;
  height:60px;
  border:1px solid black;
}
<div class="container">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
</div>
英文:

I have a flexbox with wrapping contents. How can I shrinkwrap the flexbox to its contents? The root problem I'm trying to solve is to center the flexbox. This image describes my issue:

缩小包裹 flexbox,使其包裹的 flex 项目可以居中。

How can I achieve this? Is it possible in pure CSS? I don't know beforehand if the flexbox will wrap to 1, 2 or 3 columns. The width of the elements is known. The height would preferably be unknown, but if it makes it a lot easier, I can decide on a constant height.

Neither width:min-content, width:max-content, width:fit-content nor inline-flex appear to solve the problem.

JS Fiddle: https://jsfiddle.net/oznts5bv/

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.container {
  display:flex;
  flex-wrap:wrap;
  border:1px dashed magenta;
}

.container div {
  width:100px;
  height:60px;
  border:1px solid black;
}

<!-- language: lang-html -->

&lt;div class=&quot;container&quot;&gt;
  &lt;div&gt;1&lt;/div&gt;
  &lt;div&gt;2&lt;/div&gt;
  &lt;div&gt;3&lt;/div&gt;
  &lt;div&gt;4&lt;/div&gt;
  &lt;div&gt;5&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

以下是您要翻译的内容:

Created an two examples: with display: flex;, and display: grid;. The second option will probably suit you better:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  display: flex;
  flex-direction: column;
  align-items: center;
  overflow-y: scroll;
}

.container {
  display: flex;
  flex-wrap: wrap;
  border: 1px dashed magenta;
  max-width: min(100%, 302px);
}

.container div {
  width: 100px;
  height: 60px;
  border: 1px solid black;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
}

<!-- language: lang-html -->

&lt;input type=&quot;range&quot; min=&quot;100&quot; max=&quot;600&quot; value=&quot;302&quot; oninput=&quot;document.querySelectorAll(&#39;.container&#39;).forEach(el =&gt; el.style.maxWidth = &#39;min(100%, &#39; + this.value + &#39;px)&#39;)&quot;&gt;
&lt;div class=&quot;container&quot;&gt;
  &lt;div&gt;1&lt;/div&gt;
  &lt;div&gt;2&lt;/div&gt;
  &lt;div&gt;3&lt;/div&gt;
  &lt;div&gt;4&lt;/div&gt;
  &lt;div&gt;5&lt;/div&gt;
&lt;/div&gt;
&lt;br&gt;
&lt;div class=&quot;container grid&quot;&gt;
  &lt;div&gt;1&lt;/div&gt;
  &lt;div&gt;2&lt;/div&gt;
  &lt;div&gt;3&lt;/div&gt;
  &lt;div&gt;4&lt;/div&gt;
  &lt;div&gt;5&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

英文:

Created an two examples: with display: flex;, and display: grid;. The second option will probably suit you better:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  display: flex;
  flex-direction: column;
  align-items: center;
  overflow-y: scroll;
}

.container {
  display: flex;
  flex-wrap: wrap;
  border: 1px dashed magenta;
  max-width: min(100%, 302px);
}

.container div {
  width: 100px;
  height: 60px;
  border: 1px solid black;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
}

<!-- language: lang-html -->

&lt;input type=&quot;range&quot; min=&quot;100&quot; max=&quot;600&quot; value=&quot;302&quot; oninput=&quot;document.querySelectorAll(&#39;.container&#39;).forEach(el =&gt; el.style.maxWidth = &#39;min(100%, &#39; + this.value + &#39;px)&#39;)&quot;&gt;
&lt;div class=&quot;container&quot;&gt;
  &lt;div&gt;1&lt;/div&gt;
  &lt;div&gt;2&lt;/div&gt;
  &lt;div&gt;3&lt;/div&gt;
  &lt;div&gt;4&lt;/div&gt;
  &lt;div&gt;5&lt;/div&gt;
&lt;/div&gt;
&lt;br&gt;
&lt;div class=&quot;container grid&quot;&gt;
  &lt;div&gt;1&lt;/div&gt;
  &lt;div&gt;2&lt;/div&gt;
  &lt;div&gt;3&lt;/div&gt;
  &lt;div&gt;4&lt;/div&gt;
  &lt;div&gt;5&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月17日 19:08:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271418.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定