0

I have a text which on hover displays another (otherwise hidden) text elsewhere, like this:

HTML

<div class="design">hover me<span>to show this</span></div>

CSS

.design {
    position: absolute;
    z-index: 2;
    color: #404040;
}

.design:hover {
    cursor: pointer;
}

.design span {
    position: absolute;
    display: none;
}

.design:hover span {
    display: block;
    top: 47px;
    left: 45px;
    font-family: Arial;
    font-size: 13px;
    color: #000;
}

How can apply transition effects to the text which is hidden or shown depending on the hover? Apparently transition conflicts with display:none and :block, but using visibility doesn't seem to work either.

edit: jsFiddle - http://jsfiddle.net/vwsgJ/

lapingultah
  • 196
  • 4
  • 17

1 Answers1

2

First method

I have managed to achieve what you wanted using opacity, according to (I think) MDN (Mozilla Developer Network) you can't transition display. I also rearranged your css slightly:

http://codepen.io/hwg/pen/xFDIl - Working Code.

CSS:

.design span {
position: absolute;
opacity:0;
transition: opacity 1s;

display: block;
top: 47px;
left: 45px;
font-family: Arial;
font-size: 13px;
color: #000;

width:0;
height:0;
margin-left:-1000px;

}

.design:hover span {

opacity:1;
/*transition: all 1s;*/

width:30px;
height:auto;
margin-left:0;
}

Note that codepen.io uses prefix-free, see http://caniuse.com/css-transitions for browser support, prefixes may have to be added.

Edit- to avoid the OP's problem below, add: width:0; height:0; to the un-hovered element, and a pixel width/height such as width:30px; height:auto; on hover. (See example) There is if you hover over precisely that area you see the hover effect, but in practice this would be very rare in my view, also this can be solved by the use of margin left if needed.

Note: this stops the transition on mouse out.

New Method

To avoid the problem mentioned above, the property pointer-events:none can be added to the span. This removes the need to have any negative margins, or custom widths or heights.

See http://codepen.io/hwg/pen/BojpK

However, this is not supported in IE-anything, see css 'pointer-events' property alternative for IE for maybe more help.

Otherwise I don't think the OP's aim can be completely met.

Community
  • 1
  • 1
harley_woop
  • 1,739
  • 4
  • 15
  • 28