How do I horizontally center a tooltip for a button using CSS?

I've been struggling to find a solution for this issue. The structure I have is similar to the following. This excerpt represents just one out of five menu points, with normally five <li> items in the ul.

<nav id="loggedInMenu">
    <ul role="navigation">
         <li>
            <a class="homeBtn" href="/my">
                <span class="homeBtnTooltip btn tt" title="Homepage">Homepage</span>
            </a>
         </li>
    </ul>
</nav>

In my code snippet, you can see that within the <a> tag there's a span.tt acting as a tooltip. I've created a live example of my code... feel free to check it out. http://jsfiddle.net/Ngjzk/1/

The issue at hand is aligning the tooltip centrally beneath the green button. Although the hover functionality works fine, the positioning of the blue tooltip is off to the right of the green button instead of being centered underneath it.

I currently have a workaround in the CSS section to align the tooltip but I would prefer not to use this method for each menu point. Since the tooltip text changes dynamically, it should always have the full width of the text and be centered underneath each button.

Do you have any suggestions on how to achieve this?

Thank you,

Answer №1

revision [updated correct answer is now at the top with answer history at the bottom]

Solution: Utilize a two-fold positioning method; first (outer span, position absolute; left: 50%) and second (inner span, position relative; left: -50%)

Check out this example on jsFiddle


original
Consider using a 'dynamic' negative percentage margin-left

#menu .tt {
  text-align: center;
  position: absolute;
  top: 38px;
  margin-left: -50%;
}

revision
If possible, restructure your HTML by placing the span outside of the a tag View updated example on jsFiddle

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

How to adjust the grid-gutter-width in Bootstrap 4.5 container?

If we want to change the $grid-gutter-width: 30px !default; variable specifically for the .container class, we can do so in the _variables.scss file. For example: .container { padding-left: 40px; padding-right: 40px; } Where can I make this custo ...

Struggling to get the hang of CSS animation?

Here is a code snippet that I am using: //Code for animating skills on view document.addEventListener("DOMContentLoaded", function(event) { function callback(observations, observer) { observations.forEach(observation => { if (observati ...

Ensure consistent element heights within adjacent columns using CSS

My template looks like this: https://i.sstatic.net/hoLzR.jpg I am trying to keep the same height between each item in both columns, where the height is determined by the tallest item in each column when they are placed side by side. But on smaller screen ...

Personalizing CSS for showcasing bot icon next to bot reply container

I'm currently working on modifying the layout of this HTML page. I want to include a bot icon next to each bot response, and I also need to decrease the space between consecutive messages. https://i.sstatic.net/lBiMD.png Any suggestions on how I can ...

Creating an Interactive Menu System with Nested Options in Angular 2

I have a JSON structure that I need to transform into a menu with sub-menus based on the value of the sub-menu_location field. Here's an example: { "data": [ { "menu_name": "Primary Operations", "enabled": true, "sub-menu_lo ...

What are some ways to enhance mobile browsing with CSS?

I'm facing a challenge with optimizing my webpage for different mobile resolutions in my beginner HTML/CSS course. Whenever I use a website resolution simulator to test, the layout doesn't look right on certain settings. Is there a method to aut ...

Tips on adjusting the size of a font icon using an inline element prior to the font being fully loaded

I'm facing an issue with embedding an icon font within text so that it wraps properly. Currently, the only way I can achieve this is by using &nbsp; between the text and the icon (i tag with font awesome). However, this causes a problem as the ico ...

Samsung devices exclusively showcase background in responsive design

I am encountering a major problem with my website design, specifically with its compatibility on mobile devices. To ensure its responsiveness, I have tested the design on my iPhone, iPad, and Windows Phone. Unfortunately, since I lack an Android device, I ...

What is the best way to place a dropdown menu in front of buttons?

Here is an example snippet of HTML code that showcases a SweetAlert2 date picker popup for a button titled "Click me!": <!DOCTYPE html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf ...

Blade - Assign a random CSS class from an array

I have been searching high and low for a solution to this issue, but so far, no luck. If it has already been addressed elsewhere, I apologize. Here is the code snippet I am currently working with: @{ foreach (var item in Model.Activities) ...

What is the best way to create a div that maintains a consistent size while displaying images inside?

Struggling with a project and attempting to create a fixed div container for uniform picture sizes, I've tested various methods recommended by others but have had zero success in altering the size of my div container. Could it be due to my use of boot ...

Div background image adjusting for parallax scrolling

My goal is to implement a parallax background as a separator for a website. However, I'm facing an issue where the background image only occupies half of the intended size with a white border surrounding it. Despite my efforts, I can't pinpoint t ...

What are your thoughts on using image maps with CSS?

My images are also links, and the code looks like this: <a href="../www.google.com"><img src="pages/squirrely.png" /></a> While they function properly as links, I want them to only be clickable if you click on the central part of the im ...

Combining a standard JSS class with Material-UI's class overrides using the classnames library

I am exploring the method of assigning multiple classes to an element in React by utilizing the classnames package from JedWatson, along with Material-UI's "overriding with classes" technique. For reference, you can see an instance in MUI's docu ...

How to center multiple divs in a row using HTML and CSS

I've been attempting to achieve a pretty basic task, but unfortunately my efforts have fallen short thus far. I have an image and a title that need to be centered within their parent div element. However, I'm struggling to find the proper way to ...

"Javascript encountered an unrecognizable expression when trying to parse a single

Whenever this event occurs, I'm encountering a console error that states "unrecognized expression: .". Everything seems to be working fine so I'm not entirely sure what the issue is other than possibly a syntax error. Any suggestions or ideas on ...

Using flex and align properties to position two elements on the right and one on the left is a common layout challenge

I'm facing an issue with element positioning and text alignment. The navigation container consists of three elements: logo, title, and nav-list. You can find the code on CodePen. /**{ margin: 0; padding: 0; box-sizing: ...

Looking for assistance with resizing SVG images

I am facing some challenges with the HTML/CSS code to make all SVG's scale uniformly in terms of height/width. I have set up a simple structure but I'm unsure of what steps to take next. When I view the images in the browser, they are all scaled ...

Excessive indentation detected within the website's formatting

There seems to be an issue with the mobile version of the website, possibly related to flexbox, SVG, or width settings that are unclear to me. Inspecting with Devtools reveals indents from the SVG and text itself. Check out the website here My goal is to ...

Incorporate a captivating background image onto the homepage of your Quarto website

How can I add a background image to fill the entire page on the home page only in a Quarto website? I currently have a solution that adds the background image, but it applies it across the entire website. In this case, I want to exclude the report page fr ...