Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pure CSS: Center Tooltip Above Text On Hover pt. 2

Tags:

css

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 container?

http://jsbin.com/idudal/24/edit

Ie. this (where drag drag drag gets cut off):

enter image description here

Should appear as:

enter image description here

HTML:

<span id="test" class="drag-hint"><span>drag drag drag drag drag</span>Hover me</span>

CSS:

.drag-hint {
  position:relative;
  margin: 50px;
}
.drag-hint > span {
  background: black;
  color: white;
  white-space: nowrap;
  display:none;
}
.drag-hint:hover > span {
  display: inline;
  position:absolute;
  top: -25px;
  left: 0;
  right: 0;
  text-align: center;
}
like image 209
Mark Boulder Avatar asked Oct 10 '14 05:10

Mark Boulder


People also ask

How do I change the tooltip position in CSS?

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 ). Note: See examples below on how to position the tooltip. The tooltiptext class holds the actual tooltip text.

How do I center align tooltip?

The solution is to use HTML '<table>' format for tooltip with 'text-align: center'.

What is the text called when you hover over an icon?

The tooltip, also known as infotip or hint, is a common graphical user interface (GUI) element in which, when hovering over a screen element or component, a text box displays information about that element, such as a description of a button's function, what an abbreviation stands for, or the exact absolute time stamp ...

What is tooltip on hover?

Tooltip is a concept used in HTML for showing some extra information about the specifically selected element. This can be done on the mouse hover effect whenever the user moves the mouse over an element that is using a tooltip to display specified information about that element.


1 Answers

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>
like image 71
Paulie_D Avatar answered Nov 12 '22 03:11

Paulie_D