I was trying to generate a screen in which utilises the :before
and :after
pseudo elements, but I'm wondering if such functionality is actually possible.
I have a wrapper div which is wrapped around an input (allowing for pseudo element
s on this wrapper).
something like:
+-----------------------------------+
| +-------------------------------+ |
| | | | <-- wrapper div
| +-------------------------------+ <-- input element
+-----------------------------------+
However, I was looking to have a pseudo element positioned after the div.
+-----------------------------------++-------+
| +-------------------------------+ | |¯¯¯| |
| | | | / |
| +-------------------------------+ | ! |<--pseudo element
+-----------------------------------++-------+
I was wanting to be able to hover this pseudo element, and have the other pseudo element appear.
.wrap {
position: relative;
height: 30px;
width: 200px;
display: inline-block;
}
.wrap input {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
}
.wrap:after {
content: "?";
position: absolute;
top: 0;
left: 100%;
height: 30px;
width: 30px;
font-size: 30px;
text-align: center;
}
.wrap:before {
content: "";
position: absolute;
top: 100%;
left: 0;
height: 60px;
width: 100%;
background: tomato;
opacity:0.2;
}
<div class="wrap">
<input placeholder="input element" type="text" />
</div>
From the above snippet design, is there a way of making the :before
element change its opacity when i hover only the :after
element, and not the wrap
div itself (note: html cannot be changed, hence why this question)?
I tried using something like:
.wrap:not(input):hover:before{
after changing the width of the input to 90%
, but this didn't make a difference
I know it's not exactly what you asked for but until css can select parents (it's comming) you could just add one more html element:
<div class="wrap">
<div class="inner_wrap">
<input placeholder="input element" type="text" />
</div>
</div>
css:
.wrap {
position: relative;
height: 30px;
width: 200px;
display: inline-block;
}
.wrap input {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
}
.wrap:after {
content: "?";
position: absolute;
top: 0;
left: 100%;
height: 30px;
width: 30px;
font-size: 30px;
text-align: center;
}
.inner_wrap:before {
content: "";
position: absolute;
top: 100%;
left: 0;
height: 60px;
width: 100%;
background: tomato;
opacity:0.2;
display:none;
}
.wrap:hover .inner_wrap:before{
display:block;
}
.wrap .inner_wrap:hover:before{
display:none;
}
It seems the because pseudo elements are not 'real' elements, it means that the (currently) cannot be used in this way. Instead, using a 'real' element would allow for this, and so I have chosen to use a span element until this feature may or may not be implemented.
The current implementation displays:
input {
position: relative;
display: inline-block;
height: 30px;
vertical-align: top;
}
span {
position: relative;
height: 30px;
width: 30px;
display: inline-block;
text-align: center;
border-radius: 50%;
font-size: 25px;
line-height: 30px;
background: tomato;
}
span:after {
content: "A Question Mark";
position: relative;
display: inline-block;
top: 0;
left: 0;
height: 60px;
width: 100px;
background: tomato;
opacity: 0;
transition: all 0.8s;
font-size: 16px;
}
span:hover:after {
opacity: 1;
}
<input placeholder="input element" type="text" />
<span>?</span>
Much to the disappointment of my beloved pseudo element design.
来源:https://stackoverflow.com/questions/29601511/on-hovering-one-pseudo-element-make-the-other-pseudo-element-appear