Flexbox:使第一个div居中,第二个div对齐末尾。

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

Flexbox : make first div center and second div align at the end

问题

我想创建一个菜单,其中菜单的名称位于其父元素的中心,而操作按钮位于菜单的末尾。

我尝试过像这样做:

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

<!-- language: lang-css -->
.container{
    display: flex;
    justify-content: flex-start;
    align-items: center;
    flex-direction: row;
    width:16rem;
    background-color:green;
    padding:1rem;
    margin-top:1rem;
}

.text{
    margin-left: auto;
    margin-right: auto;
    background-color:lime;
    padding:1rem;
}

.ico{
    height: 2em;
    width: 2em;
}

<!-- language: lang-html -->
<p>What I get</p>
<div class="container">
  <span class="text">My title is a bit long</span>
  <button class="ico">IC</button>
</div>
<p>What I want with my button positioned at the end</p>
<div class="container">
  <span class="text">My title is a bit long</span>
</div>

<!-- end snippet -->

但正如您所见,文本不再完全位于 div 的中心。

那么,如何保持文本位于 div 的中心,并将按钮添加到末尾呢?

英文:

I want to make a menu where the name of the menu is at the center of his parrent and the action button is at the end of the menu.

I have tried to do something like this :

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

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

.container{
    display: flex;
    justify-content: flex-start;
    align-items: center;
    flex-direction: row;
    width:16rem;
    background-color:green;
    padding:1rem;
    margin-top:1rem;
}

.text{
    margin-left: auto;
    margin-right: auto;
    background-color:lime;
    padding:1rem;
}

.ico{
    height: 2em;
    width: 2em;
}

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

&lt;p&gt;What I get&lt;/p&gt;
&lt;div class=&quot;container&quot;&gt;
  &lt;span class=&quot;text&quot;&gt;My title is a bit long&lt;/span&gt;
  &lt;button class=&quot;ico&quot;&gt;IC&lt;/button&gt;
&lt;/div&gt;
&lt;p&gt;What I want with my button positioned at the end&lt;/p&gt;
&lt;div class=&quot;container&quot;&gt;
  &lt;span class=&quot;text&quot;&gt;My title is a bit long&lt;/span&gt;
&lt;/div&gt;

<!-- end snippet -->

But as you can see, the text is not exactly at the center of the div anymore.

So, how can I keep my text at the center of the div and add my button at the end?

答案1

得分: 0

以下是您的建议:

这是我的建议:

<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->

<!-- 语言:lang-css -->
.container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 16rem;
    background-color: green;
    padding: 1rem;
    margin-top: 1rem;
}

.text {
    background-color: lime;
    padding: 1rem;
}

.ico,
.spacer {
    flex: 0 0 2em;
    height: 2em;
    width: 2em;
}

.spacer {
    visibility: hidden;
}

<!-- 语言:lang-html -->
<p>我希望我的按钮在末尾位置</p>
<div class="container">
    <div class="spacer"></div>
    <span class="text">我的标题有点长</span>
    <button class="ico">IC</button>
</div>

<!-- 结束代码片段 -->

这是它给我的效果:

Flexbox:使第一个div居中,第二个div对齐末尾。

希望我的答案对您有所帮助。


<details>
<summary>英文:</summary>

Here&#39;s my suggestion:

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-css --&gt;

    .container{
        display: flex;
        justify-content: space-between;
        align-items: center;
        width:16rem;
        background-color:green;
        padding:1rem;
        margin-top:1rem;
    }

    .text{
        background-color:lime;
        padding:1rem;
    }

    .ico, .spacer{
        flex: 0 0 2em;
        height: 2em;
        width: 2em;
    }

    .spacer {
        visibility: hidden;
    }

&lt;!-- language: lang-html --&gt;

    &lt;p&gt;What I want with my button positioned at the end&lt;/p&gt;
    &lt;div class=&quot;container&quot;&gt;
      &lt;div class=&quot;spacer&quot;&gt;&lt;/div&gt;
      &lt;span class=&quot;text&quot;&gt;My title is a bit long&lt;/span&gt;
      &lt;button class=&quot;ico&quot;&gt;IC&lt;/button&gt;
    &lt;/div&gt;

&lt;!-- end snippet --&gt;

Here&#39;s what it gives me:

[![enter image description here][1]][1]

Hope that my answer helps you.


  [1]: https://i.stack.imgur.com/2RYKq.png

</details>



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

发表评论

匿名网友

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

确定