Correct placement of elements using absolute positioning and optimized scroll functionality

I am currently working on implementing tooltips for items within a scrollable list. My goals for the tooltips are as follows:

  • Ensure they are visible outside and not restricted by the scroll area
  • Appear immediately after the corresponding item
  • Be detached from the content of the item containing them

The structure in markup is as shown below:

<ul>
  <li>
    <div class='option'>Option</div>
    <div class='tooltip'><h4>Tooltip</h4> Tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip.</div>
  </li>
  <!-- repeat the above li structure fourteen more times -->
</ul>

A sample CSS snippet includes:

ul{
  list-style-type:none;
  border:thin solid gray;
  width:80px;
  height:200px;
  overflow:auto;
}

.tooltip {
  display:none;
  border:thin solid black;
  box-shadow: 2px 2px 2px #aaa;
  background-color:white;
  z-index:10000;
  padding:3px;
  width:150px;
  position:absolute;
}

.option:hover + .tooltip{
  display:block;
}

You can try out a demo of the existing implementation here: http://plnkr.co/edit/vhKquqeswyDwvDQNP12G?p=preview

When using relative positioning for the tooltip, it shows up correctly but gets clipped by the scroll area (and remains part of the content).

Alternatively, with absolute positioning, the tooltip appears outside the scroll area, but its placement becomes incorrect upon scrolling away from the top of the ul element.

To address these issues, applying absolute positioning along with a relatively positioned wrapper to align the tooltip with the list item ensures proper positioning even after scrolling, detachment from the item's content, but may result in clipping by the list area.

Is there a way to achieve all three desired behaviors using CSS?

Answer №1

Although it may not align perfectly with your initial intentions, thinking creatively and outside the usual boundaries could offer users a unique and engaging UX experience...

It's worth mentioning that this approach may not be fully compatible with mobile devices as it relies on hover functionality rather than touch. Testing and adjustments will be necessary to ensure events work smoothly or including JavaScript shims where needed.

Check out the Demo

http://plnkr.co/edit/ZYkyjbS9vPzQ4EYXKHHG?p=preview

HTML Structure

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
  </head>

  <body>
<h3>Relative positioning</h3>  
  <div class='wrapper'>
      <ul>
        <li>
          <div class='option'>Option</div>
          <div class='tooltip'><h4>Tooltip 1</h4> Tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip.</div>
        </li>
        <li>
          <div class='option'>Option</div>
          <div class='tooltip'><h4>Tooltip 2</h4> Tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip tooltip.</div>
        </li>
        ... etc
      </ul>
    </div>
  </body>
</html>

CSS Styling

body{
  font-family:arial;
  font-size:80%;
  }

ul{
  list-style-type:none;
  border:thin solid gray;
  width:5em;
  height:10em;
  overflow:auto;
  margin:0;
  padding:0;
}

.tooltip {
  display:none;
  border:thin solid black;
  box-shadow: 2px 2px 2px #aaa;
  background-color:white;
  padding:3px;
  width:150px;
}

.option:hover {
  background-color: #CCC;
}

.option:hover + .tooltip{
  display:block;
  position:absolute;
  top:0;
  left:6em;
}

.wrapper{position:relative;}

For further enhancement: consider adjusting the styling so that the tooltip text appears on the left side with a bold border matching the highlight color of the option for a more visually appealing effect!

http://plnkr.co/edit/7JTjgxDTjSNonIZCOPZE?p=preview

Additional modifications are necessary to ensure accessibility (consider implementing longdesc or aria-describedby attributes).

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The z-index of the fixed header in FullPageJS is causing the section's content to be hidden

For my current project, I am incorporating FullPageJS and using a fixed top menu that adjusts its height based on the viewport size. On smaller screens, the menu items will be stacked on top of each other, while on wider screens, the menu items will be po ...

Implementing Fullpage.js with persistent elements throughout slides

Can I keep certain elements fixed between slides? I found that the only way to accomplish this was by placing the text element outside of the slide div. <div class="section" id="section1"> <div class="intro"> <h1> ...

The content of the served HTML Table remains static and requires a server restart for any updates to

Having been encountering this issue for quite some time, I have searched extensively for a solution but to no avail. Therefore, I am taking matters into my own hands and posing the question myself. The dilemma is as follows: https://i.stack.imgur.com/UZrQ ...

Angular renders HTML elements

