ul 列表项之间为什么没有间距?

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

why ul-list items not getting space between them?

问题

我试图创建一条线,其中列表项在一行中应具有间隔,但我不明白为什么在使用 display: flex; 和应用 justify-content: space-between 时没有生效。

.container {
  border: 2px solid red;
  background-color: antiquewhite;
  height: 650px;
  display: flex;
  justify-content: space-around;
}

.ul-list {
  display: flex;
  list-style-type: none;
  border: 2px solid green;
  margin-top: 0;
  margin-left: 40px;
  margin-right: 50px;
  justify-content: space-between;
}

.list-item {
  border: 2px solid red;
}
<div class="container">
  <div class="n1">
    <div>Left</div>
  </div>
  <div class="list1">
    <ul class="ul-list">
      <li class="list-item">ONE</li>
      <li class="list-item">TWO</li>
      <li class="list-item">THREE</li>
      <li class="list-item">FOUR</li>
      <li class="list-item">FIVE</li>
    </ul>
  </div>
  <div class="n1">
    <div>RIGHT</div>
  </div>
</div>

我知道可以通过应用外边距来实现,但为什么不能使用 space-around 呢?

英文:

I am trying to create a line in which the list item should have space in a row but I'm not getting why it is not applying when using display: flex; and justify-content: space-between is applied in ul-list class

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

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

.container {
  border: 2px solid red;
  background-color: antiquewhite;
  height: 650px;
  display: flex;
  justify-content: space-around;
}

.ul-list {
  display: flex;
  list-style-type: none;
  border: 2px solid green;
  margin-top: 0;
  margin-left: 40px;
  margin-right: 50px;
  justify-content: space-between;
}

.list-item {
  border: 2px solid red;
}

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

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;n1&quot;&gt;
    &lt;div&gt;Left&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;list1&quot;&gt;
    &lt;ul class=&quot;ul-list&quot;&gt;
      &lt;li class=&quot;list-item&quot;&gt;ONE &lt;/li&gt;
      &lt;li class=&quot;list-item&quot;&gt;TWO&lt;/li&gt;
      &lt;li class=&quot;list-item&quot;&gt;THREE&lt;/li&gt;
      &lt;li class=&quot;list-item&quot;&gt;FOUR&lt;/li&gt;
      &lt;li class=&quot;list-item&quot;&gt;FIVE&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
  &lt;div class=&quot;n1&quot;&gt;
    &lt;div&gt;RIGHT&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

I know this can be done by applying a margin, but why it's not getting why not with space-around?

答案1

得分: 1

justify-content: space-between 只有在 flex 容器的宽度大于项目的宽度时才会在项目之间添加空白间隔。

在你的情况下,你可以为 .ul-list 设置一个比内容更宽的宽度,或者按照 @ImranMalik 的建议设置一个 gap

请注意,以上是 CSS 样式的说明,无需翻译。

英文:

justify-content: space-between will put space between your flex items only if the flex container is larger than the items.

In your case, you could set a width for .ul-list which is wider than the contents, or you can set a gap as suggested by @ImranMalik.

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

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

.container {
  border: 2px solid red;
  background-color: antiquewhite;
  height: 50px;
  display: flex;
  justify-content: space-around;
  margin-bottom: 1em;
}
.ul-list {
  display: flex;
  list-style-type: none;
  border: 2px solid green;
  justify-content: space-between;
  margin: 0;
  padding: 0;
}
.list-item {
  border: 2px solid red;
}

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

&lt;div class=&quot;container&quot;&gt;
    &lt;div class=&quot;n1&quot;&gt;
        &lt;div&gt;Left&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;list1&quot;&gt;
        &lt;ul class=&quot;ul-list&quot;&gt;
            &lt;li class=&quot;list-item&quot;&gt;ONE &lt;/li&gt;
            &lt;li class=&quot;list-item&quot;&gt;TWO&lt;/li&gt;
            &lt;li class=&quot;list-item&quot;&gt;THREE&lt;/li&gt;
            &lt;li class=&quot;list-item&quot;&gt;FOUR&lt;/li&gt;
            &lt;li class=&quot;list-item&quot;&gt;FIVE&lt;/li&gt;
        &lt;/ul&gt;
    &lt;/div&gt;
    &lt;div class=&quot;n1&quot;&gt;
      &lt;div&gt;Right&lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&quot;container&quot;&gt;
    &lt;div class=&quot;n1&quot;&gt;
        &lt;div&gt;Left&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;list1&quot;&gt;
        &lt;ul class=&quot;ul-list&quot; style=&quot;width: 285px;&quot;&gt;
            &lt;li class=&quot;list-item&quot;&gt;ONE &lt;/li&gt;
            &lt;li class=&quot;list-item&quot;&gt;TWO&lt;/li&gt;
            &lt;li class=&quot;list-item&quot;&gt;THREE&lt;/li&gt;
            &lt;li class=&quot;list-item&quot;&gt;FOUR&lt;/li&gt;
            &lt;li class=&quot;list-item&quot;&gt;FIVE&lt;/li&gt;
        &lt;/ul&gt;
    &lt;/div&gt;
    &lt;div class=&quot;n1&quot;&gt;
      &lt;div&gt;Right&lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&quot;container&quot;&gt;
    &lt;div class=&quot;n1&quot;&gt;
        &lt;div&gt;Left&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;list1&quot;&gt;
        &lt;ul class=&quot;ul-list&quot; style=&quot;gap: 1em;&quot;&gt;
            &lt;li class=&quot;list-item&quot;&gt;ONE &lt;/li&gt;
            &lt;li class=&quot;list-item&quot;&gt;TWO&lt;/li&gt;
            &lt;li class=&quot;list-item&quot;&gt;THREE&lt;/li&gt;
            &lt;li class=&quot;list-item&quot;&gt;FOUR&lt;/li&gt;
            &lt;li class=&quot;list-item&quot;&gt;FIVE&lt;/li&gt;
        &lt;/ul&gt;
    &lt;/div&gt;
    &lt;div class=&quot;n1&quot;&gt;
      &lt;div&gt;Right&lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

.ul-list { gap: 10px; }

使用 gap 属性在 flex 项目之间添加间隔。

英文:

.ul-list {
gap: 10px;
}

Use gap property to add spaces between flex items.

huangapple
  • 本文由 发表于 2023年7月20日 13:11:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726839.html
匿名

发表评论

匿名网友

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

确定