2

Goal:

Create a simple text-box element with remove icon with css3 only. The icon should change his color to blue on hover.

Progress:

It's pretty easy to achieve my wish with a little html and css code.

.ui-textbox {  
    padding: 0 10px 0 0;             
    position: relative;
}
.ui-textbox span {
    margin: 0 10px;
}
.ui-textbox:hover:after {
    content:"x";
    position: absolute;
    top: 0;    
    font-size: 16px;
    right: 20px;
}
<label class="ui-textbox">
    <span>Some label</span>
    <input type="text" />
</label>

My Problem:

Can't find out how to catch the hover event on the after element so I could change his color.

Something like:

.ui-textbox:hover:after:hover {
    color: blue;
}
Alex Char
  • 32,879
  • 9
  • 49
  • 70
Dvir
  • 3,287
  • 1
  • 21
  • 33

3 Answers3

4

You can not "catch" :hover on ::after pseudo-elements. I suggest the following using an extra element(like span):

.ui-textbox {
  padding: 0 10px 0 0;
  position: relative;
}
.ui-textbox span {
  margin: 0 10px;
}
.ui-textbox:hover span.cross {
  position: absolute;
  top: 0;
  font-size: 16px;
  right: 20px;
  visibility: visible;
}
.ui-textbox span.cross {
  visibility: hidden;
}
.ui-textbox:hover span.cross:hover {
  color: red;
}
<label class="ui-textbox">
  <span>Some label</span>
  <input type="text" />
  <span class="cross">x</span>
</label>
Alex Char
  • 32,879
  • 9
  • 49
  • 70
0

.ui-textbox {  
    padding: 0 10px 0 0;             
    position: relative;
}
.ui-textbox span {
    margin: 0 10px;
}
.ui-textbox:hover:after {
    content:"x";
    position: absolute;
    top: 0;    
    font-size: 16px;
    right: 20px;
}
.ui-textbox:hover:after {
    color: red;
}
<label class="ui-textbox">
    <span>Some label</span>
    <input type="text" />
</label>

http://jsfiddle.net/konsoetw/

user229044
  • 232,980
  • 40
  • 330
  • 338
Madalina Taina
  • 1,968
  • 20
  • 26
0

Maybe this is closer to what you want (just css): http://jsfiddle.net/konsoetw/3/

    .ui-textbox {  
        padding: 0 10px 0 0;             
        position: relative;
    }
    .ui-textbox span {
        margin: 0 10px;
    }
    .ui-textbox:after {
        content:"x";
        position: absolute;
        top: 0;    
        font-size: 16px;
        right: 20px;
        color:blue;
    }
    .ui-textbox:hover:before {
        color: red;
        content:"x";
        position: absolute;
        top: 0;    
        font-size: 16px;
        right: 20px;
        z-index:10;
    }

 <label class="ui-textbox">
        <span>Some label</span>
        <input type="text" />
 </label>
Madalina Taina
  • 1,968
  • 20
  • 26