0

I'm trying to set up custom JSON formatting with an image that is clickable to follow a link. I can display the image OK (using "elmType": "img"), and I can set up text that links OK (using "elmType": "a"), but I can't seem to combine the two.

The following code I think shows my intent, but does not create the link, rather it only displays the image:

{
  "elmType": "img",
  "style": {
    "display": "block",
    "background-color": "#abeccd",
    "position": "relative",
    "width": "225px",
    "height": "225px"
  },
  "attributes": {
    "src": "[$ImageLink]",
    "target": "_blank",
    "href": "http://www.google.co.uk"
  }
}
1
  • You have to nest two elements in JSON to create clickable image Commented Mar 15, 2023 at 10:31

1 Answer 1

0

You have to nest two elements (img element inside a element) in JSON to create clickable image.

Example:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "a",
  "txtContent": "",
  "attributes": {
    "target": "_blank",
    "href": "[$RedirectLink]"
  },
  "style": {
    "text-decoration": "none"
  },
  "children": [
    {
      "elmType": "img",
      "style": {
        "display": "block",
        "background-color": "#abeccd",
        "position": "relative",
        "width": "50px",
        "height": "50px"
      },
      "attributes": {
        "src": "[$ImageLink]"
      }
    }
  ]
}

Output:

enter image description here

1
  • 1
    Thank you, this worked perfectly. In fact I used the same principle to turn the entire div, including the image, into link (this is what I needed ideally).
    – gvmcd
    Commented Mar 15, 2023 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.