skip to Main Content

I have styled inputs (I’ll put the CSS here, which has ultimately got too complicated and I have lost track of where I need to remove and fix). I am trying to get labels to fit above my input, my select options and my output.

/* ================== */
/* === GLOBAL CSS === */
/* ================== */

/* Input Placeholder and Select Text Size */

input[type="number"]::-webkit-input-placeholder {
  font-size: 75%;
  line-height: 1.5;
}

input[type="number"]::-moz-placeholder {
  font-size: 75%;
  line-height: 1.5;
}

input[type="number"]:-moz-placeholder {
  font-size: 75%;
  line-height: 1.5;
}

input[type="number"]:-ms-input-placeholder {
  font-size: 75%;
  line-height: 1.5;
}

select.form-select {
  font-size: 75%;
  box-sizing: border-box;
}

select.form-select,
input.reset_form {
  height: calc(1.5em + 0.75rem + 2px);
}

.form-group .row .col:first-child {
  padding-right: 0;
}

.form-group .row .col:last-child {
  width: auto;
}

.form-group {
  display: flex;
  align-items: center;
}

.form-group .row {
  flex: 1;
}

.form-group .row .col:first-child {
  flex: 1;
}

label.form-label {
  padding-top: 6px;
}

/* Set height of input boxes */

.form-control.input-rounded-start {
  height: 34px;
}

.input-group-text {
  height: 34px;
  padding: 0.375rem 0.75rem;
}




/* ================= */
/* === LOCAL CSS === */
/* ================= */


td.form-group {
  display: flex;
  align-items: center;
}

td.form-group label.form-label {
  padding-top: 6px;
}

.input-wrapper {
  display: flex;
  align-items: center;
}

.input-group {
  display: flex;
  width: 100%;
}

.input-group input {
  width: 75%;
  display: inline-block;
  vertical-align: middle;
  height: calc(1.5em + 0.75rem + 2px);
  padding: 0.375rem 0.75rem;
  font-size: 1rem;
  font-weight: 400;
  line-height: 1.5;
  color: #495057;
  background-color: #fff;
  border: 1px solid #ced4da;
  border-radius: 0.25rem 0 0 0.25rem;
}

.input-group select {
  max-width: 20%;
  display: inline-block;
  vertical-align: middle;
  height: calc(1.5em + 0.75rem + 2px);
  padding: 0.375rem 0.75rem;
  font-size: 1rem;
  font-weight: 400;
  line-height: 1.5;
  color: #495057;
  background-color: #f0f0f0;
  border: 1px solid #ced4da;
  border-left: none;
  border-radius: 0 0.25rem 0.25rem 0;
}

.row-input {
  display: flex;
  align-items: center;
  padding: 0 10px;
}

.row-input .form-control {
  height: calc(1.5em + 0.75rem + 2px);
}

.input-group {
  display: flex;
  align-items: center;
  flex: 1;
}

.input-group input {
  width: 60%;
}

.input-group select {
  width: 15%;
  margin-left: -1px;
}

.output-group {
  width: 25%;
  margin-left: -1px;
}

.select-input {
  width: 10% !important;
}

.output-group {
  background-color: #f8f8f8;
  border: 1px solid #ccc;
  color: #555;
  font-style: italic;
}

.input-label-container {
  position: relative;
}

.label-above {
  position: absolute;
  top: -1.3em;
  font-size: 0.8em;
  opacity: 0.7;
}
<section class="container border border-primary bg_cont mb-3">
  <div class="row justify-content-center">
    <div class="col-lg-10 mx-auto">
      <div class="card shadow-sm p-3 mt-1 mb-1">
        <span></span>
        <div class="card-body">
          <div class="row mb-3 row-input">
            <div class="col-md-12">
              <label for="input" class="form-label d-none d-sm-inline">Input</label>
              <div class="input-group p-1">
                <input type="number" name="" id="" class="reset_form formCheck maxAllow form-control input-rounded-start">
                <!-- Label Needs To Go Here -->
                <select class="form-select select-input" name="unitsVelocity" id="units">
                  <option value="c" selected>c</option>
                </select>
                <!-- Label Needs To Go Here -->
                <div class="form-control output-group" id="value">
                  *stuff*</div>
              </div>
            </div>
          </div>
        </div>

        <!--          More stuff  -->
</section>

The labels, if I put them there, just break the input and select boxes. (picture below).
I know I need to get these into columns but I want the output to look like the picture below except with labels above the elements.

I’ve tried manipulating columns etc, but this ultimately ends up breaking the boxes.

enter image description here

I have numerous CSS to keep my inputs the way I want them (check snippet).

The lavel-above and container idea was good, but it didn’t put the labels above without breaking the input box.

This is the best I have at the moment… any ideas would be wonderful. I am using Bootstrap 5.2

Summary: I would like labels above my select box and my output box the same way I have a label above my input box. I want to keep the same style for all my input, select and output, but just with labels above.

2

