0

HTML:

<div style="width:200px;height:200px;background:yellow;position:relative">
    <span class="hover-content">
        <span>+</span>
    </span>    
</div>

CSS:

.hover-content{position:absolute;display:none}
div:hover .hover-content{display:block}

The div's width and height are dynamic, being changed by javascript-events. My goal is to make the + sign appear at the center of the div, no matter how it changes. I wonder if this can be done by mere CSS.

JSfiddle:http://jsfiddle.net/1pwww691/

shenkwen
  • 3,536
  • 5
  • 45
  • 85
  • Doesn't this (http://stackoverflow.com/questions/114543/horizontally-center-a-div-in-a-div) answer your question ? – merours Nov 19 '14 at 16:18

2 Answers2

2

By making the hover-content element display as a table, and the span inside as a table-cell, you'll get the result you want:

.hover-content {
  display: table;
  width: 100%;
  height: 100%;
}

.hover-content span {
  display: table-cell;
  vertical-align: middle;
  text-align: center;
}

Here's the live example: http://codepen.io/TheDutchCoder/pen/GggWqP

Reinier Kaper
  • 1,079
  • 1
  • 8
  • 17
1

Here's the standard way for IE8+

.hover-content {
    position:absolute;
    display:none;
    top:50%;
    left:50%;
    transform:translate(-50%, -50%);
}
div:hover .hover-content {
    display:block
}
<div style="width:200px;height:200px;background:yellow;position:relative"> <span class="hover-content">
        <span>+</span>
</span>
</div>
Paulie_D
  • 107,962
  • 13
  • 142
  • 161