我想把标签放在输入的顶部。但是当我在代码中移动它时,CSS Focus样式停止工作。在焦点上,标签需求变得更大。如何解决这个问题?尝试了一切...
form {
margin-top: 25px;
}
input {
display:block;
}
form input:focus + label {
font-size: 1.5em;
}

<form>
<!-- DOESN'T WORK
=====================
-->
<label for="firstname">First name:</label>
<input id="firstname" name="firstname" type="text">
<br>
<!-- WORKS
=====================
-->
<input id="lasttname" name="firstname" type="text">
<label for="lasttname">Last name:</label>
</form>
&#13;
答案 0 :(得分:2)
jQuery解决方案。
$(function() {
$('form input[type="text"]').focus(function() {
$(this).prev('label').css("font-size", "1.5em");
});
$('form input[type="text"]').focusout(function() {
$(this).prev('label').css("font-size", "1em");
});
});
form {
margin-top: 25px;
}
form input:focus + label {
font-size: 1.5em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<!-- DOESN'T WORK
=====================
-->
<label for="firstname">First name:</label>
<input id="firstname" name="firstname" type="text">
<!-- WORKS
=====================
-->
<input id="lasttname" name="firstname" type="text">
<label for="lasttname">Last name:</label>
</form>
答案 1 :(得分:2)
尝试在额外div
中包装标签和输入,并使用flex-box
:
form {
margin-top: 25px;
}
form input:focus + label {
font-size: 1.5em;
}
.form-row {
display: flex;
flex-direction: column;
margin-bottom: 8px;
}
.form-row label {
order: 1;
}
.form-row input {
order: 2;
width: 141px;
}
&#13;
<form>
<!-- DOESN'T WORK
=====================
-->
<div class='form-row'>
<input id="firstname" name="firstname" type="text">
<label for="firstname">First name:</label>
</div>
<!-- WORKS
=====================
-->
<input id="lasttname" name="firstname" type="text">
<label for="lasttname">Last name:</label>
</form>
&#13;