我有一个输入和一个div,其高度和宽度等于输入的边框底部。我只想展示首先隐藏的div。
.cool{
font-size: 30px;
margin: auto;
outline: none;
padding-bottom: 0.2em;
border-width: 0px 0px 2px 0px;
border-bottom-color: black;
}
div.bar{
width: 365.3px;
height: 2px;
background-color: #42c0fb;
position: relative;
top: -2px;
display: none;
}
input:focus .bar{
display: block
}

<div style = "margin: auto;">
<input class = "cool" type = "text" />
<div class = "bar"></div>
</div>
&#13;
请帮忙。上面的代码不起作用。
答案 0 :(得分:2)
当您使用input:focus .bar
选择器时,浏览器会在.bar
的后代下搜索input
。鉴于.bar
是input
的兄弟姐妹。
您可以在此处使用其中一个兄弟选择器。例如:input:focus + .bar
或input:focus ~ .bar
.cool {
font-size: 30px;
margin: auto;
outline: none;
padding-bottom: 0.2em;
border-width: 0px 0px 2px 0px;
border-bottom-color: black;
}
div.bar {
width: 365.3px;
height: 2px;
background-color: #42c0fb;
position: relative;
top: -2px;
display: none;
}
input:focus + .bar {
display: block
}
&#13;
<div style="margin: auto;">
<input class="cool" type="text" />
<div class="bar"> Hello</div>
</div>
&#13;
答案 1 :(得分:1)
为此使用同级选择器+
。检查下面更新的代码段。
.cool{
font-size: 30px;
margin: auto;
outline: none;
padding-bottom: 0.2em;
border-width: 0px 0px 2px 0px;
border-bottom-color: black;
}
div.bar{
width: 365.3px;
height: 2px;
background-color: #42c0fb;
position: relative;
top: -2px;
display: none;
}
input:focus + .bar{
display: block
}
&#13;
<div style = "margin: auto;">
<input class = "cool" type = "text" />
<div class = "bar">bar text</div>
</div>
&#13;
答案 2 :(得分:1)
试试这个
/* form starting stylings ------------------------------- */
.group {
position:relative;
margin-bottom:45px;
}
input {
font-size:18px;
padding:10px 10px 10px 5px;
display:block;
width:300px;
border:none;
border-bottom:1px solid #757575;
}
input:focus { outline:none; }
/* active state */
input:focus ~ label, input:valid ~ label {
top:-20px;
font-size:14px;
color:#5264AE;
}
/* BOTTOM BARS ================================= */
.bar { position:relative; display:block; width:300px; }
.bar:before, .bar:after {
content:'';
height:2px;
width:0;
bottom:1px;
position:absolute;
background:#42c0fb;
transition:0.2s ease all;
-moz-transition:0.2s ease all;
-webkit-transition:0.2s ease all;
}
.bar:before {
left:50%;
}
.bar:after {
right:50%;
}
/* active state */
input:focus ~ .bar:before, input:focus ~ .bar:after {
width:50%;
}
/* HIGHLIGHTER ================================== */
.highlight {
position:absolute;
height:60%;
width:100px;
top:25%;
left:0;
pointer-events:none;
opacity:0.5;
}
/* active state */
input:focus ~ .highlight {
-webkit-animation:inputHighlighter 0.3s ease;
-moz-animation:inputHighlighter 0.3s ease;
animation:inputHighlighter 0.3s ease;
}
/* ANIMATIONS ================ */
@-webkit-keyframes inputHighlighter {
from { background:#5264AE; }
to { width:0; background:transparent; }
}
@-moz-keyframes inputHighlighter {
from { background:#5264AE; }
to { width:0; background:transparent; }
}
@keyframes inputHighlighter {
from { background:#5264AE; }
to { width:0; background:transparent; }
}
<div class="group">
<input type="text" required>
<span class="highlight"></span>
<span class="bar"></span>
</div>
答案 3 :(得分:0)
.cool{
font-size: 30px;
margin: auto;
outline: none;
padding-bottom: 0.2em;
border-width: 0px 0px 2px 0px;
border-bottom-color: black;
}
div.bar{
width: 365.3px;
height: 2px;
background-color: #42c0fb;
position: relative;
top: -2px;
display: none;
}
input:focus + .bar{
display: block
}
<div style = "margin: auto;">
<input class = "cool" type = "text" />
<div class = "bar"></div>
</div>
答案 4 :(得分:0)
在这种情况下,兄弟选择器将适合您。
但您也可以使用:focus-within
.cool {
font-size: 30px;
margin: auto;
outline: none;
padding-bottom: 0.2em;
border-width: 0px 0px 2px 0px;
border-bottom-color: black;
}
div.bar {
width: 365.3px;
height: 2px;
background-color: #42c0fb;
position: relative;
top: -2px;
display: none;
}
div:focus-within .bar {
display: block
}
<div style="margin: auto;">
<input class="cool" type="text" />
<div class="bar"></div>
</div>