Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS3 "Tooltip" with :hover:after positioning and size

Tags:

css

tooltip

I know it is possible to create a custom "tooltip" with the :hover:after selectors and to align this tooltip relative to the original element by marking the original element as position:relative and the tooltip as absolute.

HTML:

test
<span custom-tooltip="testing a custom tooltip" class="tooltip">
    test
</span>
test

CSS:

.tooltip {
    position: relative;
}

.tooltip:hover:after {
    content: attr(custom-tooltip);
    position: absolute;
    background: black;
    color: white;
}

However, I must use absolute values to position or size this :after element

top: 30px;
left: -30px;
width: 300px;

What if I want to make the element as wide as it needs to be (Percentages are relative to the parent element creating a large vertical box so I can't tell it to go width: 100%)

And centered under the parent (left: -50% results in it being moved 50% of the parent to the left, not centered)

Is this possible without javascript? (If not, are there some magic selectors or functions to get the width of this or that and calc() the correct values?

like image 718
J V Avatar asked Nov 21 '11 01:11

J V


People also ask

How do I change the position of a tooltip in CSS?

Basic Tooltip The tooltip text is placed inside an inline element (like <span>) with class="tooltiptext" . CSS: The tooltip class use position:relative , which is needed to position the tooltip text ( position:absolute ).

How do I make hover tooltip?

To make a simple tooltip, we'll first create the HTML element that triggers the tooltip when hovered over. We'll create this element as a div and assign it the class hover-text. Next, we'll create the element for the tooltip itself. This will be a span element with the class tooltip-text.

How do I show the tooltip on the right side?

Here is how to do it with position: absolute and relative . On the container, set position: relative , and display: table (shrink to fit). For the position: absolute tooltip, set top: 0 , and left: 100% (moves to the right edge of the relative container).


1 Answers

You can force the tooltip onto a single line by using white-space:nowrap. I don't know of any way to center the tooltip without forcing a specific width on both the tooltip and the item the tooltip applies to. Here's a general-purpose example (without centering):

<p>Lorem <span tooltip="Lorem ipsum">ipsum</span> dolor sit amet.</p>

And the CSS:

*[tooltip] {
  position: relative;
  border-bottom: dotted 1px #000;
}
*[tooltip]:hover:before {
  content: attr(tooltip);
  background-color: #000;
  color: #fff;
  top: 1em;
  position: absolute;
  white-space: nowrap;
}

Note that I'm using :before instead of :after. If you want to center the tooltip and are able to define a fixed width, you can use this CSS instead:

*[tooltip] {
  position: relative;
  display: inline-block;
  text-align: center;
  width: 200px;
  margin: 0 -75px;
}
*[tooltip]:hover:before {
  content: attr(tooltip);
  background-color: #000;
  color: #fff;
  top: 1em;
  position: absolute;
  white-space: nowrap;
  width: 200px;
}

Here, the item is given a fixed width equal to the width of the tooltip then negative left/right margins to collapse it back down to the desired size. Note the addition of display:inline-block and text-align:center.

This technique isn't practical for inline tooltips, but works well for buttons and "call to action" links.

like image 64
Brandon Gano Avatar answered Sep 21 '22 13:09

Brandon Gano