Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create tooltip without JavaScript using only inline CSS?

Tags:

html

css

I'm trying to create a hover tooltip using inline CSS without JavaScript.

This is the code I have now

<a href="#" 
style="{position:relative; top:50px; left:50px;}
       :hover span {opacity:1; visibility:visible;}">
  hover text
  <span 
  style="top:-10px; background-color:black; color:white; border-radius:5px; opacity:0; position:absolute; -webkit-transition: all 0.5s; -moz-transition:  all 0.5s; -ms-transition: all 0.5s; -o-transition:  all 0.5s; transition:  all 0.5s; visibility:hidden;">
    tooltip text
  </span>
</a>

According to this it should be allowed: http://www.w3.org/TR/2002/WD-css-style-attr-20020515

I know this is not the recommended way to do it, but it needs to be usable where only inline CSS can be used.

like image 780
Kyra Avatar asked Oct 02 '13 03:10

Kyra


People also ask

How do you make a tooltip only in CSS?

To create a tooltip with only CSS, we'll need to use the pseudo-element. The pseudo-element has a content attribute that can store its value. We can pass a text, that will be used to display tooltip info.

Can we add tooltip in CSS?

You can create custom CSS tooltips using a data attribute, pseudo elements and content: attr() eg.

How do I create a custom tooltip?

To make a simple tooltip, we'll first create the HTML element that triggers the tooltip when hovered over. We'll create this element as a div and assign it the class hover-text. Next, we'll create the element for the tooltip itself. This will be a span element with the class tooltip-text.

How do I change the tooltip style in CSS?

The tooltip is automatically generated by web browsers, it actually can not be removed or changed. To change the tooltip style, we need to add a tooltip text to a different attribute, for example data-title , then use CSS code to create and customise the style. In WordPress, you can add the CSS code to your theme CSS.


1 Answers

You were pretty close, I've added some properties:

HTML Markup:

<a href="#" class="tooltip">hover text
  <span>tooltip thisIsALongTextMadeToBeBreak</span>
</a>

CSS Markup:

a.tooltip {
    position: relative; 
    top: 50px; 
    left: 50px;
}

a.tooltip:hover span {
    opacity: 1; 
    visibility: visible;
}

a.tooltip span {
    padding: 10px;
    top: 20px;     
    min-width: 75px;
    max-width: 150px;
    background-color: #000000; 
    color: #FFFFFF;
    height: auto;
    border-radius: 5px; 
    opacity: 0; 
    position:absolute;
    visibility: hidden;
    word-wrap: break-word;
    -webkit-transition: all 0.5s; 
       -moz-transition: all 0.5s; 
        -ms-transition: all 0.5s; 
         -o-transition: all 0.5s; 
            transition: all 0.5s;    
}

Here's a live demo if you want to check it out

If you want, you can check it out some more examples/ideas here

Hope it helps!

like image 146
Luis Avatar answered Sep 26 '22 12:09

Luis