0

I try to create a CSS-only IMG-Tooltip generated from an attribute value. I tried the following code

<img src="blabla.jpg" data-caption="this is my caption" />

and the CSS-Code:

img {
  position: relative;
}
img:after {
  content: attr(data-caption);
  position: absolute;
  bottom: 2em;
  left: 2em;
  z-index: 2
}

Such a way is no problem with div containers. But how can I get this working on images?

Thank you, Thomas

1 Answers1

0

Well you have multiple ways to create a tooltip using CSS but here is a small example to create tooltip for image.

In below example I have use title attribute of html which also works kind of similar.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/42/Shaqi_jrvej.jpg/1200px-Shaqi_jrvej.jpg" title="imgae">



  
</body>
</html>

You'll find more useful answer here : Answers

Let me know if you find this code helpful.

  • I am looking for a way to realize it without any further html markup. As mentioned before it works with other elements like divs. IMG-Tags does not seem to recognize :after pseudo elements. -> The markup is used in a cms and we can't control the output markup. – tkwebreform Oct 25 '21 at 20:27