2

I am using flexbox to center a form vertically and horizontally. Inside this form I'd like to pin a button to the bottom right of the flexbox container. I am not sure how to get the button pinned to the bottom right though.

html,
body {
  height: 100%;
}

.container {
  height: 100%;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
}

form {
  border: 1px solid gray;
  padding: 1em;
}

.form-button {
  margin-top: 1em;
  align-self: flex-end;
}
<div class="container">
  <form>
    <div class="form-input">
      <label> Name <input type="text" /></label>
    </div>
    <div class="form-button">
      <button type="submit">Submit</button>
    </div>
  </form>
</div>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Mdd
  • 4,140
  • 12
  • 45
  • 70

3 Answers3

3

You just need to make the form element a flex container, because flex properties only work between parent and child elements.

In other words, your align-self: flex-end on the .form-button is not working because the parent – form – does not have display: flex or display: inline-flex applied.

Here's a more complete explanation:

html, body {
  height: 100%;
}

.container {
  height: 100%;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
}

form {
  border: 1px solid gray;
  padding: 1em;

  /* NEW */
  display: flex;
  flex-direction: column;
}

.form-button {
  margin-top: 1em;
  align-self: flex-end;
}
<div class="container">
  <form>
    <div class="form-input">
      <label> Name <input type="text" /></label>
    </div>
    <div class="form-button">
      <button type="submit">Submit</button>
   </div>
  </form>
</div>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
1
.form-button {
  margin-top: 1em;
  align-self: flex-end;
  display: flex;
  justify-content: flex-end;
}
kyun
  • 9,710
  • 9
  • 31
  • 66
0

Just insert float: right;

like this:

.form-button {
  float: right;<-----------added
  //more code...
}

html,
body {
  height: 100%;
}

.container {
  height: 100%;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
}

form {
  border: 1px solid gray;
  padding: 1em;
}

.form-button {
  margin-top: 1em;
  float: right;
}
<div class="container">
  <form>
    <div class="form-input">
      <label> Name <input type="text" /></label>
    </div>
    <div class="form-button">
      <button type="submit">Submit</button>
    </div>
  </form>
</div>
Ehsan
  • 12,655
  • 3
  • 25
  • 44