Styling a tooltip
You have full style control over how the tooltip looks. The tooltip container is a div. You can assign your own class name to it with the className property or the setClassName method.
The tooltip container div will have the following default styles:
backgroundColor: #fffcolor: #333padding: 3px 6pxborderRadius: 4pxboxShadow: 0 0 5px rgba(0,0,0,0.3)transform: translate(-50%, 0)
If the center option is set to false then the transform style will not be set.
The other default styles can be removed by setting the theme option to none.
You can also override individual default styles with the styles property or the style() method.
There are four ways to style the tooltip:
- Do nothing and the above default styles are set.
- Create a
divtag that you set as the content for the tooltip. Thatdivtag can either have a CSS class that you attach styles to, or you can assign styles to thedivDOM element when you are creating it. You will probably want to also set thethemetononeto remove the default styles. Note that the tooltip content is set inside the tooltip containerdivthat by default has the above styles applied to it. - Pass an object of styles to the
stylesto style the containerdivtag that wraps the tooltip content. These styles will overwrite the default styles above if you pass the same style name. For example, if you includecolorin yourstylesobject then that will overwrite the default color above. You can also set thethemetononeto completely remove the default styles. - Add a class name to the container
divthat wraps the tooltip content with theclassNameoption and then attach styles to that CSS class name.
Recommended styles
If you end up styling the tooltip with your own custom class, here are some recommended styles:
.MapTooltip {
background: #fff;
box-shadow: 0 0 5px rgb(0 0 0 / 30%);
color: #555;
font-size: 1.2rem;
padding: 4px 8px;
}
.MapTooltip::before {
border: 8px solid transparent;
border-bottom-color: #fff;
content: '';
left: 50%;
position: absolute;
top: -15px; /* Google maps sometimes uses partial pixel values. This pulls the triangle down enough to overlap the tooltip box */
transform: translateX(-50%);
}
You may also need to add an offset for the triangle that gets added above the tooltip with the ::before element. How you set this depends on how you're adding your tooltip. Below is a recommended offset that works well for markers.
offset: [0, 15]