2024-08-27 Web Development
How to Use Images in MDX or Markdown Files
By O Wolfson
Whether you’re writing a blog post, documentation, or any other content, knowing how to properly include and manage images is crucial. In this article, we'll explore how to use images in Markdown and MDX files effectively.
1. Using Images in Markdown
Basic Image Syntax in Markdown
In Markdown, the most straightforward way to include an image is by using the following syntax:
- Alt text: Describes the image and is displayed if the image cannot be loaded.
- Path to the image: Can be a relative path (e.g.,
./images/pic.jpg
) or an absolute URL (e.g.,https://example.com/image.jpg
).
Example:
This will display an image with the alternative text "A beautiful sunset" if the image fails to load.
Adding Titles to Images
You can also add a title to your images, which appears as a tooltip when you hover over the image:
Example:
This syntax adds a tooltip that appears when users hover over the image.
Including Captions for Images
Captions can be added directly below the image in Markdown:
trees, grass, and sky
Using Images with External URLs
You can also use images hosted on external sites by providing the full URL:
Example:
This method is handy when you want to include images that are not part of your local project or when you want to link to external resources.
Images in Next.js Public Folder
In Next.js, you can place your images in the public
folder, which allows you to reference them directly from the root of the application. For example:
The image above exists in the public
folder, which is not processed by webpack. This makes it ideal for static assets like images.
2. Using Images in MDX Files
MDX (Markdown with JSX) is a powerful way to use React components inside markdown files. While the basic Markdown image syntax works in MDX, you can take advantage of React components for greater flexibility.
Basic Markdown Image in MDX
Using Next.js Image
Component in MDX
MDX allows you to use the Next.js Image
component to optimize and handle responsive images:
This method is particularly useful if you need to optimize images, handle responsive images, or include advanced functionality provided by the Image
component.
Custom Image Component in MDX

You can create a custom image component to handle additional functionality like captions and titles:
If you're using MDXBlog, this custom <Image />
component should already be available via the mdx-components.tsx file, so you don't need to import it manually.
Handling Images Responsively in MDX
When using the next/image
component or a custom component, you can manage responsiveness by setting the layout
to responsive
:
This ensures that your images scale correctly on different screen sizes, improving user experience across devices.
3. Best Practices for Using Images
- Optimize Images: Use tools like ImageOptim or modern formats like WebP to reduce file sizes and improve loading times.
- Always Include Alt Text: Descriptive alt text improves accessibility and SEO.
- Ensure Responsiveness: Test your images on different devices to ensure they display properly.
- Organize Your Images: Keep your images in a dedicated folder (e.g.,
/public/images/
) for easy reference. - Use Next.js Features: Take advantage of the Next.js
Image
component for built-in optimization and lazy loading.
Conclusion
Start with basic Markdown syntax, and for more advanced use cases, leverage the power of MDX and React components like Next.js's Image
. Following best practices for image optimization, accessibility, and responsiveness ensures your content is both user-friendly and visually appealing.