Zeev Katz Zeev Katz - 2 months ago 11
CSS Question

Tooltip using title attribute without js?

I'm searching a long time after another solution for making tooltip from

title=""
attribute on
:hover
selection.

I hope to find a way for using
title=""
attribute and change my
content:""
property dynamically.



* {
-webkit-font-smoothing: antialiased;
font-family: "Trebuchet MS", Helvetica, sans-serif;
}

div {
width: 300px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
}

button {
cursor: pointer;
position: relative;
display: inline-block;
background: rgb(25, 181, 206);
color: #fff;
padding: 5px 15px;
border: none;
font-size: 1em;
}

button::after {
content: "I'm a css tooltip!"; /* <= dynamically title content */
z-index: -999;
position: absolute;
top: 110%;
left: 0;
width: 90%;
padding: 5px 5%;
font-size: 70%;
text-align: center;
color: #fff;
background: rgb(21, 151, 171);
opacity: 0;
-webkit-transform: rotateX(180deg) rotateY(20deg) rotateZ(20deg);
-moz-transform: rotateX(180deg) rotateY(0deg) rotateZ(0deg);
-o-transform: rotateX(180deg) rotateY(0deg) rotateZ(0deg);
-ms-transform: rotateX(180deg) rotateY(0deg) rotateZ(0deg);
transform: rotateX(180deg) rotateY(0deg) rotateZ(0deg);
-webkit-transform-origin: 100% 0%;
transform-origin: 100% 0%;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
transition: cubic-bezier(0.3, 0.05, 0.4, 1.7) .55s;
-webkit-transition: cubic-bezier(0.3, 0.05, 0.4, 1.7) .55s;
-moz-transition: cubic-bezier(0.3, 0.05, 0.4, 1.7) .55s;
-ms-transition: cubic-bezier(0.3, 0.05, 0.4, 1.7) .55s;
-o-transition: cubic-bezier(0.3, 0.05, 0.4, 1.7) .55s;
}

button:hover::after {
opacity: 1;
box-shadow: 0px 2px 3px 0px rgba(0,0,0,0.15);
-webkit-transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg);
-moz-transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg);
-o-transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg);
-ms-transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg);
transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg);
}

<div>
<button title="I'm a css tooltip1!">button</button>
<button title="I'm a css tooltip2!">button</button>
<button title="I'm a css tooltip3!">button</button>
</div>





Demo

There is a way to do that?

Answer

Use data- attribute if you need custom attributes, and without data- if you need non custom attributes (eg title)

* {
   -webkit-font-smoothing: antialiased;
   font-family: "Trebuchet MS", Helvetica, sans-serif;
}

div {
  width: 300px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -webkit-transform: translate(-50%, -50%);
  -moz-transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  -o-transform: translate(-50%, -50%);
}

button {
  cursor: pointer;
  position: relative;
  display: inline-block;
  background: rgb(25, 181, 206);
  color: #fff;
  padding: 5px 15px;
  border: none;
  font-size: 1em;
}
    
button::after {
  content: attr(data-title); /* <= dynamically title content */
  z-index: -999;
  position: absolute;
  top: 110%;
  left: 0;
  width: 90%;
  padding: 5px 5%;
  font-size: 70%;
  text-align: center;
  color: #fff;
  background: rgb(21, 151, 171);
  opacity: 0;
  -webkit-transform: rotateX(180deg) rotateY(20deg) rotateZ(20deg);
  -moz-transform: rotateX(180deg) rotateY(0deg) rotateZ(0deg);
  -o-transform: rotateX(180deg) rotateY(0deg) rotateZ(0deg);
  -ms-transform: rotateX(180deg) rotateY(0deg) rotateZ(0deg);
  transform: rotateX(180deg) rotateY(0deg) rotateZ(0deg);
  -webkit-transform-origin: 100% 0%;
  transform-origin: 100% 0%;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  transition: cubic-bezier(0.3, 0.05, 0.4, 1.7) .55s;
  -webkit-transition: cubic-bezier(0.3, 0.05, 0.4, 1.7) .55s;
  -moz-transition: cubic-bezier(0.3, 0.05, 0.4, 1.7) .55s;
  -ms-transition: cubic-bezier(0.3, 0.05, 0.4, 1.7) .55s;
  -o-transition: cubic-bezier(0.3, 0.05, 0.4, 1.7) .55s;
}
    
button:hover::after {
  opacity: 1;
  box-shadow: 0px 2px 3px 0px rgba(0,0,0,0.15);
  -webkit-transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg);
  -moz-transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg);
  -o-transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg);
  -ms-transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg);
  transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg);
}
<div>
  <button data-title="I'm a css tooltip1!">button</button>
  <button data-title="I'm a css tooltip2!">button</button>
  <button data-title="I'm a css tooltip3!">button</button>
</div>

Jsfiddle link