Styling text inside a text hover box

I have implemented code that displays a text box when you hover your mouse over specific text. However, the issue I am facing is that the text inside the text box is coming from an attribute of the <a> tag and I am unable to format it as desired. I intend to create a list within the text box, but it only appears as a continuous block of text.

Here is the HTML structure:

<a data-text="Could use this text instead of title" title="1. ~~~~ List item (/br)~~~~ 
                                    2. ~~~~ List item ~~~~ 
                                    3. ~~~~ List item ~~~~ 
                                    4. ~~~~ List item ~~~~ 
                                    5. ~~~~ List item ~~~~ " class="tooltip">Hover over this text</a>

Below are the CSS styles applied:

.tooltip{
    display: inline;
    position: relative;
}

.tooltip:hover:after{
    background: #333;
    background: rgba(0,0,0,.8);
    border-radius: 5px;
    bottom: 26px;
    color: #fff;
    content: attr(title);
    left: 100%;
    padding: 5px 15px;
    position: absolute;
    z-index: 98;
    width: 220px;
}

content: attr(title);

.tooltip:hover:before{
    border: solid;
    border-color: #333 transparent;
    border-width: 6px 6px 0 6px;
    bottom: 20px;
    content: "";
    left: 50%;
    position: absolute;
    z-index: 99;
}

You can view a demo of this functionality by visiting: http://jsfiddle.net/h3tqwust/1/

My question is, how can I format the text within the text box to include line breaks?

Answer №1

One way to maintain white space in a simple manner is by using white-space: pre within the :after pseudo-element, and paying close attention to formatting the title attribute.

Unfortunately, certain HTML tags like <b> may not function properly due to how the inserted content is interpreted.

To achieve more intricate styling, a jQuery/JavaScript solution may be necessary.

.tooltip{
    display: inline;
    position: relative;
}

.tooltip:hover:after{
    background: #333;
    background: rgba(0,0,0,.8);
    border-radius: 0px 5px 5px 5px;
    top: 15px;
    color: #fff;
    content: attr(title);
    left: 100%;
    padding: 5px 15px;
    position: absolute;
    z-index: 98;
    width: 220px;
    white-space: pre;
}
<a
title="1. ~~~~ List item ~~~~
2. ~~~~ List Item ~~~~
3. ~~~~ List Item ~~~~
4. ~~~~ List Item ~~~~
5. ~~~~ List Item ~~~~"
class="tooltip">Hover over this text</a>

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 transform a JQuery button into a dropdown menu

For inspiration, consider this JQuery UI Button example: http://jqueryui.com/demos/button/#splitbutton Now, how can you create a dropdown menu when the small button is clicked? My main concern is the way .button() alters the button's actual appearanc ...

Using html as a template parameter in django

When passing HTML to my template, I am using the following code: passDict["problemText"] = <p> Some html</p> return render(response,'main/base.html',passDict). However, when trying to display {{problemText}} in my HTML file, I only se ...

Ways to update font color using bootstrap-cue

Looking for a button with white text on a dark-blue background Find the code in xxx.vue <b-dropdown text="user" right variant="blue" class="signout-button"> <b-dropdown-item @click="toMain()">sign out</b-dropdown-item> </b-dro ...

How can I prevent my Bootstrap navbar from collapsing?

Is it possible to prevent my Bootstrap nav from collapsing when the page is minimized? I only have two elements in my navigation bar, and the Login button fits even when the page is minimized. /* Navigation */ .navbar-brand img { width:30%; heig ...

Steps to launch a new image in the same window when clicking on a specific id within the accordion panel

Is it possible to switch the image on the left side of a bootstrap accordion panel when clicking on a specific id, using jQuery alone or is AJAX necessary in conjunction with jQuery? I have 6 different images that should be changed based on which panel id ...

Challenge involving CSS and Javascript for solving puzzles

In my attempt to create a puzzle with 2 rows and 3 columns using CSS and JavaScript, I envision the pieces of the puzzle being black and cut into various shapes. The objective is for users to drag these pieces onto the board in order to complete it. I have ...

Incorrect legend colors in Highcharts pie chart

[![enter image description here][1]][1] There seems to be an issue with the Pie-Chart where the Slice color and the Legend color do not match when the color is set using className. This problem does not occur with some other charts. If you look at the co ...

Dividing the z-index by half of the shape

I am trying to achieve the following shape: https://i.stack.imgur.com/Lr9gv.png So far, I have attempted the following code. How can I combine these elements together? .clal-loader{ display:flex; } .clal-loader div{ border: 10px solid #38477e; b ...

Guarding Components Against Vague CSS

Currently, we are in the process of developing widgets that will be embedded on multiple pages. To safeguard these widgets from potential CSS conflicts (such as a generic p { background-color: red }), I have two options in mind: The first option involv ...

Troubleshooting an HTML Design Flaw: How to create a 3x3 table with

I have been attempting to design a HTML page with a 3x3 layout. Despite trying various methods, including divs and tables, I am having difficulty achieving equal spacing around the centered image and the other table rows. Below is an example of the layout ...

Adjusting the sensitivity of mousewheel and trackpad using JavaScript

I am currently utilizing CSS3 to smoothly move a div up and down based on the direction of scrolling. By monitoring the mouse wheel event, I can detect when a user scrolls down and trigger a CSS3 transition to slide the div up. However, I am encountering a ...

Creating visually appealing emails can be a breeze with the help of freemarker, spring, and outlook

I've been facing challenges with email templates and images for a while now. Although I have managed to get the images to display in the email, they are not taking into account the CSS. Backend/Spring : This is a basic Spring service used for sendin ...

Utilize JavaScript conditions to dynamically apply styles within your web application

I am facing a challenge with managing two separate <style> tags that each contain a large number of styles and media queries. The issue is that one set of styles is intended for desktop users, while the other is meant for mobile users. When both se ...

CSS animation for expanding and collapsing a section of the sidebar

I am attempting to recreate the animation effect seen in the gif below using CSS to open and close the sidebar, but I am having trouble achieving it. https://i.sstatic.net/DFnhP.gif I have tried using hover but it is not working as expected. (You can se ...

Issue where the background color remains the same and does not change

I'm having trouble identifying the issue in this code. I am attempting to change the background color using the following CSS: body { background-color: black; padding: 0px; margin:0px; height: 1500px; } However, it doesn't seem to ...

Efficient page navigation bar that doesn't clutter the layout

I'm facing an issue with my navbar setup. I want it to stay at the top of the page without being sticky. However, the presence of the navbar is causing a scroll to appear on my page. This happens because: The navbar takes up space I sometimes use tri ...

Remove the link to a file that was uploaded with the help of angular

I have created a script in AngularJS for uploading files which successfully uploads the files to a folder. However, I am facing an issue where removing a file from the queue does not delete the file from the folder. Can anyone suggest how to unlink or re ...

Guide on transferring an HTML variable to a PHP script through AJAX

I am currently working on my website where I aim to create a random code that will be passed onto a PHP file for future retrieval. However, the code seems to be malfunctioning. Take a look at the code: Javascript/HTML: function init() { var code ...

Ways to extract image URLs from HTML and use them in Java

Hi there, I am trying to retrieve the URLs for images on Bing. For example, this link returns cat images. How can I specifically obtain the .png URLs? ...

Creating captivating website effects: Techniques for fading in divs using CSS3, HTML5, and Javascript

Currently facing a puzzling challenge that has me scratching my head. I'm trying to figure out how to achieve a fade-in effect for dynamically generated divs. These divs are created through Javascript, and I need them to smoothly fade onto the page. I ...