Setting the maximum width for a tooltip with the property to prevent white space wrapping

I'm currently working on overarchitecting a tooltip. My aim is for the tooltip abstraction to have no knowledge of the wider context (document). It should simply be placed within an element and function seamlessly.

Unfortunately, I'm struggling to achieve the desired max-width behavior:

https://i.sstatic.net/fUmBP.png

This difficulty arises because the content has white-space: nowrap.

However, removing it presents me with this issue:

https://i.sstatic.net/GK7PO.png

I need assistance in adjusting the layout so that the proper max-width behavior can be achieved. Specifically, when the line runs out of room, the content should wrap onto a new line.

You can view the example here: https://jsbin.com/fucujak/3/edit?html,css,output

helper {
  display: inline-block;
  position: relative;
  top: 30%;
  left:30%;
  padding: 10px;
  background: green;
  color: white;
  border-radius: 300px
}

tooltip {
  display: block;
  position: absolute;
  top: 0; right: 0; bottom: 0; left: 0;
}

tooltip:hover tooltip-anchor {
  display: block;
}

tooltip-anchor {
  display: none;
  position: absolute;
  top: 100%;
  left: 50%;
  width: 0;
  height: 0;
}
tooltip-container {
  display: block;
  position: absolute;
}
tooltip-positioner {
  display: block;
  position: relative;
  left: -50%;
}
tooltip-content {
  display: block;
  padding: 10px;
  /* white-space: nowrap; */
  background: blue;
  color: white;
  max-width: 100px;
}
  <helper>
    i
    <tooltip>
      <tooltip-anchor>
        <tooltip-container>
          <tooltip-positioner>
            <tooltip-content>
              Some long, extended tooltip content
            </tooltip-content>
          </tooltip-positioner>
        </tooltip-container>
      </tooltip-anchor>
    </tooltip>
  </helper>

Answer №1

Here is a simple way to achieve this:

element {
  display: inline-block;
  position: relative;
  top: 30%;
  left: 30%;
  padding: 10px;
  background: green;
  color: white;
  border-radius: 300px
}

tooltip {
  position: absolute;
  display:none;
  top: 100%; /* place at bottom*/
  /* center*/
  left:50%;
  transform:translateX(-50%);
  /**/
  margin-right: -200px; /* this is the trick that will make sure the content can go up to 180px (an arbitrary big value) */
  max-width:180px; /* your max-width*/
  padding:10px;
  background:blue;
}
element:hover tooltip {
 display:block;
}
<element>
  i
  <tooltip>
    Some long, extended tooltip content
  </tooltip>
</element>

<element>
  i
  <tooltip>
    Some long
  </tooltip>
</element>

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

Tips for adding a click event to a random "div" within a parent "div" element?

