Back to column formats
Column formattingSharePoint Online

Dynamic Colored Pills

This sample demonstrates how to dynamically add colored pills to lookup columns.

Dynamic Colored Pills formatting preview
Sample preview · View original image

Copy. Apply. Make it yours.

Get this format

1 definition

Review the requirements below, then paste this JSON into the matching formatting panel. Show me where.

Use your list's internal column names and replace any tenant-specific URLs or flow IDs. Formatting is presentation, not a way to secure data.

Column formatting
Download JSON
{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "style": {
    "display": "flex",
    "flex-wrap": "wrap",
    "width": "100%"
  },
  "children": [
    {
      "elmType": "div",
      "style": {
        "margin": "2px auto"
      },
      "children": [
        {
          "elmType": "div",
          "style": {
            "vertical-align": "middle",
            "white-space": "nowrap",
            "width": "auto",
            "padding": "5px",
            "color": "=if( ((((ceiling((((@currentField.lookupValue.length) * pow(@currentField.lookupId, 5)) % 255) / 10) * 10) * 0.299) + (ceiling(((((@currentField.lookupValue.length) * pow(@currentField.lookupId, 5) + @currentField.lookupId ) % 255) / 10) * 10) * 0.587) + ((255 - ceiling(((((@currentField.lookupValue.length) * pow(@currentField.lookupId, 5) + @currentField.lookupId ) % 255) / 10) * 10)) * 0.114)) ) > 145, '#000000', '#ffffff')",
            "background-color": "='rgba(' + (ceiling((((@currentField.lookupValue.length) * pow(@currentField.lookupId, 5)) % 255) / 10) * 10) + ', ' + (ceiling((((@currentField.lookupValue.length) * pow(@currentField.lookupId, 5) + @currentField.lookupId ) % 255) / 10) * 10)  + ', ' + (255 - ceiling(((((@currentField.lookupValue.length) * pow(@currentField.lookupId, 5) + @currentField.lookupId ) % 255) / 10) * 10)) + ', 1)'",
			"border-width": "2px",
            "border-style": "solid",
            "border-color": "='rgba(' + (255 - ceiling((((@currentField.lookupValue.length) * pow(@currentField.lookupId, 5)) % 255) / 10) * 10) + ', ' + (255 - ceiling((((@currentField.lookupValue.length) * pow(@currentField.lookupId, 5) + @currentField.lookupId ) % 255) / 10) * 10)  + ', ' + (ceiling(((((@currentField.lookupValue.length) * pow(@currentField.lookupId, 5) + @currentField.lookupId ) % 255) / 10) * 10)) + ', 1)'",
            "border-radius": "25px"
          },
          "children": [
            {
              "elmType": "span",
              "style": {
                "padding": "8px"
              },
              "txtContent": "@currentField.lookupValue"
            }
          ]
        }
      ]
    }
  ]
}

From the contributor

How to use this sample

Summary

This sample demonstrates how to dynamically add colored pills to lookup columns. If you have a lookup column with more than 10 options or a lookup column that can have additional items added, managing the pill colors can be time-consuming. This format will calculate a color, background, and border color based on the length of the lookup value and the lookupId.

screenshot of the sample

View requirements

This code can be applied to any lookup column

Sample

Solution Author(s)
generic-dynamic-colored-pills.json Larry Pfaff

Version history

Version Date Comments
1.0 July 17, 2024 Initial release
1.1 September 12, 2025 Sample Updates

Disclaimer

THIS CODE IS PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.