How to center a tooltip horizontally on top of a link
Asked Answered
H

4

9

I'm trying to get a tooltip box to be inserted on top and centered of an a tag. I have this working for small Text, but I can't seem to get it to center on large text.

I understand it has to do with positioning the box and the arrow but I can't seem to find the right combination to get the arrow centered under the box and the box centered above the a tag.

I'm hoping to accomplish this with just CSS alone.

HTML:

<div> 
    <a class="tooltip" href="#" title="tooltip">
        <span>Text</span>    
    </a>

</div>
<div> <a class="tooltip" href="#" title="tooltip">
        <span>TextTextText</span>    
    </a>

</div>

CSS:

div {
    padding: 150px;
    display: inline-block;
    background-color: red;
}
.tooltip{
    display: inline;
    position: relative;
}
.tooltip:hover:after{
    background: #333;
    background: rgba(0,0,0,.8);
    border-radius: 5px;
    bottom: 26px;
    color: #fff;
    content: attr(title);
    padding: 10px;
    left: -50%;
    position: absolute;
    z-index: 98;
}
.tooltip:hover:before{
    border: solid;
    border-color: #333 transparent;
    border-width: 6px 6px 0 6px;
    bottom: 20px;
    content: "";
    left: 30%;
    position: absolute;
    z-index: 99;
}

And here is the fiddle.

I really would appreciate any help I can get.

Hindustani answered 11/2, 2014 at 18:7 Comment(4)
What's #333 transparent?Mena
@Mena This is two-value syntax border-color: horizontal vertical;Phytography
Oh. Who needs a transparent border?Mena
to build the arrow for around the tooltipHindustani
P
42

If the tooltip doesn't have an explicit width, you could align that horizontally center, by using a negative translateX as follows:

.tooltip:hover:after {
    left: 50%;
    transform: translateX(-50%);
    /* other styles ... */
}

.tooltip:hover:before {
    left: 50%;
    transform: translateX(-50%);
    /* other styles ... */
}

div {
  padding: 150px;
  display: inline-block;
  background-color: red;
}

.tooltip {
  display: inline;
  position: relative;
}

.tooltip:hover:after {
  background: #333;
  background: rgba(0, 0, 0, .8);
  border-radius: 5px;
  bottom: 26px;
  color: #fff;
  content: attr(title);
  text-decoration: none;
  padding: 10px;
  left: 50%;
  transform: translateX(-50%);
  position: absolute;
}

.tooltip:hover:before {
  border: solid;
  border-color: #333 transparent;
  border-width: 6px 6px 0 6px;
  bottom: 20px;
  content: "";
  left: 50%;
  transform: translateX(-50%);
  position: absolute;
}
<div>
  <span class="tooltip" title="tooltip">
    <a href="#">Work</a>    
  </span>
</div>
<div>
  <span class="tooltip" title="tooltip">
    <a href="#">Doesn't Work as hoped</a>    
  </span>
</div>
Phytography answered 11/2, 2014 at 18:16 Comment(2)
only thing missed was the -ms-transform: translateX(-50%); for IEHindustani
@Hindustani Yes you're right, and -o- for Opera 10.5-12.10 :)Phytography
C
-1

You might find the following link helpful. I think this will do exactly what you need:

http://www.webdesignerdepot.com/2012/11/how-to-create-a-simple-css3-tooltip/

They have a working example here:

http://netdna.webdesignerdepot.com/uploads7/how-to-create-a-simple-css3-tooltip/tooltip_demo.html

Cowhide answered 11/2, 2014 at 18:36 Comment(1)
That's actually where I got some of my css from, they just didn't center the popup correctlyHindustani
L
-1

I think you need tutorial to view Create CSS3 Tooltip and the main code is

<a title="Create Simple Tooltip Using CSS3" class="tooltip">Some Sample CSS3 Tooltip</a>

.tooltip
{
 display: inline;
 position: relative;
}
.tooltip:hover:after
{
 background: #333;
 background: rgba(0,0,0,.8);
 border-radius: 5px;
 bottom: 26px;
 color: #fff;
 content: attr(title);
 left: 20%;
 padding: 5px 15px;
 position: absolute;
 z-index: 98;
 width: 220px;
}

.tooltip:hover:before
{
 border: solid;
 border-color: #333 transparent;
 border-width: 6px 6px 0 6px;
 bottom: 20px;
 content: "";
 left: 50%;
 position: absolute;
 z-index: 99;
}
Lexy answered 8/6, 2016 at 7:33 Comment(0)
V
-2

This is what I can think of now(at 1am :) ). works well with all browsers.

div {
padding: 150px;
display: inline-block;
background-color: red;
text-align: center;
}
.tooltip {
display: inline;
position: relative;
}
.tooltip:hover:after {
background: #333;
background: rgba(0, 0, 0, .8);
border-radius: 5px;
bottom: 26px;
color: #fff;
content: attr(title);
padding: 10px;
left: 50%;
margin-left: -50px;
position: absolute;
z-index: 98;
width: 100px;
text-align: center;
}
.tooltip:hover:before {
border: solid;
border-color: #333 transparent;
border-width: 6px 6px 0 6px;
bottom: 20px;
right: 50%;
margin-right: -15px;
content:"";
position: absolute;
z-index: 99;
}

Regards, Ashok

Vandalism answered 11/2, 2014 at 19:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.