Expanding on Pure CSS: Center Tooltip Above Text On Hover -- how does one make the tooltip hover centered relative to its container if the tooltip is wider than said
Here's the simplest solution using CSS3 transform.
JSfiddle Demo
.drag-hint {
position: relative;
margin: 50px;
display: inline-block;
padding: 0 1em;
border: 1px solid red;
/* for visual reference */
}
.drag-hint > span {
background: black;
color: white;
white-space: nowrap;
display: none;
}
.drag-hint:hover > span {
display: inline-block;
position: absolute;
top: -25px;
left: 50%;
transform: translateX(-50%);
text-align: center;
}
<span class="drag-hint">
<span>drag drag drag drag drag</span>
Hover me</span>
<span class="drag-hint">
<span>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</span>
Hover me</span>
Try with modified below code in your code.
.drag-hint:hover > span {
display: inline;
position:absolute;
top: -25px;
left: 0;
right: 0;
text-align: center;
width:200px;
}
I achieve your question.
<h1>This:</h1>
<div id="test" class="drag-hint"><p>drag drag drag drag drag</p>Hover me</div>
<h1>Should appear as:</h1>
<img src="http://s22.postimg.org/h1qk194bh/Untitled_2.jpg">
.drag-hint {
position:relative;
margin: 50px;
}
.drag-hint > p {
background: black;
color: white;
width: 100%;
display: none;
}
.drag-hint:hover > p {
display: inline;
position:absolute;
top: -40px;
left: 0;
right: 0;
text-align: center;
}
JS Bin
.drag-hint:hover > span {
display: table;
position: absolute;
top: -25px;
left: -51px;
right: 0;
text-align: center;
padding: 2px;
}
Use div instead of span
<div id="test" class="drag-hint"><div>drag drag drag drag drag</div>Hover me</div>
a fiddle here
I hope this will solve your problem. http://jsbin.com/xihupeyowize/1/