Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On hovering one pseudo element, make the other pseudo element appear?

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 elements 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

like image 703
jbutler483 Avatar asked Apr 13 '15 09:04

jbutler483


People also ask

Can you use hover on pseudo element?

You can't apply a hover to just the pseudo element.

What does the pseudo-class hover do?

A CSS pseudo-class is a keyword added to a selector that specifies a special state of the selected element(s). For example, :hover can be used to change a button's color when the user's pointer hovers over it.

Can you have multiple pseudo-elements?

1, an element can only have at most one of any kind of pseudo-element at any time. (This means an element can have both a :before and an :after pseudo-element — it just cannot have more than one of each kind.)

Can pseudo-elements and pseudo-classes be combined?

Combining pseudo-classes and pseudo-elementsIf you wanted to make the first line of the first paragraph bold you could chain the :first-child and ::first-line selectors together. Try editing the previous live example so it uses the following CSS.


2 Answers

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;
}

http://fiddle.jshell.net/0vwn1w9t/

like image 121
Victor Radu Avatar answered Oct 02 '22 06:10

Victor Radu


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.

like image 28
jbutler483 Avatar answered Oct 02 '22 06:10

jbutler483