Aligning elements horizontally adjacent that are in different grid cells with CSS Grid and Flexbox

Viewed 472

Currently I'm using a mixture between grid and flexbox to try and achieve a layout for a form.

I would like both input elements to horizontally lineup without setting a height on them. If I were to apply for padding on the text input, the range should continue to vertically center with the input adjacent to it.

Additionally I would like the label elements to also remain horizontally aligned too.

Please suggest solutions that DO NOT require changing the markup or using Javascript.

Currently what I have: enter image description here

What I'm trying to achieve: enter image description here

I know I could apply a fixed height or padding on the range to align it but I'm trying to make this as fluid as possible.

HTML:

<form class="grid">
  <div class="group">
    <label for="input-1">Label 1</label>
    <input type="text" id="input-1" placeholder="placeholder" />
  </div>

  <div class="group">
    <label for="input-2">Label 2</label>
    <div class=range-wrapper>
      <input type="range" id="input-2" placeholder="placeholder" />
      <output>0</output>
    </div>
  </div>
</form>

SCSS:

html {
  box-sizing: border-box;
}
*, *::before, *::after {
  box-sizing: inherit;
}

body {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 1.2rem;
}

.grid {
  width: 80%;
  max-width: 960px;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-gap: 40px 10px;

  padding: 50px;
  background: #ccc;
}

.group {
  display: flex;
  flex-direction: column;
  margin-top: auto;

  label {
    margin-bottom: 10px;
  }
}

input {
  font-size: inherit;

  &[type="text"] {
    padding: 14px;
  }
  &[type="range"] {
    width: 100%;
  }
}

.range-wrapper {
  display: flex;
  align-items: center;

  output {
  padding: 10px;
}

You can also check out the codepen here.

Many thanks!


** EDIT **

Should support multiline labels, if the label wraps multiple lines, elements should still horizontally align.

Multiline label example: Codepen

1 Answers

try this for your "group" class. (or use a new class for that element with this styling)

.group {
  display: flex;
  flex-direction: column;
  justify-content: center;

  label {
    margin-bottom: 10px;
  }
}
Related