How AI is powering better personalization in e-commerce [with Vercel]. Sign up now

Configure inline images in Portable Text with predefined alt text and caption

31 repliesLast updated: Nov 29, 2025

Newbie Question: Is it possible to configure an image object in Sanity Studio so editors can select and insert an image into a portable text block as an inline image and have the alt text and caption predefined? The perfect scenario would be if the caption and alt text were set when the image is first uploaded, then editors have the ability to override the original values where required in subsequent uses of the image.
AI Update

Yes, it is possible to configure an image object in Sanity Studio so editors can insert images into portable text with predefined alt text and captions! Here's how this works:

Adding Custom Fields to Images

You can add custom fields like alt and caption directly to your image schema within the portable text block definition:

import {defineField, defineArrayMember} from 'sanity'

defineField({
  name: 'body',
  title: 'Body',
  type: 'array',
  of: [
    {type: 'block'},
    defineArrayMember({
      type: 'image',
      options: {
        hotspot: true
      },
      fields: [
        {
          name: 'alt',
          title: 'Alt Text',
          type: 'string',
          description: 'Important for SEO and accessibility'
        },
        {
          name: 'caption',
          title: 'Caption',
          type: 'string'
        }
      ]
    })
  ]
})

About Asset-Level Metadata

While Sanity's image asset documents store metadata like dimensions and color palette automatically, image assets themselves don't have built-in altText or caption fields. These fields need to be defined at the document level (as shown above).

Creating Inline Images

For truly inline images (appearing within the text flow rather than as separate blocks), you can create custom inline objects using the of property within your block definition:

defineArrayMember({
  type: 'block',
  of: [
    {
      type: 'image',
      name: 'inlineImage',
      title: 'Inline Image',
      options: {inline: true},
      fields: [
        {name: 'alt', type: 'string', title: 'Alt Text'},
        {name: 'caption', type: 'string', title: 'Caption'}
      ]
    }
  ]
})

The Inheritance Challenge

Your ideal scenario—setting alt text and caption when an image is first uploaded, then allowing overrides in subsequent uses—is not directly supported in Sanity's architecture. Here's why:

Workaround Options

The fields-on-image approach shown above is the standard pattern and gives you the flexibility you need, though it requires editors to fill in these fields each time they use an image.

Show original thread
31 replies

Was this answer helpful?

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Related contributions