限制使用 display flex space between 时子项目之间的间距

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

Limit the space between of the child items when using using display flex space between

问题

我有一个宽度为100%的div,里面有3个按钮。我需要让它们之间有一点间距。

我使用了Bootstrap,并且当我使用display flex和space-between选项时,得到了以下效果。

.container {
    display: flex;
    flex-direction: row;
    text-align: center;
    font-weight: normal;
    font-size: 16px;
    line-height: 19px;
    align-items: center;
    justify-content: space-between;
}

.child-element {
    background: #FFFFFF;
    box-sizing: border-box;
    width: 100px;
    height: 60px;
    border: solid 1px #C4C4C4;
}

.child-element .selected {
    background: #FFED00;
}
<div class="container">
    <button class="child-element" data-option="1"><span style="color:#000">First</span></button>
    <button class="child-element" data-option="2"><span style="color:#000">Second</span></button>
    <button class="child-element" data-option="3"><span style="color:#000">Third</span></button>
</div>

这是我想要看到的效果:

限制使用 display flex space between 时子项目之间的间距

在这里,我需要做哪些更改?此外,我认为使用margin-left和margin-right不是一个好选择。但如果我错了,请纠正我。

英文:

I have a div (100% width) with 3 buttons in it. I need to keep them with a little margin one to another.

I used bootstrap and this is what I get when I used display flex and space-between option.

限制使用 display flex space between 时子项目之间的间距

The code;

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

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

.container {
    display: flex;
    flex-direction: row;
    text-align: center;
    font-weight: normal;
    font-size: 16px;
    line-height: 19px;
    align-items: center;
    justify-content: space-between;
}

.child-element {
    background: #FFFFFF;
    box-sizing: border-box;
    width: 100px;
    height: 60px;
    border: solid 1px #C4C4C4;
}

.child-element .selected {
    background: #FFED00;
}

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

&lt;div class=&quot;container&quot;&gt;
    &lt;button class=&quot;child-element&quot; data-option=&quot;1&quot;&gt;&lt;span style=&quot;color:#000&quot;&gt;First&lt;/span&gt;&lt;/button&gt;
    &lt;button class=&quot;child-element&quot; data-option=&quot;2&quot;&gt;&lt;span style=&quot;color:#000&quot;&gt;Second&lt;/span&gt;&lt;/button&gt;
    &lt;button class=&quot;child-element&quot; data-option=&quot;3&quot;&gt;&lt;span style=&quot;color:#000&quot;&gt;Third&lt;/span&gt;&lt;/button&gt;
&lt;/div&gt;

<!-- end snippet -->

This is how I want to see them;

限制使用 display flex space between 时子项目之间的间距

What are the changes I need to do here? Also, I think using margin-left,right is not a good option. But correct me if I'm wrong

答案1

得分: 2

You could remove justify-content: space-between and add some gap such as gap: 8px:

.container {
    display: flex;
    flex-direction: row;
    text-align: center;
    font-weight: normal;
    font-size: 16px;
    line-height: 19px;
    align-items: center;

    /* add gap */
    gap: 8px;
}

.child-element {
    background: #FFFFFF;
    box-sizing: border-box;
    width: 100px;
    height: 60px;
    border: solid 1px #C4C4C4;
}

.child-element .selected {
    background: #FFED00;
}
<div class="container">
    <button class="child-element" data-option="1"><span style="color:#000">First</span></button>
    <button class="child-element" data-option="2"><span style="color:#000">Second</span></button>
    <button class="child-element" data-option="3"><span style="color:#000">Third</span></button>
</div>
英文:

You could remove justify-content: space-between and add some gap such as gap: 8px:

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

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

.container {
    display: flex;
    flex-direction: row;
    text-align: center;
    font-weight: normal;
    font-size: 16px;
    line-height: 19px;
    align-items: center;
    
    /* add gap */
    gap: 8px;
}

.child-element {
    background: #FFFFFF;
    box-sizing: border-box;
    width: 100px;
    height: 60px;
    border: solid 1px #C4C4C4;
}

.child-element .selected {
    background: #FFED00;
}

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

&lt;div class=&quot;container&quot;&gt;
    &lt;button class=&quot;child-element&quot; data-option=&quot;1&quot;&gt;&lt;span style=&quot;color:#000&quot;&gt;First&lt;/span&gt;&lt;/button&gt;
    &lt;button class=&quot;child-element&quot; data-option=&quot;2&quot;&gt;&lt;span style=&quot;color:#000&quot;&gt;Second&lt;/span&gt;&lt;/button&gt;
    &lt;button class=&quot;child-element&quot; data-option=&quot;3&quot;&gt;&lt;span style=&quot;color:#000&quot;&gt;Third&lt;/span&gt;&lt;/button&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月5日 16:49:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76404801.html
匿名

发表评论

匿名网友

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

确定