Center text in st.metric

It is really difficult to center the text in st.metric I have tried every css selector I could think of. - Anyone knows how to do it?

import streamlit as st

col_row1_1, col_row1_2, col_row1_3 = st.columns(3)
col_row1_1.metric("text1", value=3434)
col_row1_2.metric("text2", value=3434)
col_row1_3.metric("text3", value=5667567)

# css injection
def _max_width_():
    max_width_str = "max-width: 1900px;"
    st.markdown(
        f"""
    <style>
    .block-container {{
        {max_width_str}
        }}
    .css-ocqkz7 {{
        text-align: center;
        color: #23C802;
        white-space: normal;
    }}
    .css-1r6slb0.e1tzin5v2 {{
        background-color: #D2D1D1;
        border: 2px solid #A9A9A9;
        padding: 3px 3px 3px 30px;
        border-radius: 5px;
    }}
    <style>
    """,
        unsafe_allow_html=True,
    )


_max_width_()
  • Python==3.9
  • streamlit==1.11.0
import streamlit as st

col_row1_1, col_row1_2, col_row1_3 = st.columns(3)
col_row1_1.metric("text1", value=3434)
col_row1_2.metric("text2", value=3434)
col_row1_3.metric("text3", value=5667567)

st.markdown('''
<style>
/*center metric label*/
[data-testid="stMetricLabel"] > div:nth-child(1) {
    justify-content: center;
}

/*center metric value*/
[data-testid="stMetricValue"] > div:nth-child(1) {
    justify-content: center;
}
</style>
''', unsafe_allow_html=True)


Notice: when streamlit update,the css selector maybe change.

2 Likes

Thanks for the quick reply :heart:
How did you find that HTML tag - I use a browser extension CSS Scan to locate selectors but I did not know that tag.

I use google browser,and press F12 to open devTools,in Elements,use Arrow to locate target elements,then analyzing web page structure, use css selector to locate target elements.
I think you may be konw more about css selector, so you can write your own css selector to locate elemetns instead of relying on tools.

Does anyone know how to center the delta value and the little arrow SVG in the st.metric?

It seems like the two are separated divs. I have not been able to center both such that the two elements respect each other. Depending on the amount of digits in the delta label, the respective space can be adjusted by " left: 30% "

import streamlit as st

col_row1_1, col_row1_2, col_row1_3 = st.columns(3)
col_row1_1.metric("text1", value=3434, delta= 666)
col_row1_2.metric("text2", value=3434, delta=666)
col_row1_3.metric("text3", value=5667567, delta=666)

st.markdown('''
<style>
/*center metric delta value*/
div[data-testid="metric-container"] > div[data-testid="stMetricDelta"] > div{
  justify-content: center;
  color: #01cefc;
}

/*center metric delta svg*/
[data-testid="stMetricDelta"] > svg {
  position: absolute;
  left: 30%;
  -webkit-transform: translateX(-50%);
  -ms-transform: translateX(-50%);
  transform: translateX(-50%);
}
</style>
''', unsafe_allow_html=True)

Thanks

Hey guys!

I realise this thread is a little bit old but was hoping to get some help.
I’m trying to center align some metrics, but after using the code provided by @ji_haoran I can’t get it work.

Just to make sure I am doing this correctly:

  • Do I simply have to include that st.markdown code snippet and my st.metrics should appear center aligned?
  • Or do I need to do something else to specifically indicate that I want that css to apply to those elements?

Thanks in advance!

When including CSS in markdown, you need to make sure the CSS string includes the <style>...</style> tags and that you set the unsafe_allow_html=True optional keyword argument for st.markdown. Other than that, it can be anywhere on you page (I usually put my CSS hacks at the end to avoid the extra blank line they create.)

CSS hacks always have a caveat of breaking with version changes, so just be aware. Here’s an alternate solution I posted a while back if you want to try that CSS, too. (It should apply to all st.metrics on the page as written.)