Search code examples
htmlcsscenteringcss-grid

Vertical centering of form element in CSS grid


I have a form html/css layout that emulates a Material Design style:

<div class="form-group">
  <input
    type="text"
    id="feedName"
    name="name"
    value={nameValue}
    onChange={this.handleInputChange}
   />
   <label class="control-label" htmlFor="feedName">Name</label>
   <i class="bar"></i>
</div>

and the CSS is:

.form-group {
  position:relative;
}

.form-group input,
.form-group select {
  width: 100%;
}

.form-group input {
  display: block;
  background: none;
  padding: 0.125rem 0.125rem 0.0625rem;
  font-size: 1rem;
  border-width: 0;
  border-color: transparent;
  transition: all 0.28s ease;
  box-shadow: none;
  height: 1.9rem;
}

.form-group .control-label {
  position: absolute;
  top: 0.25rem;
  pointer-events: none;
  padding-left: var(--gutter);
  font-weight: normal;
  transition: all 0.28s ease;
}

.form-group .bar {
  position: relative;
  border-bottom: 0.0625rem solid #999;
  display: block;
}

.form-group .bar::before {
  content: '';
  height: 0.125rem;
  width: 0;
  left: 50%;
  bottom: -0.0625rem;
  position: absolute;
  background: var(--secondaryGreen);
  transition: left 0.28s ease, width 0.28s ease;
  z-index: 2;
}

Essentially, I set relative positioning on form-group and then absolute positioning on the control-label to position the label just above the input field.

I'm trying to center this as a grid-item within a CSS Grid. For just text items, I can center with:

.main > div:not(.form-group) {
  /* text-only item vertical centering */
  display: flex;
  align-items: center;
}

but the flexbox above screws up the form-group. I've also tried:

.form-group {
  position:relative;
  margin: auto 0;
}

but that doesn't work either.

The full code is:

:root {
  --secondaryGreen: rgba(114, 191, 68, 1);
  --gutter: 15px;
}

.main {
  margin-top: 50px;
  border: 1px solid rgba(0, 0, 0, 0.13);
  border-radius: 4px;
  background-color: #fff;
  box-shadow: 0 -1px 2px 0 rgba(0, 0, 0, .05), 0 2px 3px 0 rgba(0, 0, 0, 0.10);
  display: grid;
  grid-template-columns: 100%;
  grid-template-rows: 60px 50px 80px 80px 80px;
}

.main > div {
  padding: 0 var(--gutter);
}

.main > div:not(.form-group) {
  /* text-only item vertical centering */
  display: flex;
  align-items: center;
}

.title {
  font-weight: 600;
  color: var(--primaryBlue);
  font-size: 16px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.13);
}

.form-group {
  position:relative;
  margin: auto 0;
  /*margin-bottom: 2.25rem;*/
  /*margin-top: auto;
  margin-bottom: auto;*/
  /*vertical-align: baseline;*/
}

.form-group input,
.form-group select {
  width: 100%;
}

.form-group input {
  display: block;
  background: none;
  padding: 0.125rem 0.125rem 0.0625rem;
  font-size: 1rem;
  border-width: 0;
  border-color: transparent;
  transition: all 0.28s ease;
  box-shadow: none;
  height: 1.9rem;
}

.form-group .control-label {
  position: absolute;
  top: 0; /* 0.25rem; */
  pointer-events: none;
  padding-left: var(--gutter);
  /*z-index: 1;*/
  /*font-size: 1rem;*/
  font-weight: normal;
  transition: all 0.28s ease;
}

.form-group .bar {
  position: relative;
  border-bottom: 0.0625rem solid #999;
  display: block;
}
.form-group .bar::before {
  content: '';
  height: 0.125rem;
  width: 0;
  left: 50%;
  bottom: -0.0625rem;
  position: absolute;
  background: var(--secondaryGreen);
  transition: left 0.28s ease, width 0.28s ease;
  z-index: 2;
}

.form-group select,
.form-group input:focus,
.form-group input:valid,
.form-group input.form-file,
.form-group input.has-value,
.form-group textarea:focus,
.form-group textarea:valid,
.form-group textarea.form-file,
.form-group textarea.has-value {
  color: #333;
}

.form-group select ~ .control-label,
.form-group input:focus ~ .control-label,
.form-group input:valid ~ .control-label,
.form-group input.form-file ~ .control-label,
.form-group input.has-value ~ .control-label,
.form-group textarea:focus ~ .control-label,
.form-group textarea:valid ~ .control-label,
.form-group textarea.form-file ~ .control-label,
.form-group textarea.has-value ~ .control-label {
  font-size: 0.8rem;
  color: gray;
  top: -1rem;
  left: 0;
}

.form-group select ~ .control-label {
  top: -1.5rem;
}

