2

I'm using some code that shows a text box with when you hover your mouse over some text. The problem is the text that is displayed in the text box comes from an attribute of the <a> tag it's in and I can't format it. I want to make a list in the text box, but I can only make it just a wall of text.

HTML:

<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>   

CSS:

.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;
}

Check out this demo: http://jsfiddle.net/h3tqwust/1/

How can I format the text (have line breaks) for this?

MitchCool1
  • 329
  • 3
  • 14

1 Answers1

4

Here is a simplistic approach to formatting, use white-space: pre to preserve the white space in the :after pseudo-element and make sure the title attribute is formatted carefully.

However, HTML tags such as <b> won't work because of the way that the inserted content is parsed.

If you need more sophisticated styling, then you need a jQuery/JavaScript assisted solution.

.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>
Marc Audet
  • 46,011
  • 11
  • 63
  • 83