Tooltip for images?

Resurfacing this question from about a year ago… is there a way to make the image caption appear as a tooltip when hovering over an image? Alternatively, something similar to the way help works in other widgets.

yes, you can do this with the html_content component and css

# Importing Streamlit
import streamlit as st

# Define HTML and CSS for the tooltip
html_content = """
<style>
.tooltip {
  position: relative;
  display: inline-block;
  cursor: pointer;
}

.tooltip .tooltiptext {
  visibility: hidden;
  width: 120px;
  background-color: black;
  color: white;
  text-align: center;
  border-radius: 6px;
  padding: 5px 0;
  position: absolute;
  z-index: 1;
  top: 100%;
  left: 50%;
  margin-left: -60px;
  opacity: 0;
  transition: opacity 0.3s;
}

.tooltip:hover .tooltiptext {
  visibility: visible;
  opacity: 1;
}
</style>

<div class="tooltip">
  <img src="https://cdn.vox-cdn.com/thumbor/WR9hE8wvdM4hfHysXitls9_bCZI=/0x0:1192x795/1400x1400/filters:focal(596x398:597x399)/cdn.vox-cdn.com/uploads/chorus_asset/file/22312759/rickroll_4k.jpg" alt="Image" style="width:200px;height:auto;">
  <span class="tooltiptext">Tooltip text</span>
</div>
"""

# Display HTML in Streamlit
st.markdown(html_content, unsafe_allow_html=True)

1 Like

Thanks for this suggestion. It’ll work for sure, however I was hoping to find a solution which will work on the st.image widget, rather than st.markdown. Much appreciated.