Sp-dev-docs: What is the JSON syntax to format > center-align text in a SharePoint column

Created on 24 Aug 2018  路  4Comments  路  Source: SharePoint/sp-dev-docs

Category

  • [X] Question
  • [ ] Typo
  • [ ] Bug
  • [ ] Additional article idea

Hello,

I've used the below piece of code to hightlight a date in the past (column name is 'Due Date') in a SharePoint Online list. It works OK (background color is red for past dates, and green for the rest). The text, however is not centered (nor horizontally, nor vertically) even though I've included this:
"text-align": "center", "vertical-align": "center",

What is it that I am missing? Many thanks in advance.

{ "$schema": "http://columnformatting.sharepointpnp.com/columnFormattingSchema.json",
"elmType": "div",
"debugMode": "true",
"txtContent": "@currentField",
"style": {
"text-align": "center",
"vertical-align": "center",
"background-color": "=if([$Due_x0020_Date] <= @now, '#ffcccc', '#ccffcc'"
}
}

question

Most helpful comment

This should work for you:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
  "elmType": "div",
  "style": {
    "background-color": "=if([$Due_x0020_Date] <= @now, '#ffcccc', '#ccffcc')",
    "display": "table",
    "width": "100%"
  },
  "children": [
    {
      "elmType": "div",
      "txtContent": "@currentField",
      "style": {
        "display": "table-cell",
        "text-align": "center",
        "vertical-align": "middle"
      }
    }
  ]
}

Please let me know if you have any other questions, thanks!

All 4 comments

This should work for you:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
  "elmType": "div",
  "style": {
    "background-color": "=if([$Due_x0020_Date] <= @now, '#ffcccc', '#ccffcc')",
    "display": "table",
    "width": "100%"
  },
  "children": [
    {
      "elmType": "div",
      "txtContent": "@currentField",
      "style": {
        "display": "table-cell",
        "text-align": "center",
        "vertical-align": "middle"
      }
    }
  ]
}

Please let me know if you have any other questions, thanks!

It dit. Perfectly well. Many thanks!

Tkx

Issues that have been closed & had no follow-up activity for at least 7 days are automatically locked. Please refer to our wiki for more details, including how to remediate this action if you feel this was done prematurely or in error: Issue List: Our approach to locked issues

Was this page helpful?
0 / 5 - 0 ratings

Related issues

StfBauer picture StfBauer  路  3Comments

karishmaTCS picture karishmaTCS  路  3Comments

jonthenerd picture jonthenerd  路  3Comments

christianbueschi picture christianbueschi  路  3Comments

StfBauer picture StfBauer  路  3Comments