4

How to use different align on same row with CSS flex?

I have this:

.buttons-container {
  display: flex;
  flex-direction: row;
  justify-content: center;
}
<div class="buttons-container">
  <button mat-raised-button>A</button>
  <button mat-raised-button>B</button>
  <button mat-raised-button>C</button>
</div>

My result is:

                                 | (center)
+---------------------------------------------------------------------+
|                              A B C                                  |
+---------------------------------------------------------------------+
                                 | (center)

But I need align C to right:

                                 | (center)
+---------------------------------------------------------------------+
|                               A B                                 C |
+---------------------------------------------------------------------+
                                 | (center)

IS IT IMPORTANT TO KEEP div and button

I try other code but KO

.buttons-container {
  display: flex;
  flex-direction: row;
  justify-content: center;
}
.buttons-container>button:last-child {
  flex: 1;
  justify-self: end;
}
<div class="buttons-container">
  <button mat-raised-button>A</button>
  <button mat-raised-button>B</button>
  <button mat-raised-button>C</button>
</div>

none of the proposed solution on Center and right align flexbox elements does work with buttons in a div

.buttons-container div::before {
  content:"D";
  margin: 1px auto 1px 1px;
  visibility: hidden;
  padding: 5px;
  background: #ddd;
}
.buttons-container button:last-child {
  margin-left: auto;
  background: #ddd;
}
.buttons-container div {
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}
.buttons-container button {
  display: flex;
  margin: 1px;
  padding: 5px;
  background: #aaa;
}

/* true center */
p    { text-align: center; position: relative; }
span { background-color: aqua; }
p::before {
  content: "\2191";
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  
}
<div class="buttons-container">
  <button mat-raised-button>A</button>
  <button mat-raised-button>B</button>
  <button mat-raised-button>C</button>
</div>

.buttons-container button:first-child {
  margin-right: auto;
  /*visibility: hidden; */
}
.buttons-container button:last-child {
  margin-left: auto;
  background: #ddd;
}
.buttons-container {
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}
.buttons-container button {
  display: flex;
  margin: 1px;
  padding: 5px;
  background: #aaa;
}

/* true center */
p    { text-align: center; position: relative; }
span { background-color: aqua; }
p::before {
  content: "\2191";
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  
}
<div class="buttons-container">
  <button mat-raised-button>A</button>
  <button mat-raised-button>B</button>
  <button mat-raised-button>C</button>
</div>
Stéphane GRILLON
  • 11,140
  • 10
  • 85
  • 154

0 Answers0