Upon clicking a main "Div", it splits into an n X n matrix with random colored divs. I now want to implement a click function for these random colorful divs that are scattered throughout the main "div". $(window).load(function() { var no = 1, $m = ...

I am seeking to enable a specific div when the crawler condition is met

This specific div houses the character and becomes active when the character is clicked. Upon clicking, a jQuery function is triggered to display other countries. //this particular div should remain active when the crawler condition is met. It displays th ...

Recalling a hidden div in a flexbox arrangement

I am facing a challenge with hiding and displaying two aside divs to enable a fullscreen view of my central div main. The layout is created using flexbox to assign proportions, but when I try to redisplay the elements, it disrupts the original design. Belo ...

Creating visuals from written content

I am attempting to transform Y[0] into an image rather than text. Currently, it is only displayed as a plain text link and not as an image. <html> <head> <script type="text/javascript"> function modifyContent(){ var rows=document.g ...

CSS failing to reach the full width of the container

My navigation tab is currently only occupying half of the width that I need it to. I have tried various solutions found on this site and through Google, but none seem to work as expected. The black background of the navigation should fill up 100% of the wi ...

Retrieve only the most recent input value

Currently, I am working with a text input to capture user input. As of now, here is my code snippet: $(myinput).on('input', function() { $(somediv).append($(this).val()); }); While I want to continue using the append function, I am facing an i ...

Issues with 'Backspace' and 'Clear' buttons on HTML calculator preventing proper function in JavaScript

Hello everyone! I've recently started working on a pure HTML/CSS/JS calculator project. While the HTML and CSS parts went smoothly, I'm facing some issues with getting the initial functions to work properly. Despite doing some research online, I ...

The battle between Hover, Focus, and Blur modes intensifies

My goal is to implement 4 different effects on an element: When hovering over the element. When moving away from the element. When focusing on the element. When blurred. However, I'm encountering a conflict where when I focus on the element, the ...

What is the best way to update just the image path in the source using jQuery?

I am currently working on implementing a dark mode function for my website, and I have successfully adjusted the divs and other elements. However, I am facing an issue with changing the paths of images. In order to enable dark mode, I have created two sep ...

Deactivate a button on a specific tab

My setup includes two tabs: <div class="modal-body"> <form name="myForm" novalidate="novalidate"> <ul class="nav nav-tabs"> <li class="active"><a data-toggle="tab" href="#basicInfo">Info</a></li> ...

Predicting the width of an HTML element to smoothly transition its opacity and width using CSS - a helpful guide

Currently, I am facing a situation where I have a span node with an innerText that I will be changing at runtime. As I change the innerText, the offsetWidth of the node will also change based on the length of the future innerText. In order to handle CSS tr ...

What are some solutions for resolving differences in the display of pseudo elements between Macbooks and Windows desktops?

Greetings! I successfully converted an XD file to HTML and CSS code. I tested it on my Windows PC, but I haven't had the chance to test it on a Macbook yet. Details: I implemented a button with a pseudo element to show a right arrow on the right side ...

Is it possible to change the text displayed when hovering over various images using just CSS and HTML?

I have created an image and text hover effect. There are four images and corresponding paragraphs for each image. When hovering over an image, the specific paragraph should replace the previous one using only HTML and CSS. Please note the following: The de ...

Using Selenium with Java to interact with elements on a webpage, I encountered an issue where I was unable to click

I utilize Selenium for purposes other than web testing; my main goal is to extract and modify text from a website. Specifically, I use it within an additional editor to manipulate content on Confluence. The text I extract often contains various formatting ...

What is a creative way to get rid of old content on a webpage using jQuery without relying on the

As I work on my crossword project, I have almost completed it, but encountering a problem. Within my HTML code, I have a select list that loads a .JSON file using Javascript. <form method="post" id="formulaire"> <div class="toto"> <sel ...

Ensuring Proper Alignment in Bootstrap

https://i.sstatic.net/bGTXG.jpg https://i.sstatic.net/4Vh1k.jpg Having some difficulties displaying JSON data in a grid layout with React. Facing the following challenges: Uneven spacing due to less margin on the left side. Attempts to align ...

What is the reason for the absence of margin collapsing in this particular instance?

One thing that confuses me is the absence of vertical margin collapse between each definition list(dl). Looking at the style selector "#sweden dl" below, I have specified a margin of 10px 20px; meaning a margin-top and margin-bottom of 10px for each dl, a ...

I am currently experiencing an issue where the list items bullets (<ul>) and numbers (<o>) are not displaying correctly on my webpage, even after using the overflow-x: hidden

I am facing a challenge with my web page where the list items are not displaying the usual dots and numbers next to them. Interestingly, when I remove the overflow-x: hidden; property in my CSS, the list item numbers and dots show up immediately, although ...

The issue with CSS filter invert not functioning properly in Mozilla Firefox is causing complications

I am currently developing a small Firefox add-on designed to make reading pages at night more comfortable. The goal is to invert page colors without affecting images. Here is the code snippet I am using: document.body.style.filter = "invert(100%)"; var i ...

Identifying the object property of a popup window in Selenium Webdriver: simple hacks!

Whenever I attempt to click a link in my application, a popup window opens. In this window, I need to execute a certain action such as a click, however, I am struggling to identify any properties of the objects within. My application is optimized for Inte ...