如何使标签浮动在select元素的顶部?

时间:2017-12-13 11:27:15

标签: javascript html css css3

此代码适用于输入,但我不知道如何在select元素上浮动标签。默认选择和禁用选项类别,只有当我单击选择并选择类别时,我想要的标签才会显示在选择元素的顶部。这适用于输入和占位符,但我无法通过选择元素实现此目的。

<form>
<div class="form-group row">
  <div class="col-sm-12">
    <span class="has-float-label">
      <select class="form-control" name="productType" id="productType" v-model="categoryId" data-vv-as="Kategoria" v-validate="'required|numeric'">
        <option value disabled selected>Category *</option>
        <option vaue="cat one">Cat one</option>
        <option vaue="cat two">Cat two</option>
        <option vaue="cat three">Cat three</option>
      </select> 
      <label for="productType">Category *</label>
    </span>     

  </div>
</div>

<div class="form-group row" >
  <div class="col-12 col-sm-12 col-md-6">
    <span class="has-float-label">
      <input type="text" 
             class="form-control" 
             name="firstName" 
             id="firstName" 
             placeholder="Name *" 
             v-model="user.first_name"
             data-vv-as="Name" 
             v-validate="'required|alpha_spaces'">
      <label for="firstName">Emri*</label>
    </span>

  </div>
  <div class="col-12 col-sm-12 col-md-6">
    <span class="has-float-label">
      <input type="text" 
             class="form-control"
             name="lastName" 
             id="lastName" 
             placeholder="Last Name *" 
             v-model="user.last_name" 
             data-vv-as="Last Name"
             v-validate="'required|alpha_spaces'">
      <label for="secondName">Last Name *</label>
    </span>

  </div>
  </div>
</form>

//===css===


.form-control{
  margin: 10px 0;
}
.has-float-label {
    display: block;
    position: relative;
    }
    .has-float-label label, .has-float-label > span{
    position: absolute;
    left: 0;
    top: 0;
    cursor: text;
    font-size: 75%;
    opacity: 1;
    transition: all .8s;
    top: -.5rem;
    left: 0.55rem;
    z-index: 3;
    line-height: 1;
    padding: 0 1px;
    }
    .has-float-label label::after, .has-float-label > span::after {
    content: " ";
    display: block;
    position: absolute;
    background: white;
    height: 2px;
    top: 50%;
    left: -.2em;
    right: -.2em;
    z-index: -1;
    }
    .has-float-label .form-control::-webkit-input-placeholder {
    opacity: 1;
    transition: all .8s;
    }
    .has-float-label .form-control::-moz-placeholder {
    opacity: 1;
    transition: all .8s;
    }
    .has-float-label .form-control:-ms-input-placeholder {
    opacity: 1;
    transition: all .8s;
    }
    .has-float-label .form-control::placeholder {
    opacity: 1;
    transition: all .8s;
    }
    .has-float-label .form-control:placeholder-shown:not(:focus)::-webkit-input-placeholder {
    opacity: 0;
    }
    .has-float-label .form-control:placeholder-shown:not(:focus)::-moz-placeholder {
    opacity: 0;
    }
    .has-float-label .form-control:placeholder-shown:not(:focus):-ms-input-placeholder {
    opacity: 0;
    }
    .has-float-label .form-control:placeholder-shown:not(:focus)::placeholder {
    opacity: 0;
    }
    .has-float-label .form-control:placeholder-shown:not(:focus) + * {
    font-size: 1rem;
    opacity: .5;
    top: .6em;
    line-height: 1;
    color: #495057;
    }

    .input-group .has-float-label {
    -webkit-box-flex: 1;
    -webkit-flex-grow: 1;
        -ms-flex-positive: 1;
            flex-grow: 1;
    margin-bottom: 0;
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-orient: vertical;
    -webkit-box-direction: normal;
    -webkit-flex-direction: column;
        -ms-flex-direction: column;
            flex-direction: column;
    -webkit-box-pack: center;
    -webkit-justify-content: center;
        -ms-flex-pack: center;
            justify-content: center;
    }
    .input-group .has-float-label .form-control {
    width: 100%;
    border-radius: 0.25rem;
    }
    .input-group .has-float-label:not(:last-child), .input-group .has-float-label:not(:last-child) .form-control {
    border-bottom-right-radius: 0;
    border-top-right-radius: 0;
    border-right: 0;
    }
    .input-group .has-float-label:not(:first-child), .input-group .has-float-label:not(:first-child) .form-control {
    border-bottom-left-radius: 0;
    border-top-left-radius: 0;
    }

https://jsfiddle.net/bernardo86/7t56f557/

1 个答案:

答案 0 :(得分:1)

.has-float-label .form-control:placeholder-shown:not(:focus) + * {
  top: .6em;
}

如果你仔细看到这段代码。你会找到解决方案。这意味着哪个带占位符的输入不在focus中,然后保留label top:0.6em

但是select没有占位符。所以你可以

.has-float-label.select-label label{
    top:.9em;
}
.select-label select:focus + label{
  top:-0.5em;
}

fiddle

您可以调整字体大小和间距。