Aligning the tooltip element vertically

I've created a unique flexbox calendar layout with CSS tooltips that appear on hover. One challenge I'm facing is vertically aligning these tooltips with the corresponding calendar dates effectively.

Although I prefer to achieve this alignment using CSS, my attempts have been unsuccessful so far. Currently, I have set a left value of 4em and calculated the top value dynamically in jQuery due to the varying height of the tooltips. Despite this approach, the vertical alignment of the tooltips isn't perfect, and I can't figure out the reason behind it.

Below is the snippet of my jQuery function:

$('.cal-tooltip').each(function() {
  var calTooltipHeight = $(this).outerHeight(true) / 2;

  $(this).css('top', -calTooltipHeight);
});

CSS Styles:

.tooltip {
  position: absolute;
  z-index: 1;
  background-color: white;
  box-shadow: 0 0 30px 0 rgba(0, 0, 0, 0.2);
}
.tooltip:after {
  content: '';
  position: absolute;
  width: 0;
  height: 0;
  margin-left: -15px;
  margin-top: -15px;
  top: 50%;
  box-sizing: border-box;
  border: 15px solid black;
  border-color: transparent transparent white white;
  transform-origin: 50% 50%;
  box-shadow: -5px 5px 10px 0 rgba(0, 0, 0, 0.1);
}
/* Additional CSS styles omitted for brevity */

#event-calendar .cal-tooltip {
  display: none;
  width: 20em;
  cursor: auto;
}
/* Additional CSS classes and styling rules excluded */

For a live demonstration, visit: CodePen

Answer №1

When working with absolutely-positioned elements, it's important to align them relative to the closest positioned ancestor. Ensure that this ancestor serves as the container for the tooltip:

table-cell { position: relative; }

Instead of relying on pixel values for alignment, consider using percentages:

.tooltip { top: 50%; transform: translateY(-50%); }

By making these adjustments to your code...

https://i.stack.imgur.com/eNIOX.png

You'll notice that the tooltips are now vertically aligned with the date:

https://i.stack.imgur.com/0Zxzt.png

For more in-depth information, check out: Element will not stay centered, especially when re-sizing screen

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 function for displaying checked text is currently malfunctioning when I attempt to open a new tab

I am attempting to display the checked value text in one div. https://i.sstatic.net/rXfai.png By default, Tiffen is active. When I click on idly and puri images, the text of that image appears and shows in the selected food list. However, when I click o ...

Eliminate spacing between divs of varying heights

I'm facing an issue with my small gallery of images. Despite having the same width, they vary in height causing the second row to start below the tallest image from the previous row. How can I eliminate these empty spaces while still maintaining the v ...

Creating a polygon path in Google Maps v3 using an array of coordinates

I'm currently working on creating a polygon around US counties. The coordinates for this polygon are coming from a database and then being json encoded for use in JavaScript. Below is the JSON encoded array generated from PHP code: {"Abbeville-sc":[[ ...

What strategies are effective for handling state reactively in Vuex?

Currently, I am facing an issue with my vuex store. I have implemented two different actions in my store, one being reactive and the other not. However, I specifically need the loadSlidesEach() action to be reactive so that the data gets updated accordingl ...

What is the process for dynamically inserting a new object into an array of data objects in Vue.js?

I am a beginner with vue.js and currently working on a form. I have an add button in my form, so when the user clicks on it, the same form field will be added to the form. Users can add as many times as they want. Here is my initial data: data () { ret ...

Exploring the Canvas with Full Element Panning, Minimap Included

Currently, I am working on incorporating a mini map onto my canvas that mirrors what is displayed on the main canvas. The main canvas includes zoom and pan functions. I have created a rectangular shape for the minimap to display the content of the canvas. ...

Arranging divs within a wrapper, ensuring that one of them displays a vertical scrollbar when the content exceeds the space available

I'm currently facing a challenge with defining the height of the description box in order to achieve the layout shown. I am trying to find a solution without relying on javascript. https://i.stack.imgur.com/3j278.png At present, the wrapper and titl ...

The Npm generate script is failing to create the necessary routes

I've integrated vue-router into my nuxt project, but I encountered an issue when running npm run generate - it generates everything except for my pages. I suspect the problem lies with the router implementation as I didn't face any issues before ...

What steps can be taken to fix the recurring node error "EADDRINUSE"?

Having trouble running an http server through node as I keep encountering the EADDRINUSE error specifically on port 5000. I've attempted using sudo lsof -i tcp:5000 and sudo kill -9 [PID]. Here's a snippet of the shell command: Borealis:BackEnd g ...

The grid fails to apply remote filtering values when an additional Nested ajax call is incorporated alongside the current HttpProxy configuration

Whenever I click for filter/sort for remote filtering, Forms.asp triggers using a proxy and automatically reloads. Previously, when I used the script below to reload the ExtJS grid with Forms.asp returning new XML with filtered grid data, everything worked ...

The argument in question has not been defined

Experimenting with AngularJS, I encountered an error message in the browser console when trying to display a particular example: Error: Argument 'mainController as mainCtrl' is not a function, got undefined at Error (native) at bb My pr ...

Transforming the table into a list by categorizing every 3 rows together

SharePoint tends to use nested tables excessively, causing issues when trying to implement a jQuery carousel on them. To resolve this, I discovered a piece of jQuery code that can convert a table into a list: var list = $("<ul/>"); $('#tab1&apo ...

Can you explain the distinction between using angular.copy() and simply using an assignment (=) to assign values

When a button is clicked, I want to assign certain values using the event parameter. Here is the code: $scope.update = function(context) { $scope.master = context; }; The $scope.master has been assigned the values of user. Recently, I came across th ...

The button labeled "issett" is malfunctioning

Hey there, so I've created a registration form using jQuery and PHP that validates user input in real-time as they type. Once all the information is correct, I want to allow the user to submit the form. My plan is to write an if statement that checks ...

The npm package for @azure/storage-blob doesn't seem to play nice with the Azure Blob storage modules for IoT Edge

Currently, I am developing an edge module using Node.js to interact with Azure Blob storage modules on IoT Edge platform. To achieve this, I am following the documentation available at https://www.npmjs.com/package/@azure/storage-blob. The npm package ut ...

What is the mechanism behind the operation of asynchronous functions within the bcrypt() method in Node.js?

const bcrypt = require('bcrypt'); const saltRounds = 8; const plainPassword1 = "12345"; const plainPassword2 = "56789"; const func1 = async (password, plainP) => { console.log("hashing password"); const h ...

Examining the words within strings and drawing comparisons

I am attempting to analyze words within strings for comparison purposes. This is my objective: var string1 = "Action, Adventure, Comedy"; var string2 = "Action Horror"; if (string1 contains a word from string 2 == true) { alert("found!"); } I have e ...

Rails inspired tag selection tool akin to Stack Overflow

In my Rails application, I am looking to implement a tagging system for models. I want to create a tag selection feature similar to Stack Overflow where I can type in a tag like 'rails' and a drop-down list of options containing that tag will app ...

Leveraging props to set the initial value of component data in Vue 3 Composition API

Currently, I am in the process of developing a search page in Vue 3 using the composition API. One of my components is responsible for displaying a snippet of data that includes specific keywords provided by the parent component. To achieve this, I need to ...

Display the selected value in the `vuetify` select component before the user

I have integrated Vuetify into my project and encountered an issue with the select component. Here is how I have set it up: <v-select v-model="gender.value" :items="gender.items" label="Gender" :solo=" ...