loading...
Cover image for CSS Only Tooltips

CSS Only Tooltips

dailydevtips1 profile image Chris Bongers Originally published at daily-dev-tips.com ・2 min read

Today we will be building some tooltips with nothing more than CSS. The idea is we hide the tooltip, and we will show it once hovering over the div.

Note: These tooltips must have a fixed width to work!

HTML Structure

As for the HTML, we use the following setup:

<div class="skills">
  <div class="skills-item">
    <span class="tooltip">HTML5</span>
    <!-- Content -->
  </div>
  <div class="skills-item">
    <span class="tooltip">CSS3</span>
    <!-- Content -->
  </div>
  <div class="skills-item">
    <span class="tooltip">JavaScript</span>
    <!-- Content -->
  </div>
</div>

This will give us a basic grid like setup!

CSS Only Tooltips

As for the CSS which is going to be the main part of this article, we will use some basic styling to style the boxes next to each other.

.skills {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  max-width: 50vw;
  margin: 0 auto;
  height: 100vh;
  align-items: center;
}
.skills-item {
  background: #fff;
  padding: 10px;
  margin: 5px;
  position: relative;
  border-radius: 5px;
}

This is our basic template; we are using a basic grid solution and center the elements.

Let's now style the tooltip:

.skills-item .tooltip {
  width: 100px;
  background-color: #555;
  color: #fff;
  text-align: center;
  padding: 5px 0;
  border-radius: 6px;
  position: absolute;
  z-index: 1;
  bottom: 100%;
  left: 50%;
  margin-left: -50px;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s;
}

The main important parts here lay in the default hidden structure; we also add a transition so it will animate once we hover.

Now we will also add a small triangle in the center, just a touch of extra CSS

.skills-item .tooltip:after {
  content: '';
  position: absolute;
  top: 100%;
  left: 50%;
  margin-left: -5px;
  border-width: 5px;
  border-style: solid;
  border-color: #555 transparent transparent transparent;
}

So now let's move on to making it actually show!

.skills-item:hover .tooltip {
  visibility: visible;
  opacity: 1;
}

That's it. We can now view our awesome tooltip in action.

View it on Codepen.

See the Pen CSS Only Tooltips by Chris Bongers (@rebelchris) on CodePen.

Alt Text

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Posted on by:

dailydevtips1 profile

Chris Bongers

@dailydevtips1

I write daily web development tips that help you become a more efficient developer. 👨‍💻⚡️

Discussion

markdown guide
 
 
 

Very nice, I love how many ways there are to solve one problem.
Definitely going to give the attributes a try

 

Hey, That's a very neat solution man!
How is the browser support on those selectors?

I do guess my solution could be converted to use the dynamic width, but I like your approach!

 

I haven't tested extensively... but caniuse gives pretty wide support:
caniuse.com/#feat=css-gencontent
caniuse.com/#search=attribute%20se...
caniuse.com/#feat=calc

The most problematic one looking to be calc in IE may or may not work correctly

Nice, I'm going to give them a try

Awesome, these selectors are amazing, created the alternative way as well:

dev.to/dailydevtips1/css-only-tool...

 

Why did you take a screenshot of the Codepen instead of embeding it?!

 

It is embedded above, but dev.to doesn't seem to render the codepens. So someone asked me if i could at-least add images so they could see how it looks.

 

You can input a line like this in you markdown

{% codepen https://codepen.io/rebelchris/pen/LYNEYEv %}

and it will render like this in your post

There's plenty of good stuff in the editor guide

Awesome man, It seems to work different on all platforms haha, probably should have read the editor guide yes 🤯