Tips for resizing a tooltip using CSS

I am currently working on customizing tooltips and would like them to display right below the hover text in a responsive manner, with the ability to have multiline text. Instead of spreading horizontally, I want the tooltip to expand vertically based on the length of the content.

My goal is to adjust the width of the text so that the container's vertical size increases as the text gets longer rather than flowing horizontally.

Here is what I have attempted so far:

.tooltip {
    position: relative;
    display: inline-block;
    border-bottom: 1px dotted black;
    color: black;
}

.tooltip:hover {
    text-decoration: none;
}

[data-title]::after {
    content: attr(data-title);
    background: rgba(0, 0, 0, 0.2);
    color: #111;
    -website-transform: translateY(50%) translateX(-55%);
    transform: translateY(50%) translateX(-55%);
    word-wrap: break-word;
    height: 350px;
    position: absolute;
    padding: 1px 5px 2px 5px;
    bottom: -2.6em;
    left: 0%;
    white-space: nowrap;
    box-shadow: 1px 1px 3px #222222;
    opacity: 20;
    border: 1px solid #111111;
    z-index: 99999;
    visibility: hidden;
}
<p>Words words <a class="tooltip" data-title="Title text.">link text</a> words.
    More words <a class="tooltip" data-title="More link text.">more link text</a>.</p>

Answer №1

Check out this code snippet to create tooltips:

[data-title]:after {
    content: attr(data-title);
    background: rgba(0,0,0,.9);
    color: #fff;
    word-wrap: break-word;
    height: auto;
    width: 100px;
    position: absolute;
    padding: 1px 5px 2px 5px;
    top: 18px;
    left: 0;
    word-break: break-all;
    box-shadow: 1px 1px 3px #222222;
    opacity: 20;
    border: 1px solid #111111;
    z-index: 99999;
    visibility: hidden;
}

.tooltip {
    color: blue;
    position: relative;
    cursor: pointer;
}

[data-title]:hover::after{
    visibility: visible;
}
To use this code effectively, make sure to understand how to manipulate tooltip placement using JavaScript for better performance. This will allow for automatic adjustment of the tooltip based on the position of the element triggering it.

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

Getting the JavaScript file name within another JavaScript file - a simple guide

Imagine having an HTML file that references two external JavaScript files. One of these JavaScript files contains errors (likely compilation errors), while the other file includes an onerror method without any issues. When you open the HTML file in a brows ...

What is the proper way to structure the ng-options syntax in AngularJS?

