pinn.xu
中

←︎ Blogs

Quarto Image Centering Styles

2026-06-15 4 min read 705 words 中文版

Exploring image centering styles in a Quarto Website and fixing hover preview issues.

contents
  1. 1 Summary
    1. 1.1 Step 1: External CSS
    2. 1.2 Step 2: Page-level Internal CSS
    3. 1.3 Step 3: Image Inline Styles
  2. 2 Controlling Image Size
    1. 2.1 The Traditional Approach
    2. 2.2 Issue 1: Hover Preview Breaks
    3. 2.3 Issue 2: Image No Longer Centers
  3. 3 Centering Both Image and Caption

1Summary

To achieve all of the following in a Quarto Website:

  • Control image width with rem
  • Cross-reference hover preview displays correctly
  • Image centering
  • Image caption centering

Use the approach below.

1.1Step 1: External CSS

css
.quarto-float.quarto-figure.quarto-figure-center.anchored {
    width: 100% !important;
}

Register it in _quarto.yml:

yaml
format:
  html:
	css:
	  - path/to/style.css

1.2Step 2: Page-level Internal CSS

Add a <style> tag at the top of the page (below the YAML block):

markdown
<style>
	figcaption {
		text-align: center;
	}
</style>

1.3Step 3: Image Inline Styles

markdown
![Caption a](URL){#fig-a style="width:20rem;"}
![Caption b](URL){#fig-b style="width:15rem;"}

This post is implemented using the approach above. Click </> Code in the top-right corner of the page to view the full source.


Here is the full walkthrough:

2Controlling Image Size

2.1The Traditional Approach

When writing my Quarto Website, I initially controlled image size using Quarto’s built-in width attribute:

markdown
![Caption](image URL){#fig-title width="15%"}

This approach is clean and intuitive — no need to write inline styles like style="width:15%" — and the results looked good.

Img Width: Percentage
Figure 1: Img Width: Percentage

2.2Issue 1: Hover Preview Breaks

After using cross-references (e.g. @fig-title) in the body text, I noticed the hover preview inherited the width percentage, causing it to display incorrectly.

Issue 1: Hover Preview Broken
Figure 2: Issue 1: Hover Preview Broken

To fix this, I tried switching to rem as the width unit, which behaves consistently regardless of page scaling and hover context.

I first tried:

markdown
![Caption](image URL){#fig-title width="7rem"}

This had no effect — Quarto’s width attribute does not support rem (it accepts px, percentages, etc.).

I then switched to:

markdown
![Caption](image URL){#fig-title style="width:7rem;"}

2.3Issue 2: Image No Longer Centers

A new problem appeared: the image was no longer centered, and setting {fig-align="center"} had no effect.

Issue 2: Image No Longer Centered
Figure 3: Issue 2: Image No Longer Centered

Inspecting with browser DevTools revealed that when style="width:7rem" is set, Quarto also constrains the parent container’s width to 7rem.

Parent Container Width Constraint
Figure 4: Parent Container Width Constraint

Removing the parent container’s width constraint restored image centering.

Centering Restored
Figure 5: Centering Restored

I also tried several other approaches, such as:

markdown
# Attempt 1
:::{#fig-title width="100%"}
![](image URL){style="width:7rem;"}
Caption
:::

# Attempt 2
:::{#fig-title style="width:100% !important;"}
![Caption](image URL){style="width:7rem;"}
:::

# Attempt 3
:::{#fig-title style="text-align:center; width:100% !important;"}
![](image URL){style="width:7rem;"}
Caption
:::

# ...

None of them achieved the desired result.

In the end, I decided to target the parent container directly via CSS, adding this rule to style.css:

css
.quarto-float.quarto-figure.quarto-figure-center.anchored {
    width: 100% !important;
}

Combined with the image syntax:

markdown
![Caption](image URL){#fig-title style="width:7rem;"}

This successfully centered the image while keeping the hover preview intact:

Centering Achieved
Figure 6: Centering Achieved
Hover Preview Working
Figure 7: Hover Preview Working

3Centering Both Image and Caption

After fixing image centering, the caption still defaulted to left-aligned.

While Quarto’s fig-cap-location attribute lets you place the caption at top, bottom, or margin, it does not control alignment.

After testing, the simplest solution is to set alignment directly in the image’s inline style:

markdown
# Method 1

![Caption](image URL){#fig-title style="text-align:center; width:7rem;"}

This requires no extra container and no caption separation.

Other viable approaches:

markdown
# Method 2

:::{#fig-title fig-cap="write caption here"}
![leave empty](image URL){style="text-align:center; width:7rem;"}
:::

# Method 3

:::{#fig-title}
![leave empty](image URL){style="text-align:center; width:7rem;"}
<note: a blank line is required here>
Caption
:::

Method 1 has the cleanest syntax and the best code highlighting; Methods 2 and 3 are more verbose and introduce additional container nesting.

Syntax Highlighting
Figure 8: Syntax Highlighting

To center all captions on a page, add internal CSS instead:

markdown
<style>
	figcaption {
		text-align: center;
	}
</style>

The image syntax stays the same:

markdown
![Caption](<image URL>){#fig-title style="width:7rem;"}

↑↓ moveEnter openEsc close or / opens search