当聚焦时,我已经成功地将标签浮动在表单输入字段之上,但是如果文本输入到字段中并且未聚焦,我会对如何保持标签浮动感到困惑。
在线有大量的纯CSS示例,但没有一个对我来说很明显是什么让填充的字段标签浮动。我相信这很容易,但我对此很新。
编辑:我需要这样做而不使用下面评论中提到的“必需”方法。我的表单是必填字段和非必填字段的组合。
以下是我发现的众多例子之一:http://codepen.io/atunnecliffe/pen/gpKzQw
这是我的jsfiddle:https://jsfiddle.net/yzzxrsnn/1/
提前谢谢了。
HTML:
<fieldset class="form-field">
<textarea maxlength="280" class="form-textarea"></textarea>
<label class="form-label" alt='Search Map' placeholder='Search Map'>Text Area</label>
<br>
<input type='text' class="form-input" placeholder="" onclick='this.value = "";'>
<label class="form-label" alt='Search Map' placeholder='Search Map'>Text</label>
<br>
<select class="form-input" >
<option value="" disabled selected hidden></option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
</select>
<label class="form-label" for="name">Select</label>
</fieldset>
CSS:
.form-input {
box-sizing: border-box;
width: 100%;
max-width: 400px;
height: 55px;
margin: 15px 0 15px 0;
padding: 0px 10px 0px 10px;
border: 3px solid #ccc;
outline: none;
border-radius: 8px;
outline: none;
resize: none;
text-align:left;
-webkit-appearance: none;
background: transparent;
font-family: 'Yanone Kaffeesatz', Arial, sans-serif;
color: #000;
background:#fff;
font-size: 18px;
-webkit-transition: border-color ease-in-out .5s,box-shadow ease-in-out .5s;
transition: border-color ease-in-out .5s,box-shadow ease-in-out .5s;
}
.form-input:focus {
border-color: #00bafa;
}
.form-input + .form-label {
display: block;
pointer-events: none;
margin: 0 0 0 15px;
line-height: 0px;
margin-top: -43px;
margin-bottom: 43px;
transition: transform 1s;
}
.form-label {
font-family: 'Yanone Kaffeesatz', Arial, sans-serif;
color: #ccc;
font-size: 18px;
}
.form-input:focus + .form-label {
transform: translateY(-38px);
font-family: 'Yanone Kaffeesatz', Arial, sans-serif;
color: #00bafa;
font-size: 18px;
}
.form-textarea {
box-sizing: border-box;
width: 100%;
max-width: 400px;
height: 150px;
margin: 15px 0 15px 0;
padding: 15px 10px 0px 10px;
border: 3px solid #ccc;
border-radius: 8px;
outline: none;
resize: none;
text-align:left;
-webkit-appearance: none;
background: transparent;
font-family: 'Yanone Kaffeesatz', Arial, sans-serif;
color: #000;
background:#fff;
font-size: 18px;
-webkit-transition: border-color ease-in-out .5s,box-shadow ease-in-out .5s;
transition: border-color ease-in-out .5s,box-shadow ease-in-out .5s;
}
.form-textarea:focus {
border-color: #00bafa;
}
.form-textarea + .form-label {
display: block;
pointer-events: none;
margin: 0 0 0 15px;
line-height: 0px;
margin-top: -145px;
margin-bottom: 145px;
transition: transform 1s;
}
.form-textarea:focus + .form-label {
transform: translateY(-30px);
font-family: 'Yanone Kaffeesatz', Arial, sans-serif;
color: #00bafa;
font-size: 18px;
}
.form-textarea + .form-label:before {
transform: translateY(-38px);
pointer-events: none;
font-family: 'Yanone Kaffeesatz', Arial, sans-serif;
color: #00bafa;
font-size: 18px;
}
您可以使用jquery执行此操作。使用jquery,我们可以将类添加到输入,如果该输入的值大于0,则为标签。
$(document).ready(function() {
var $input = $('.form-input');
$input.focusout(function() {
if($(this).val().length > 0) {
$(this).addClass('input-focus');
$(this).next('.form-label').addClass('input-focus-label');
}
else {
$(this).removeClass('input-focus');
$(this).next('.form-label').removeClass('input-focus-label');
}
});
});
和一些CSS
.input-focus-label{
transform: translateY(-38px);
font-family: 'Yanone Kaffeesatz', Arial, sans-serif;
color: #00bafa;
font-size: 18px;
}
.input-focus{
border-color: #00bafa;
}
这是一个例子fiddle.Hope这对你有所帮助。
如果为输入元素设置required
,则可以利用:valid
选择器:
.form-input {
box-sizing: border-box;
width: 100%;
max-width: 400px;
height: 55px;
margin: 15px 0 15px 0;
padding: 0px 10px 0px 10px;
border: 3px solid #ccc;
outline: none;
border-radius: 8px;
outline: none;
resize: none;
text-align: left;
-webkit-appearance: none;
background: transparent;
font-family: 'Yanone Kaffeesatz', Arial, sans-serif;
color: #000;
background: #fff;
font-size: 18px;
-webkit-transition: border-color ease-in-out .5s,box-shadow ease-in-out .5s;
transition: border-color ease-in-out .5s,box-shadow ease-in-out .5s;
}
.form-input:focus {
border-color: #00bafa;
}
.form-input + .form-label {
display: block;
pointer-events: none;
margin: 0 0 0 15px;
line-height: 0px;
margin-top: -43px;
margin-bottom: 43px;
transition: transform 1s;
}
.form-label {
font-family: 'Yanone Kaffeesatz', Arial, sans-serif;
color: #ccc;
font-size: 18px;
}
.form-input:focus + .form-label, .form-input:valid + .input-label, .form-input:valid + .dropdown-label {
transform: translateY(-38px);
font-family: 'Yanone Kaffeesatz', Arial, sans-serif;
color: #00bafa;
font-size: 18px;
}
.form-textarea {
box-sizing: border-box;
width: 100%;
max-width: 400px;
height: 150px;
margin: 15px 0 15px 0;
padding: 15px 10px 0px 10px;
border: 3px solid #ccc;
border-radius: 8px;
outline: none;
resize: none;
text-align: left;
-webkit-appearance: none;
background: transparent;
font-family: 'Yanone Kaffeesatz', Arial, sans-serif;
color: #000;
background: #fff;
font-size: 18px;
-webkit-transition: border-color ease-in-out .5s,box-shadow ease-in-out .5s;
transition: border-color ease-in-out .5s,box-shadow ease-in-out .5s;
}
.form-textarea:focus {
border-color: #00bafa;
}
.form-textarea + .form-label {
display: block;
pointer-events: none;
margin: 0 0 0 15px;
line-height: 0px;
margin-top: -145px;
margin-bottom: 145px;
transition: transform 1s;
}
.form-textarea:focus + .form-label, .form-textarea:valid + .textarea-label {
transform: translateY(-38px);
font-family: 'Yanone Kaffeesatz', Arial, sans-serif;
color: #00bafa;
font-size: 18px;
}
.form-textarea + .form-label:before {
transform: translateY(-38px);
pointer-events: none;
font-family: 'Yanone Kaffeesatz', Arial, sans-serif;
color: #00bafa;
font-size: 18px;
}
<fieldset class="form-field">
<textarea maxlength="280" class="form-textarea" required></textarea>
<label class="form-label textarea-label" alt='Search Map' placeholder='Search Map'>Text Area</label>
<br>
<input type='text' class="form-input" placeholder="" onclick='this.value = "";' required>
<label class="form-label input-label" alt='Search Map' placeholder='Search Map'>Text</label>
<br>
<select class="form-input" required>
<option value="" disabled selected hidden></option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="DC">District Of Columbia</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select>
<label class="form-label dropdown-label" for="name">Select</label>
</fieldset>
这是一个快速而肮脏的edit on your fiddle
<fieldset class="form-field">
<input type='text' class="form-input" placeholder="" required onclick='this.value = "";'>
<label class="form-label" alt='Search Map' placeholder='Search Map'>Text</label>
</fieldset>
而对于CSS:
.form-input:focus + .form-label,
.form-input:valid + .form-label {
transform: translateY(-38px);
font-family: 'Yanone Kaffeesatz', Arial, sans-serif;
color: #00bafa;
font-size: 18px;
}
基本上,通过添加:valid
到你的领域,利用:focus
与required
重合