SharePoint List JSON - centering text in a list column

Vanessa 356 Reputation points
2023-09-15T14:49:57.3766667+00:00

I created the following JSON column code, but the words are not centered in the color background.

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "style": {
    "box-sizing": "border-box",
    "border-radius": "25px",
    "padding": "0px 15px",
    "text-align": "center"
  },
  "attributes": {
    "class": {
      "operator": ":",
      "operands": [
        {
          "operator": "==",
          "operands": [
            "[$Color]",
            "Green"
          ]
        },
        "sp-css-backgroundColor-BgGreen sp-field-fontSizeSmall sp-css-color-WhiteText",
        {
          "operator": ":",
          "operands": [
            {
              "operator": "==",
              "operands": [
                "[$Color]",
                "Pink"
              ]
            },
            "sp-css-backgroundColor-BgCoral sp-field-fontSizeSmall sp-css-color-BlackText",
            {
              "operator": ":",
              "operands": [
                {
                  "operator": "==",
                  "operands": [
                    "[$Color]",
                    "Red"
                  ]
                },
                "sp-css-backgroundColor-BgRed sp-field-fontSizeSmall sp-css-color-WhiteText",
                {
                  "operator": ":",
                  "operands": [
                    {
                      "operator": "==",
                      "operands": [
                        "[$Color]",
                        "Maroon"
                      ]
                    },
                    "sp-css-backgroundColor-BgDarkRed sp-field-fontSizeSmall sp-css-color-WhiteText",
                    {
                      "operator": ":",
                      "operands": [
                        {
                          "operator": "==",
                          "operands": [
                            "[$Color]",
                            "Gray"
                          ]
                        },
                        "sp-css-backgroundColor-BgGray sp-field-fontSizeSmall sp-css-color-WhiteText",
                        {
                          "operator": ":",
                          "operands": [
                            {
                              "operator": "==",
                              "operands": [
                                "[$Color]",
                                "Orange"
                              ]
                            },
                            "sp-css-backgroundColor-BgOrange sp-field-fontSizeSmall sp-css-color-WhiteText",
                            {
                              "operator": ":",
                              "operands": [
                                {
                                  "operator": "==",
                                  "operands": [
                                    "[$Color]",
                                    "Lime"
                                  ]
                                },
                                "sp-css-backgroundColor-BgSage sp-css-borderColor-SageFont sp-field-fontSizeSmall sp-css-color-BlackText",
                                {
                                  "operator": ":",
                                  "operands": [
                                    {
                                      "operator": "==",
                                      "operands": [
                                        "[$Color]",
                                        "Purple"
                                      ]
                                    },
                                    "sp-css-backgroundColor-BgPurple sp-field-fontSizeSmall sp-css-color-WhiteText",
                                    {
                                      "operator": ":",
                                      "operands": [
                                        {
                                          "operator": "==",
                                          "operands": [
                                            "[$Color]",
                                            "Teal"
                                          ]
                                        },
                                        "sp-css-backgroundColor-BgTeal sp-field-fontSizeSmall sp-css-color-WhiteText",
                                        {
                                          "operator": ":",
                                          "operands": [
                                            {
                                              "operator": "==",
                                              "operands": [
                                                "[$Color]",
                                                "Gold"
                                              ]
                                            },
                                            "sp-css-backgroundColor-BgGold sp-field-fontSizeSmall sp-css-color-BlackText",
                                            {
                                              "operator": ":",
                                              "operands": [
                                                {
                                                  "operator": "==",
                                                  "operands": [
                                                    "[$Category]",
                                                    ""
                                                  ]
                                                },
                                                "sp-css-backgroundColor-BgLightBlue sp-css-color-LightBlueFont",
                                                ""
                                              ]
                                            }
                                          ]
                                        }
                                      ]
                                    }
                                  ]
                                }
                              ]
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  },
  "txtContent": "[$Color]"
}
Microsoft 365 and Office | SharePoint | For business | Windows
{count} votes

Accepted answer
  1. Yanli Jiang - MSFT 31,596 Reputation points Microsoft External Staff
    2023-09-18T07:44:55.2766667+00:00

    Hi @Vanessa ,

    It seems that the text is not centered because of the padding applied to the style.

    You can try adding the following style property to the div element to center the text:

    "display": "flex",
    "justify-content": "center",
    "align-items": "center"
    

    This should center the text both horizontally and vertically:

    09181

    Hope this helps. :-)


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    3 people found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.