0

I am trying to show an image in a tooltip on hover. I found this solution, which is awesome and I need just something like this:

Codepen

CSS:

@import "compass";

* {
  box-sizing: border-box;
}

.tooltip {
  position: relative;
  border-bottom: 1px dotted #ccc;
  cursor: help;

  &:before, &:after {
    @include single-transition($duration: .6s);
    visibility: hidden;
  }

  &:before {
    @include opacity(0);
    position: absolute;
    bottom: 0;
    left: 50%;
    margin-left: -6px;
    content: '\00a0';
    height: 0;
    width: 0;
    border-bottom: 6px solid #363636;
    border-left: 6px solid transparent;
    border-right: 6px solid transparent;
  }

  &:after {
    @include opacity(0);
    @include border-radius(4px);
    position: absolute;
    content: attr(title);
    top: 100%;
    left: -12px;
    width: 100%;
    min-width: 120px;
    margin: 0;
    padding: 8px 10px;
    background: #363636;
    color: #fff;
    font-size: 1.2rem;
    font-weight: normal;
    text-align: center;
  }

  &:hover {

    &:after, &:before {
      @include opacity(1);
      visibility: visible;
    }

    &:before {
      bottom: -6px;
    }

    &:after {
      margin-top: 6px;
    }
  }
}

// Demo

html {
  font-size: 62.25%;
}

body {
  font-size: 1.6rem;
}

h1 {
  font-size: 2.4rem;
}

.wrapper {
  max-width: 32em;
  padding: 12px;
  margin: 0 auto;
  font-family: sans-serif;
  line-height: 1.4;
}

HTML:

<div class="wrapper">
  <h1 class="tooltip" title="Tooltip">Pure CSS Tooltip</h1>
  <p><span class="tooltip" title="I'm a Tooltip">Lorem ipsum dolor sit amet</span> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Numquam molestiae, quis accusamus fugiat officiis amet ullam, inventore vero! Officia iusto totam quis amet consequatur numquam nesciunt alias deleniti eos quas?<span class="tooltip" title="Another Tooltip">
  Lorem ipsum</span> dolor sit amet, consectetur adipisicing elit. Beatae ut nihil labore at reiciendis placeat modi, Lorem ipsum dolor sit ameteum quia delectus similique accusamus sed soluta sunt aliquid repudiandae assumenda a illo molestiae.
    <span class="tooltip" title="One more Tooltip">Lorem ipsum</span> dolor sit amet, consectetur adipisicing elit. Deserunt incidunt et earum repellendus, maxime neque voluptate molestias quod at illo maiores tempora suscipit, deleniti ipsum nesciunt, animi tempore, esse cum?
  </p>
</div>

But the problem is title attribute can only be text, and I need to show an image. Can this be altered, and if so, how? Or is there another solution?

Thank you in advance.

PS: Sorry if it sounds as a stupid question, as its my first question at Stackoverflow.

vivekkupadhyay
  • 2,851
  • 1
  • 22
  • 35
Malmannim
  • 1
  • 2

3 Answers3

0

you can do this one:

css:

* {
  box-sizing: border-box;
}

.tooltip {
  position: relative;
  border-bottom: 1px dotted #ccc;
  cursor: help;
}

:before, :after {
  visibility: hidden;
}

:before {
  position: absolute;
  bottom: 0;
  left: 50%;
  margin-left: -6px;
  content: '\00a0';
  height: 0;
  width: 0;
  border-bottom: 6px solid #363636;
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
}

:after {
  position: absolute;
  content: attr(title);
  top: 100%;
  left: -12px;
  width: 100%;
  min-width: 120px;
  margin: 0;
  padding: 8px 10px;
  background: #363636;
  color: #fff;
  font-size: 1.2rem;
  font-weight: normal;
  text-align: center;
}

:hover:after, :hover:before {
  visibility: visible;
}

:hover:before {
  bottom: -6px;
}

:hover:after {
  margin-top: 6px;
}

html {
  font-size: 62.25%;
}

body {
  font-size: 1.6rem;
}

h1 {
  font-size: 2.4rem;
}

.wrapper {
  max-width: 32em;
  padding: 12px;
  margin: 0 auto;
  font-family: sans-serif;
  line-height: 1.4;
}

DEMO

Ivin Raj
  • 3,448
  • 2
  • 28
  • 65
0

Welcome to Stackoverflow! Unfortunately there is no chance to put an image to the title attribute. You should use a js library like jquery.ui. Refer to this post.

Community
  • 1
  • 1
m1crdy
  • 1,371
  • 2
  • 25
  • 58
0

If you're more focused on customizing the tooltip then you should go for

jQuery-powertip

and since you're seeking for image tooltip then follow this example.

Here's my JSFiddle

HTML:

<div id="image_tooltip">
   <div>1914 translation by H. Rackham</div>
</div>

CSS:

#image_tooltip div {
   background-color: #EEE;
   border: 1px solid #CCC;
   border-radius: 3px;
   text-align: center;
   width: 400px;
   padding: 40px;
}

jQuery(you've to include jQuery Lib as well as powertip jQuery&CSS:

/*image_tooltip examples*/
$('#image_tooltip div').data('powertip', $([
    '<img src="https://lh3.googleusercontent.com/-5khUd9Qwc-o/AAAAAAAAAAI/AAAAAAAAAZ0/IUp786Zkfgc/photo.jpg?sz=48" />'].join('\n')));
$('#element div').powerTip({
    followMouse: true
 });

Following the instruction you'll get your tooltip like:

enter image description here

you can easily customize its position, tooltip behavior as well as its looks.

Hope this will help you.

vivekkupadhyay
  • 2,851
  • 1
  • 22
  • 35