Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactJS: How can I align form <Field>'s <input> and <button> and <h1> in a row inside a <div>?

I have <button/> and <input/> inside a container. How can I get them to align in the same row, next to each other, and in the middle of the container vertically?

This is what I have so far:

enter image description here

The top black portion is a navigation bar and I want to side-by-side align them in a row, next to each other from left to right, and align them vertically in the middle of the navigation bar:

PLEASE TAKE A LOOK AT EDIT AT THE BOTTOM FOR UPDATED CODE

       import { Field, reduxForm } from 'redux-form';

       const form = reduxForm({
         form: 'register'
       });
       
       ...

  

#wrapper {
  width: 100%;
  margin: 0 auto;
}

.container {
  display: flex;
  justify-content: center;
}

header {
  width: 100%;
  height: 85px;
  overflow: hidden;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999;
  background-color: black;
}

header h1#logo {
  display: inline-block;
  height: 85px;
  line-height: 85px;
  float: left;
  font-size: 25px;
  color: purple;
  font-weight: 500;
}

#input-field {
  background-color: rgba(255, 255, 255, 0.1);
  border-radius: 5px;
  width: 150px;
  height: 40px;
  padding: 5px;
}
<div id="wrapper">
  <header>
    <div className="container">
      <h1>TESTING</h1>
      <form>
        <Field name="email" className="form-control" component={ <div>
          <input
            className='form-control'
            id='input-field'
            placeholder='Enter Email'
          />
    </div>
    } type="text" />
    <button type="submit" className="btn">Register</button>
    </form>
</div>
</header>
</div>

EDIT

Decided to take the Form out but CSS stay the same. How can I flex align the following to achieve what I originally wanted (Would like to have the h1 to the left and text field + button to the right?

  <div id="wrapper">
    <header>
       <div className="container">
          <h1>
            TESTING
          </h1>
          <input
            className="form-control"
            id="input-field"
            placeholder='Enter Email'
          />
          <button
            type="submit"
            className="btn"
          >
            Register
          </button>
      </div>
    </header>
  </div>

1 Answers

Centers flex items vertically in a horizontal row within the .container.

.container {
    display: flex;
    align-items: center;
}
like image 90
Danish Iqbal Avatar answered Dec 08 '25 10:12

Danish Iqbal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!