Search code examples
htmlcssflexboxstyled-components

Positioning two divs side by side using CSS


I've mocked up my problem using codepen, but each of the elements within are React components so I don't want to reduce them. I'm struggling to display my progress-container on an equal level, but to the left of the input-wrapper (input).

Codepen: https://codepen.io/simoncunningham/pen/mdwMgyL

<div class="progress-container"></div>
<div class="input-wrapper">
  <div class="input">I have my first input here</input>
</div>
.progress-container {
  height: 22px;
  width: 22px;
  margin-right: 16px;
  background-color: red;
  display: flex;
}

.input-wrapper {
  border: 1px solid #ced4da;
  border-radius: 2px;
  overflow-y: hidden;
  padding: 0.375rem 1.75rem 0.375rem 0.75rem;
}

Solution

  • You can wrap the 2 div's in another div with a class of wrapperDiv:

    <div class="wrapperDiv">
      <div class="progress-container"></div>
      <div class="input-wrapper">
        <div class="input">I have my first input here</input>
      </div>
    </div>
    

    Then you can define the wrapperDiv class as follows which will make them side by side:

    .wrapperDiv {
      display: flex;
      flex-direction: row;
      align-items: center;
    }
    

    These css properties represent flexbox layout, in case you want to read more about it.