Answers


  1. The following snippet fix your dropdown:

    /* ================== */
    /* === GLOBAL CSS === */
    /* ================== */
    
    /* Input Placeholder and Select Text Size */
    
    input[type="number"]::-webkit-input-placeholder {
      font-size: 75%;
      line-height: 1.5;
    }
    
    input[type="number"]::-moz-placeholder {
      font-size: 75%;
      line-height: 1.5;
    }
    
    input[type="number"]:-moz-placeholder {
      font-size: 75%;
      line-height: 1.5;
    }
    
    input[type="number"]:-ms-input-placeholder {
      font-size: 75%;
      line-height: 1.5;
    }
    
    select.form-select {
      font-size: 75%;
      box-sizing: border-box;
    }
    
    select.form-select,
    input.reset_form {
      height: calc(1.5em + 0.75rem + 2px);
    }
    
    .form-group .row .col:first-child {
      padding-right: 0;
    }
    
    .form-group .row .col:last-child {
      width: auto;
    }
    
    .form-group {
      display: flex;
      align-items: center;
    }
    
    .form-group .row {
      flex: 1;
    }
    
    .form-group .row .col:first-child {
      flex: 1;
    }
    
    label.form-label {
      padding-top: 6px;
    }
    
    /* Set height of input boxes */
    
    .form-control.input-rounded-start {
      height: 34px;
    }
    
    .input-group-text {
      height: 34px;
      padding: 0.375rem 0.75rem;
    }
    
    
    
    
    /* ================= */
    /* === LOCAL CSS === */
    /* ================= */
    
    
    td.form-group {
      display: flex;
      align-items: center;
    }
    
    td.form-group label.form-label {
      padding-top: 6px;
    }
    
    .input-wrapper {
      display: flex;
      align-items: center;
    }
    
    .input-group {
      display: flex;
      width: 100%;
    }
    
    .input-group input {
      width: 75%;
      display: inline-block;
      vertical-align: middle;
      height: calc(1.5em + 0.75rem + 2px);
      padding: 0.375rem 0.75rem;
      font-size: 1rem;
      font-weight: 400;
      line-height: 1.5;
      color: #495057;
      background-color: #fff;
      border: 1px solid #ced4da;
      border-radius: 0.25rem 0 0 0.25rem;
    }
    
    .input-group select {
      display: inline-block;
      vertical-align: middle;
      height: calc(1.5em + 0.75rem + 2px);
      padding: 0.375rem 0.75rem;
      font-size: 1rem;
      font-weight: 400;
      line-height: 1.5;
      color: #495057;
      background-color: #f0f0f0;
      border: 1px solid #ced4da;
      border-left: none;
      border-radius: 0 0.25rem 0.25rem 0;
    }
    
    .row-input {
      display: flex;
      align-items: center;
      padding: 0 10px;
    }
    
    .row-input .form-control {
      height: calc(1.5em + 0.75rem + 2px);
    }
    
    .input-group {
      display: flex;
      align-items: center;
      flex: 1;
    }
    
    .input-group input {
      width: 60%;
    }
    
    .input-group select {
      margin-left: -1px;
    }
    
    .output-group {
      width: 25%;
      margin-left: -1px;
    }
    
    .output-group {
      background-color: #f8f8f8;
      border: 1px solid #ccc;
      color: #555;
      font-style: italic;
    }
    
    .input-label-container {
      position: relative;
    }
    
    .label-above {
      position: absolute;
      top: -1.3em;
      font-size: 0.8em;
      opacity: 0.7;
    }
    <section class="container border border-primary bg_cont mb-3">
      <div class="row justify-content-center">
        <div class="col-lg-10 mx-auto">
          <div class="card shadow-sm p-3 mt-1 mb-1">
            <span></span>
            <div class="card-body">
              <div class="row mb-3 row-input">
                <div class="col-md-12">
                  <label for="input" class="form-label d-none d-sm-inline">Input</label>
                  <div class="input-group p-1">
                    <input type="number" name="" id="" class="reset_form formCheck maxAllow form-control input-rounded-start">
                    <!-- Label Needs To Go Here -->
                    <select class="form-select select-input" name="unitsVelocity" id="units">
                      <option value="a">a</option>
                      <option value="b">b</option>
                      <option value="c" selected>c</option>
                    </select>
                    <!-- Label Needs To Go Here -->
                    <div class="form-control output-group" id="value">
                      *stuff*</div>
                  </div>
                </div>
              </div>
            </div>
    
            <!--          More stuff  -->
    </section>
    Login or Signup to reply.
  2. Bootstrap 5, AFAIK, doesn’t have a built-in way to handle labels above inputs within input groups. That said, if you carefully follow the documentation, you can achieve something quite similar with floating labels and input groups.

    Here is a working snippet, with no additional CSS.

    Note the use of .form-floating, placeholders and the placement of the labels after the inputs, as per the documentation.

    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    
    <div class="container-fluid mt-3">
      <div class="input-group">
        <div class="form-floating">
          <input type="text" class="form-control" id="velocity" aria-label="velocity" placeholder="Velocity (v)">
          <label for="velocity">Velocity (v)</label>
        </div>
        <div class="form-floating">
          <select class="form-control form-select" id="velocityUnits" placeholder="Velocity Units">
            <option value="a">a</option>
            <option value="b">b</option>
            <option value="c">c</option>
          </select>
          <label for="velocityUnits">Velocity Units</label>
        </div>
        <div class="form-floating">
          <span class="input-group-text form-control" id="gammaFactor">Gamma Factor (y)</span>
          <label for="gammaFactor">Output</label>
        </div>
      </div>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search