0

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 :

.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;
}
<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>

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?

MrSolarius
  • 599
  • 11
  • 28

1 Answers1

0

Here's my suggestion:

.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;
}
<p>What I want with my button positioned at the end</p>
<div class="container">
  <div class="spacer"></div>
  <span class="text">My title is a bit long</span>
  <button class="ico">IC</button>
</div>

Here's what it gives me:

enter image description here

Hope that my answer helps you.

Sfili_81
  • 2,377
  • 8
  • 27
  • 36
Iliasse
  • 94
  • 6