5

Having this:

  <label>
    <button>Create</button>
  </label>

I want button to be aligned to the right like this

----------------------------
|                   [create]|
----------------------------

while having this:

  <label>
    <button>Delete</button>
    <button>Update</button>
  </label>

I want buttons to be in the corners

----------------------------
|[delete]          [update]|
----------------------------

Without adding additional classes to the label.

Ilya Novojilov
  • 871
  • 8
  • 12
  • @NenadVracar provides a perfect answer IMO. If you want an explanation for how it works, see here: http://stackoverflow.com/q/32551291/3597276 – Michael Benjamin Nov 06 '16 at 13:34

3 Answers3

6

You can just use margin-left: auto on last-child and that will produce desired result.

label {
  display: flex;
  border-bottom: 1px solid #aaa;
  margin: 20px 0;
}
label button:last-child {
  margin-left: auto;
}
<label>
  <button>Create</button>
</label>

<label>
  <button>Delete</button>
  <button>Update</button>
</label>
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
2

You can do this using nothing but standard flexbox properties:

  • flex-direction: row-reverse - put the items in a row, start from "the end" (depends on reading direction)
  • justify-content: space-between - put the items as far away from each other as possible

label {
  display: flex;
  flex-direction: row-reverse;
  justify-content: space-between;
}
<label>
  <button>Create</button>
</label>

<label>
  <button>Delete</button>
  <button>Update</button>
</label>
damd
  • 6,116
  • 7
  • 48
  • 77
0

You can use CSS Flexbox justify-content property to align your elements.

Have a look at this Codepen.

Or look at the code below for reference, also I've used <div>s in place of <label> as in the .two they are acting on both the buttons.

div {
  display: flex;
  background: #ddd;
  padding: 10px 20px;
  margin-bottom: 10px;
}

div:first-child {
  justify-content: flex-end;
}

div:nth-child(2) {
  justify-content: space-between;
}
<div>
  <button>Create</button>
</div>

<div>
  <button>Delete</button>
  <button>Update</button>
</div>

Learn more about CSS Flexbox

Hope this helps!

Saurav Rastogi
  • 9,575
  • 3
  • 29
  • 41