Greetings! I am currently experimenting with using AngularJS for the frontend and Wordpress as the backend of my project. To load content from Wordpress into Angular, I am utilizing a JSON wordpress plugin along with making $http requests. The content th ...

JavaScript and the importance of using commas in arrays

I am developing a system that displays text in a textarea when a checkbox is checked and removes the text when the checkbox is unchecked. The functionality is mostly working as intended, but I am facing an issue where commas remain in the textarea after un ...

Adjust the display of inline-block divs to disregard line breaks

I am having an issue with three side-by-side divs using the display:inline-block property. When the divs are empty, they all align perfectly on the same horizontal level. However, once I add <p> tags and some line breaks (<br/>) to the first ...

Tips for keeping a consistently viewable and editable iteration of your HTML form

How can I efficiently maintain both viewable and editable versions of the same HTML form? I am working on an application that has numerous custom data-entry screens with a high number of fields, and managing separate layouts for viewable and editable form ...

`A brief disruption in loading the visual style of Google Maps`

Using the Ionic Framework, AngularJS, and Google Maps API, I encountered an irritating issue with my mobile app. Every time the map loads, there is a noticeable delay in loading the map style. This delay is particularly problematic as my map style converts ...

What steps should I take to resolve the textarea border bottom CSS effect?

My simple border bottom animation is working fine with a basic input element, but it's not functioning properly when used with a textarea. (If using JavaScript is necessary for a solution, please provide guidance) How can I adjust the height of a te ...

Is it possible to incorporate a different website font into an email template?

Currently working on an email template and looking to match the font with my website. Is there a method to ensure my site's font carries over into the email template? Provided below is the HTML code: <!DOCTYPE html> <html> <head> ...

Which is better: triggering mouseleave from inside or outside of mouseenter event

As I delve into the basics of jQuery, I stumbled upon mouseenter and mouseleave actions. The question that arose in my mind is: where should I place the mouseleave action? Which approach is more correct and reliable? $(document).ready(function(){ $(&a ...

Determine the number of rows in the Tabulator's table

Can anyone tell me how to retrieve the number of rows in a table created using Tabulator on a website? Is there a method like table.size or table.length that I can use for this purpose? The table has been initialized with the following code: table = new T ...

JavaScript: Creating Custom IDs for Element Generation

I've been developing a jeopardy-style web application and I have a feature where users can create multiple teams with custom names. HTML <!--Score Boards--> <div id="teamBoards"> <div id="teams"> ...

The :after pseudo-element in action with multi-line text

Can the styling of the :after pseudo-element be applied to every line of multi-line text? Here is the code I am currently using: .container { width: 50px; } .text { position: relative; } .text:after { content: ''; position: ...

The chosen date is not preserved within the select box

I have these specific items: $scope.items = [{"date":"2014-12-26T05:00:00.000Z","display":"6:00"}, {"date":"2014-12-26T05:15:00.000Z","display":"6:15"}, {"date":"2014-12-26T05:30:00.000Z","display":"6:30"}] When I use the following code: <select ...

Enhance the appearance of Font Awesome stars by incorporating a hover effect

Is there a straightforward way to make them turn solid yellow when you hover over them? const styles = theme => ({ root: { flexGrow: 1, overflow: 'hidden', padding: theme.spacing(0, 3), }, paper: { maxWidth: 800, mar ...

Guide to quickly redirecting all 404 links to the homepage on a simple HTML website

Is there a way to automatically redirect clients to the homepage if they click on a broken link in a basic HTML website, instead of displaying a custom 404 page? UPDATE: My website consists of only 5 plain HTML pages hosted on GoDaddy. There is no server- ...

Trouble with CSS: Struggling to apply margins to a line of images in React JS

I've encountered an issue where I can't seem to add margin to the row of images. The margin appears fine without any CSS when viewed on a wide screen, but once I scale it down to mobile view, the margin disappears completely. Even after attemptin ...

Change the order of the table data elements using the nth-child CSS selector

I need the second td in my table to appear on top of the first one when I resize the screen. @media screen and (min-width: 200px) and (max-width: 872px) { td :nth-child(1) {display:bottom} td :nth-child(2) {display:top} } <table border="0" cell ...

Implementing a CSS codepen within a React Material-UI component

I'm struggling to implement this effect on my material ui card component using react and makeStyles. I am having difficulty translating this effect to the cards, could someone assist me in simplifying it into a CSS code that can be used in MakeStyles? ...