我试图相对于其相对定位的祖父母定位元素的绝对位置::before
。它的父级没有定位,但是top
和left
尊重父级而不是祖父母。
* {
box-sizing: border-box;
}
::-webkit-input-placeholder,
::-moz-placeholder {
color: #9b9b9b;
}
.field {
height: 43px;
display: flex;
flex-direction: column-reverse;
position: relative;
}
input,
label {
display: block;
}
input {
font-size: 14px;
border: 0;
border-bottom: 1px solid #F2F2F2;
border-radius: 0;
padding: 3px 0 10px 0;
background-color: transparent;
}
input:focus {
outline: 0;
border-bottom: 1px solid #D66C9C;
}
label {
font-size: 12px;
color: #9b9b9b;
transform: translateY(20px);
transition: all 0.2s;
}
input:focus + label {
color: #808080;
transform: translateY(0);
}
input:focus + label::before {
content: "";
width: 5px;
height: 5px;
display: block;
background-color: #d66c9c;
transform: rotate(45deg) translateY(-50%);
position: absolute;
left: 0;
bottom: 0;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div class="field">
<input type="text" id="fullname">
<label for="fullname">Name</label>
</div>
</body>
</html>
答案 0 :(得分:-1)
* {
box-sizing: border-box;
}
::-webkit-input-placeholder,
::-moz-placeholder {
color: #9b9b9b;
}
.field {
height: 43px;
display: flex;
flex-direction: column-reverse;
position: relative;
}
input,
label {
display: block;
}
input {
font-size: 14px;
border: 0;
border-bottom: 1px solid #F2F2F2;
border-radius: 0;
padding: 3px 0 10px 0;
background-color: transparent;
}
input:focus {
outline: 0;
border-bottom: 1px solid #D66C9C;
}
label {
font-size: 12px;
color: #9b9b9b;
transform: translateY(20px);
transition: all 0.2s;
position:relative;
}
input:focus + label {
color: #808080;
transform: translateY(0);padding:0 0 0 5px;
}
input:focus + label::before {
content: "";
width: 5px;
height: 5px;
display: block;
background-color: #d66c9c;
transform: rotate(45deg) translateY(-50%);
position: absolute;
left: -8px;
bottom:5px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div class="field">
<input type="text" id="fullname">
<label for="fullname">Name</label>
</div>
</body>
</html>
检查这个希望,这样对您有用