View as Markdown
Figure
Display related visual content with an optional caption and responsive alignment.
100%
Loading...
<twig:Figure caption="Bukchon Hanok Village, in Seoul, South Korea" captionAlign="center" style="max-width: 36rem;">
<img
src="https://images.unsplash.com/photo-1535189043414-47a3c49a0bed?auto=format&fit=crop&w=960&q=80"
class="figure-img img-fluid rounded"
alt="Bukchon Hanok Village, in Seoul, South Korea"
>
</twig:Figure>
Installation
php bin/console ux:install figure --kit bootstrap
Install the following Composer dependencies:
composer require twig/extra-bundle twig/html-extra:^3.12.0
Install the following front-end dependencies:
npm install bootstrap@^5.3.0
# ...or with Symfony AssetMapper
php bin/console importmap:require bootstrap bootstrap/dist/css/bootstrap.min.css
Copy the following file(s) into your app:
{# @prop caption string|null The optional caption displayed below the figure content. #}
{# @prop captionAlign 'start'|'center'|'end'|null The horizontal alignment of the caption. #}
{# @block content The figure content, typically a responsive image. #}
{%- props caption = null, captionAlign = null -%}
<figure {{ attributes.defaults({class: 'figure'}) }}>
{%- block content %}{% endblock -%}
{%- if caption is not null %}
<figcaption class="{{ html_classes({
'figure-caption': true,
('text-' ~ captionAlign): captionAlign in ['start', 'center', 'end'],
}) }}">{{ caption }}</figcaption>
{%- endif %}
</figure>
Usage
<twig:Figure caption="Bukchon Hanok Village, in Seoul, South Korea" style="max-width: 36rem;">
<img
src="https://images.unsplash.com/photo-1535189043414-47a3c49a0bed?auto=format&fit=crop&w=720&q=80"
class="figure-img img-fluid rounded"
alt="Bukchon Hanok Village, in Seoul, South Korea"
>
</twig:Figure>
Accessibility
Write image alternative text that describes the image's purpose in context. Avoid repeating the caption verbatim unless both carry distinct information.
Use an empty alt value for a decorative image. The caption remains associated with the figure through the native figure and figcaption elements.
Examples
Default figure
Display a responsive image with a caption below it.
100%
Loading...
<twig:Figure caption="Bukchon Hanok Village, in Seoul, South Korea" style="max-width: 36rem;">
<img
src="https://images.unsplash.com/photo-1535189043414-47a3c49a0bed?auto=format&fit=crop&w=720&q=80"
class="figure-img img-fluid rounded"
alt="Bukchon Hanok Village, in Seoul, South Korea"
>
</twig:Figure>
Caption alignment
Align the caption with Bootstrap's text utilities through the captionAlign prop.
100%
Loading...
<twig:Figure caption="Bukchon Hanok Village, in Seoul, South Korea" captionAlign="end" style="max-width: 36rem;">
<img
src="https://images.unsplash.com/photo-1535189043414-47a3c49a0bed?auto=format&fit=crop&w=720&q=80"
class="figure-img img-fluid rounded"
alt="Bukchon Hanok Village, in Seoul, South Korea"
>
</twig:Figure>
API Reference
<twig:Figure>
| Prop | Type | Default |
|---|---|---|
caption The optional caption displayed below the figure content.
|
string|null |
null |
captionAlign The horizontal alignment of the caption.
|
'start'|'center'|'end'|null |
null |
| Block | Description |
|---|---|
content |
The figure content, typically a responsive image. |