I received an array from a REST service and I am attempting to generate a dropdown menu based on that data. Check out the jsfiddle example here $scope.reasons = [{ "languageLanguageId": { "languageId": 1, "lastUpdate": "2015-05-08T11:14:00+03:00" ...

Blend jQuery fade in

I have two variables var $aa = $('#Adiv').find('li').has('class'); var $bb = $('#Bdiv'); Both variables should be faded in using fadeIn() $aa.fadeIn(); $bb.fadeIn(); If they have the same action, is it possible ...

Is it possible for me to utilize window.top within the .js file?

Within my GWT code, I am transferring a variable to a JSP file. The process looks like this: <html> <head> <script type="text/javascript"> alert("Inside the JSP file"); var criticalPath = window.top.criticalPath; ...

Expanding a list in AngularJS: Implementing a scrollbar for a seamless user experience

I need assistance in creating a layout using AngularJS and angular-material that includes a vertical scrollbar when necessary: <div layout="column"> <div> <!-- Occupies 80% of window browser --> <md-list> <md-l ...

Crash in the Android WebView of the Galaxy S3 device due to Signal 11 SIGSEGV

My Android WebView is experiencing issues with memory access and crashes on a Galaxy S3 running Android 4.0.4. The HTML5 content includes interactive battles where enemies appear for the user to slash, interspersed with normal HTML pages. Despite the use o ...

Pass along computed style data to the parent component in Vue.js

I am relatively new to VueJS and currently in the process of exploring its features. One specific issue I am facing on my website involves a TopBar component that includes both the logo and the menu. <template> <div id="topBar"> <div ...

How can TypeORM be used to query a ManyToMany relationship with a string array input in order to locate entities in which all specified strings must be present in the related entity's column?

In my application, I have a User entity that is related to a Profile entity in a OneToOne relationship, and the Profile entity has a ManyToMany relationship with a Category entity. // user.entity.ts @Entity() export class User { @PrimaryGeneratedColumn( ...

Global variable appears undefined in Angular 2 view, yet it is still displaying

I'm running into issues with my Angular 2 code. Inside my ts file, I have the following: import { Test } from '../../../../models/test'; import { TestService } from '../../../../services/test.service'; import { Job} fr ...

`Where can I locate a grid example?`

Right here: Upon reading the documentation, I discovered: onItemInserting has the following arguments: { grid // represents the grid instance item // item being inserted } In my software application there are multi ...

Enhance the performance of your React/NextJS app by controlling the rendering of a component and focusing on updating

I'm facing an issue with my NextJS application that showcases a list of audio tracks using an <AudioTrackEntry> component. This component receives props like the Track Title and a Link to the audio file from an external data source. Within the ...

Customizing the attribute of an HTML tag using EJS or jQuery

Within my express server, I am rendering a page with the following data: app.get('/people/:personID', function (req, res) { res.render("people/profile", {person: req.person }); }); Inside my profile.ejs file, I can display the data within an ...

In the world of MUI, how should one interpret the symbols '&', '>', and '*' when used together?

Currently, I am in the process of building a website using React. To help with this project, I purchased a Material-UI React template. As I was going through the code, I came across the line 4 where it mentions '& > *', and I'm not qu ...

Create copies of Div elements for them to revert back to their original state following

It seems like a simple task, but I'm struggling to figure out how to do it. On a single page, I have multiple divs: <div id="playerid-1"><a href="javascript:loadplayer(1);">[LOAD PLAYER]</a></div> <div id="playerid-2">& ...

The Material UI button feature neglects to account for custom CSS styles when attempting to override the default settings

Utilizing a custom bootstrap css styles in my react app, I am seeking to enhance the default material ui components with the bootstrap styles. import React, {useState} from 'react'; import 'cg-bootstrap/core/build/cg-bootstrap-standard.css&a ...

I am trying to make my CSS divs remain in the exact same position on the background image, even when the browser is resized both horizontally and vertically. How can I achieve this?

I need the red dot divs to remain fixed on a world map, regardless of browser resize. Not sure if I should be adjusting div properties or background. Here's my CSS code, any feedback is appreciated as I'm new to web design: HTML: <!DOCTYPE ...

Can Regex expressions be utilized within the nodeJS aws sdk?

Running this AWS CLI command allows me to retrieve the correct images created within the past 45 days. aws ec2 describe-images --region us-east-1 --owners self -- query'Images[CreationDate<`2021-12-18`] | sort_by(@, &CreationDate)[].Name&apos ...

Guidelines for dynamically switching between two buttons (function/text) depending on an external factor (such as the number of items bought)

I'm exploring a previous concept of toggling between two buttons conditionally in a CRA. import ... const ... export const Index = () => { // Setting up boolean state to toggle buttons conditionally const [reachMax] = React.useState(id <= ...

The proper approach is using CSS for linking pseudo-classes

Look what I stumbled upon: Important: Remember, a:hover MUST be placed after a:link and a:visited in the CSS code to work properly!! Remember: Place a:active after a:hover for it to have an effect in the CSS code!! Note: Pseudo-class names are not case- ...

Checkboxes display on a separate line when rendered inlines

I'm having an issue with the rendering of my checkbox in CSS. The checkbox is currently displaying on a separate line from the text, but I would like it to be inline with the rest of the content. For reference, here is the full CSS and HTML code: htt ...