Building a simple tooltip component for Blazor in under 10 lines of code*
Tags: blazor, webdev, csharp, dotnet
Published: 2020-10-07 10:39:16

---
title: Building a simple tooltip component for Blazor in under 10 lines of code*
published: true
date: 2020-10-06 14:32:27 UTC
tags: Blazor,WebDev,CSharp,DotNet
canonical_url: https://chrissainty.com/building-a-simple-tooltip-component-for-blazor-in-under-10-lines-of-code/
---

 ![Building a simple tooltip component for Blazor in under 10 lines of code*](https://chrissainty.com/content/images/2020/10/simple-tooltip-component-for-blaor-in-under-10-lines-of-code.jpg)

It has been so long since I've managed to get some time to write a blog post. Since my last post, I've been working hard writing the first few chapters for my book, Blazor in Action – which should be out on MEAP (Manning Early Access Program)  very very soon. My wife and I also had our first child at the start of September which has been an amazing and interesting new challenge to take on. I'm loving every minute so far but I'm sooo thankful for my coffee machine!!

Anyway, back to the subject in hand, in this post I'm going to be showing you how you can build a really simple, reusable, tooltip component for your Blazor applications. I've used a bit of artistic license with the title of this post, while it's less than 10 lines of Razor, that doesn't include the CSS. But as the CSS will be minified at some point I don't think it should count.... Anyway, moving on.

If you're just interested in the result, then you can [check out the GitHub repo](https://github.com/chrissainty/SimpleBlazorTooltip) which has the final working version of the code in this post. I also want to point out that I've used the .NET5 preview bits to build this so you will need those installed to run the sample.

## Creating the Tooltip component

The first thing we're going to do is create a new component called _Tooltip.razor_ and add the following code:

<!--kg-card-begin: markdown-->
```
<div class="tooltip-wrapper">
    <span>@Text</span>
    @ChildContent
</div>

@code {
    [Parameter] public RenderFragment ChildContent { get; set; }
    [Parameter] public string Text { get; set; }
}

```
<!--kg-card-end: markdown-->

The component has two parameters, `ChildContent` and `Text`. When we use the component, it's going to wrap the markup where we want the tooltip to display. The wrapped markup will be captured by the `ChildContent` parameter and displayed inside the main `div` of our tooltip. The other parameter `Text`, is what we will use to set the text that the tooltip will display.

That's all there is in terms of Razor code! Not bad, 9 lines including a space. The real magic to making this all work is in the CSS. I'm using the new .NET5 RC1 bits and with that I can take advantage of Blazor's new CSS isolation feature.

## Adding the CSS

To use CSS isolation in Blazor we need to create a CSS file with the same name as the component the styles are used by. In our case the component is called, _Tooltip.razor_. So our stylesheet needs to be called _Tooltip.razor.css_. Once this is done we can add the following styles:

<!--kg-card-begin: markdown-->
```
.tooltip-wrapper {
    position: relative;
    display: inline-block;
    border-bottom: 1px dotted black;
    cursor: help;
}

span {
    visibility: hidden;
    position: absolute;
    width: 120px;
    bottom: 100%;
    left: 50%;
    margin-left: -60px;
    background-color: #363636;
    color: #fff;
    text-align: center;
    padding: 5px 0;
    border-radius: 6px;
    z-index: 1;
}

span::after {
    content: "";
    position: absolute;
    top: 100%;
    left: 50%;
    margin-left: -5px;
    border-width: 5px;
    border-style: solid;
    border-color: #555 transparent transparent transparent;
}

.tooltip-wrapper:hover span {
    visibility: visible;
}

```
<!--kg-card-end: markdown-->

The first class, `.tooltip-wrapper` is applied to the container div of the component. It makes the div render as `inline-block` so the document flow isn't disrupted. It also sets it's position as relative. This means we can then absolute position the child span (tooltip text) where we need, relative to the parent div. The final two rules add some styling to show the user there is a tooltip available.

The next set of styles apply to the `span` element, this contains the tooltip text that will be shown. By default, the span is hidden and it's absolutely positioned relative to the parent div. With the styles shown above, the tooltip will be shown above the content which is contained between the start and end tags of the Tooltip component.

The next style is what is called a pseudo element. This is going to add an element to the DOM. What this style does is create a small arrow at the bottom of the tooltip text pointing to the content that the tooltip is wrapping.

The final style will show the span containing the tooltip text whenever the user hovers over the parent div with their cursor.

That's all there is to it!

## Using the Tooltip

In order to use the tooltip, just wrap it around the content you want the user to be able to hover over to show the tooltip text, and add whatever message you want to be displayed. If all goes well it should look something like this...

![Building a simple tooltip component for Blazor in under 10 lines of code*](https://chrissainty.com/content/images/2020/10/simple-tooltip.png)

## Summary

Just to recap, in this post I showed you how you can create a simple reusable tooltip component for your Blazor application. The whole component was only 9 lines of Razor code, require no JavaScript, and about 36 lines of CSS. We also had a chance to use the new CSS isolation feature which will be shipping in .NET 5 which is right around the corner.

This was a much shorter post than I normally write but it's just nice to actually write a blog post again! I hope to be announcing the MEAP for Blazor in Action any day now so as soon as that is available I will have a blog post up about it and how you can get your hands on it.