Search code examples
htmlcssmultiple-columnscss-multicolumn-layout

Absolutely positioned tooltip over Multi-column Layout


I'm trying to add absolutely positioned tooltips to a simple CSS three column layout - but the tooltip always gets cut off by subsequent columns.

The tooltip also breaks across columns if it runs above or below the upper or lower limits of the wrapper element.

Is there any way to ensure that the tooltip is always on top of the columns - and also that it never breaks across columns and is allowed to run outside the bounds of the wrapper element?

Edit - I should clarify that the list will be of unknown/variable length, and I'd prefer to avoid having to rely on Javascript to format the list (e.g. by splitting it in 3 and wrapping each section) if possible. The list should also wrap vertically, as in the snippet, and not horizontally across columns.

.wrapper {
  display: block;
  width: 50%;
  background-color: white;
  border: 2px solid red;
  -webkit-column-count: 3;
  -moz-column-count: 3;
  column-count: 3;
  column-gap: 2em;
  -webkit-column-gap: 2em;
  -moz-column-gap: 2em;
  -webkit-column-rule: 2px solid red;
  -moz-column-rule: 2px solid red;
  column-rule: 2px solid red;
  margin-bottom: 5em;
}

.wrapper span {
  position: relative;
  display: inline-block;
  width: 100%;
  -webkit-column-break-inside: avoid;
  page-break-inside: avoid;
  break-inside: avoid;
  padding: 1em;
  cursor: pointer;
  transition: 0.2s color linear
}

.wrapper span:hover {
  color: red;
}

.wrapper span:hover .tooltip {
  display: block;
}

.tooltip {
  display: none;
  position: absolute;
  width: 100%;
  top: 0;
  left: 50%;
  padding: 1em;
  border: 2px solid black;
  color: white;
  background-color: lightgray;
}
<div class="wrapper">
  <span>item 1 <p class="tooltip">Tooltip!</p></span>
  <span>item 2 <p class="tooltip">Tooltip!</p></span>
  <span>item 3 <p class="tooltip">Tooltip!</p></span>
  <span>item 4 <p class="tooltip">Tooltip!</p></span>
  <span>item 5 <p class="tooltip">Tooltip!</p></span>
  <span>item 6 <p class="tooltip">Tooltip!</p></span>
  <span>item 7 <p class="tooltip">Tooltip!</p></span>
  <span>item 8 <p class="tooltip">Tooltip!</p></span>
  <span>item 9 <p class="tooltip">Tooltip!</p></span>
  <span>item 10 <p class="tooltip">Tooltip!</p></span>
</div>


Solution

  • The only solution I could find that did not involve javascript was to use an ugly hack - by adding translateZ(0); to the tooltips they appears 'above' the other columns.

    It's not ideal but from my research there doesn't yet seem to be a way to allow overflow between CSS columns.