Search code examples
cssbuttoninputflexboxwidth

CSS - input field and button to be of same width


I made a simple example of my issue here.

I want the button to be of the same width as the input field. The only thing is, there's a padding-left of 1rem on the input field because I don't want the placeholder sticking directly to the left side of the field. I assume that has to do with the solution to this, but I'm stuck.

Help much appreciated!

html {
  font-size: 12px;
}

.login-form {
  display: flex;
  flex-direction: column; 
  align-items: center;
  width: 20rem;
  padding: 1em;
  background-color: #F00;
}

.form-control {
  width: 100%;
  border: 0;
  padding-left: 1rem;
  margin-bottom: 1rem;      
  color: #000;
}

.btn-sub {
  width: 100%;
}
<div class=container>
  <form class=login-form>
    <input class="form-control" placeholder="Enter your email">
    <button class="btn-sub">Subscribe</button>
  </form>
</div>


Solution

  • Adjust the box-sizing of the children to border-box...

    form.login-form * {
        box-sizing: border-box;
    }
    

    html {
      font-size: 12px;
    }
    
    .login-form {
      display: flex;
      flex-direction: column;
      align-items: center;
      width: 20rem;
      padding: 1em;
      background-color: #F00;
    }
    
    .form-control {
      width: 100%;
      border: 0;
      padding-left: 1rem;
      margin-bottom: 1rem;
      color: #000;
    }
    
    form.login-form * {
      box-sizing: border-box;
    }
    
    .btn-sub {
      width: 100%;
    }
    <div class=container>
      <form class=login-form>
        <input class="form-control" placeholder="Enter your email">
        <button class="btn-sub">Subscribe</button>
      </form>
    </div>