代码笔:链接
.body {
position: relative;
font-family: 'Roboto', 'Helvetica', 'Arial', sans-serif;
}
.wrap {
position: absolute;
right: 0;
top: 40%;
width: 350px;
left: 0;
margin: 0 auto;
}
/* select starting stylings ------------------------------*/
.select {
font-family: 'Roboto', 'Helvetica', 'Arial', sans-serif;
position: relative;
width: 350px;
}
.select-text {
position: relative;
font-family: inherit;
background-color: transparent;
width: 350px;
padding: 10px 10px 10px 0;
font-size: 18px;
border-radius: 0;
border: none;
border-bottom: 1px solid rgba(0, 0, 0, 0.12);
}
/* Remove focus */
.select-text:focus {
outline: none;
border-bottom: 1px solid rgba(0, 0, 0, 0);
}
/* Use custom arrow */
.select .select-text {
appearance: none;
-webkit-appearance: none
}
.select:after {
position: absolute;
top: 18px;
right: 10px;
/* Styling the down arrow */
width: 0;
height: 0;
padding: 0;
content: '';
border-left: 6px solid transparent;
border-right: 6px solid transparent;
border-top: 6px solid rgba(0, 0, 0, 0.12);
pointer-events: none;
}
/* LABEL ======================================= */
.select-label {
color: rgba(0, 0, 0, 0.26);
font-size: 18px;
font-weight: normal;
position: absolute;
pointer-events: none;
left: 0;
top: 10px;
transition: 0.2s ease all;
}
/* active state */
.select-text:focus~.select-label,
.select-text:valid~.select-label {
color: #2F80ED;
top: -20px;
transition: 0.2s ease all;
font-size: 14px;
}
/* BOTTOM BARS ================================= */
.select-bar {
position: relative;
display: block;
width: 350px;
}
.select-bar:before,
.select-bar:after {
content: '';
height: 2px;
width: 0;
bottom: 1px;
position: absolute;
background: #2F80ED;
transition: 0.2s ease all;
}
.select-bar:before {
left: 50%;
}
.select-bar:after {
right: 50%;
}
/* active state */
.select-text:focus~.select-bar:before,
.select-text:focus~.select-bar:after {
width: 50%;
}
/* HIGHLIGHTER ================================== */
.select-highlight {
position: absolute;
height: 60%;
width: 100px;
top: 25%;
left: 0;
pointer-events: none;
opacity: 0.5;
}
<html>
<head>
</head>
<body>
<div class="wrap">
<!--Select with pure css-->
<div class="select">
<select class="select-text" required>
<option value="" disabled selected></option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<span class="select-highlight"></span>
<span class="select-bar"></span>
<label class="select-label">Select</label>
</div>
<!--Select with pure css-->
</div>
</body>
</html>
当<select>
isrequired
时,它工作正常。
但问题是,当我删除时"required"
,它是 Label Floating 没有选择的选项。
在这种情况下如何维护浮动标签?
我错过了什么?还是我们需要 JavaScript?
所以这个想法不是在标签为空时浮动标签。
我input
使用链接解决了这个问题