.form-group select:focus,
.form-group input:focus,
.form-group textarea:focus {
  outline: none;
}
.form-group select:focus ~ .control-label,
.form-group input:focus ~ .control-label,
.form-group textarea:focus ~ .control-label {
  color: var(--secondaryGreen);
}
.form-group select:focus ~ .bar::before,
.form-group input:focus ~ .bar::before,
.form-group textarea:focus ~ .bar::before {
  width: 100%;
  left: 0;
}
<div class="main">
  <div class="title">Title</div>
  <div>Details</div>
  <div class="form-group">
      <input
        type="text"
        id="feedName"
        name="name"
        value=""
        onChange={this.handleInputChange}
      />
      <label class="control-label" htmlFor="feedName">Name</label>
      <i class="bar"></i>
  </div>
  <div class="form-group">
      <select
        id="feedKind"
        name="feedKind"
        onChange={this.handleInputChange}
      >
          <option value="option 1">
             Option 1
           </option>
          <option value="option 2">
             Option 2
           </option>
      </select>
      <label class="control-label" htmlFor="feedKind">Type</label>
      <i class="bar"></i>
    </div>

The location of the input field also seems to be impacted by the state of the select below it. I'm puzzled how a grid-item impacts location of other grid-items.


Solution

  • Make these adjustments to your code:

    (No need for absolute positioning.)

    .main > div {
      display: flex;
      flex-direction: column;
      justify-content: center;     /* vertical centering */
    }
    
    .form-group .control-label {
      order: -1;                  /* position label first in the stack */
    }
    

    :root {
      --secondaryGreen: rgba(114, 191, 68, 1);
      --gutter: 15px;
    }
    
    .main {
      margin-top: 50px;
      border: 1px solid rgba(0, 0, 0, 0.13);
      border-radius: 4px;
      background-color: #fff;
      box-shadow: 0 -1px 2px 0 rgba(0, 0, 0, .05), 0 2px 3px 0 rgba(0, 0, 0, 0.10);
      display: grid;
      grid-template-columns: 100%;
      grid-template-rows: 60px 50px 80px 80px 80px;
    }
    
    .main > div {
      padding: 0 var(--gutter);
      display: flex;
      flex-direction: column;
      justify-content: center;
      /* text-align: center; <-- horizontal centering, if necessary */
      border: 1px dashed red !important; /* demo only */
    }
    
    .main > div + div {
      margin-top: 10px; /* demo only */
    } 
    
    .title {
      font-weight: 600;
      color: var(--primaryBlue);
      font-size: 16px;
      border-bottom: 1px solid rgba(0, 0, 0, 0.13);
    }
    
    .form-group input {
      background: none;
      padding: 0.125rem 0.125rem 0.0625rem;
      font-size: 1rem;
      border-width: 0;
      border-color: transparent;
      transition: all 0.28s ease;
      box-shadow: none;
      height: 1.9rem;
    }
    
    .form-group .control-label {
      order: -1;
      pointer-events: none;
      padding-left: var(--gutter);
      font-weight: normal;
      transition: all 0.28s ease;
    }
    
    .form-group .bar {
      position: relative;
      border-bottom: 0.0625rem solid #999;
      display: block;
    }
    
    .form-group .bar::before {
      content: '';
      height: 0.125rem;
      width: 0;
      left: 50%;
      bottom: -0.0625rem;
      position: absolute;
      background: var(--secondaryGreen);
      transition: left 0.28s ease, width 0.28s ease;
      z-index: 2;
    }
    
    .form-group select,
    .form-group input:focus,
    .form-group input:valid,
    .form-group input.form-file,
    .form-group input.has-value,
    .form-group textarea:focus,
    .form-group textarea:valid,
    .form-group textarea.form-file,
    .form-group textarea.has-value {
      color: #333;
    }
    
    .form-group select ~ .control-label,
    .form-group input:focus ~ .control-label,
    .form-group input:valid ~ .control-label,
    .form-group input.form-file ~ .control-label,
    .form-group input.has-value ~ .control-label,
    .form-group textarea:focus ~ .control-label,
    .form-group textarea:valid ~ .control-label,
    .form-group textarea.form-file ~ .control-label,
    .form-group textarea.has-value ~ .control-label {
      font-size: 0.8rem;
      color: gray;
      top: -1rem;
      left: 0;
    }
    
    .form-group select ~ .control-label {
      top: -1.5rem;
    }
    
    .form-group select:focus,
    .form-group input:focus,
    .form-group textarea:focus {
      outline: none;
    }
    
    .form-group select:focus ~ .control-label,
    .form-group input:focus ~ .control-label,
    .form-group textarea:focus ~ .control-label {
      color: var(--secondaryGreen);
    }
    
    .form-group select:focus ~ .bar::before,
    .form-group input:focus ~ .bar::before,
    .form-group textarea:focus ~ .bar::before {
      width: 100%;
      left: 0;
    }
    <div class="main">
      <div class="title">Title</div>
      <div>Details</div>
      <div class="form-group">
        <input type="text" id="feedName" name="name" value="" onChange={this.handleInputChange} />
        <label class="control-label" htmlFor="feedName">Name</label>
        <i class="bar"></i>
      </div>
      <div class="form-group">
        <select id="feedKind" name="feedKind" onChange={this.handleInputChange}>
          <option value="option 1">
            Option 1
          </option>
          <option value="option 2">
            Option 2
          </option>
        </select>
        <label class="control-label" htmlFor="feedKind">Type</label>
        <i class="bar"></i>
      </div>

    jsFiddle demo