Search by

kiwi / contao-bootstrap

dev@kiwi.de

This Contao bundle implements the possibility to assign bootstrap (css-framework) grid classes to contents and therefore flexibly align them via the CMS.

Package info

github.com/KIWI-Werbeagentur/contao-bootstrap-bundle

Type:contao-bundle

pkg:composer/kiwi/contao-bootstrap

Statistics

Installs: 767

Dependents: 0

Suggesters: 1

Stars: 1

Open Issues: 1

1.2.1 2026-11-21 20:04 UTC

README

Implement the Bootstrap CSS framework into your Contao CMS.

Table of contents

  1. Scope
  2. Setup
    1. Installation
    2. Implementation
      1. Themes
      2. Layouts
      3. Wrapping elements: articles, element groups & modules of type list
      4. Elements: content elements, form fields & modules
    3. Gutters, spacings and gaps
    4. Form templates
    5. Widgets

Scope

This bundle allows you to define bootstrap layout properties within your Contao backend.

Define container widths and flex properties for wrapping elements:

  • articles (tl_article)
  • element groups (tl_content)
  • fieldsets (tl_form_field)
  • forms (tl_form)
  • sections (tl_layout)

Set colspans and flex properties for components:

  • content elements (tl_content)
  • form fields (tl_form_field)
  • modules and their list items (tl_module)

Create responsive fields yourself by using the integrated ResponsiveWidget or OptionalResponsiveWidget.

Setup

Installation

Install the bundle via composer

composer require kiwi/contao-bootstrap

Configuration

Some options can be configured via .env variables:

# Controls which spacing keys appear in the BE dropdowns and the generated SCSS.
# unset / 0  → only the new space-N keys (implicit default — will be migrated to 1 if deprecated values are still in use)
#         1  → only the deprecated keys (legacy default)
#         2  → both sets (for legacy projects mid-migration)
#         3  → only the new space-N keys (explicit opt-in — the migration
#              will not touch this even when stored deprecated values exist)
# The `default` option renders its pre-partial `pt-default` / `pb-default` class
# instead of the partialed one in mode 1, and both in mode 2, so existing project CSS
# keeps matching. See "The `default` class alias" below for why and the removal path.
# Fresh projects leave it unset; the migration writes 1 for projects with stored deprecated values.
#KIWI_BOOTSTRAP_DEPRECATED_SPACINGS=1

# Renders Bootstrap's stock grid (negative row margins, column padding, container padding)
# instead of the marginless one. Written as 1 by the migration on installations upgrading
# from a release before the marginless grid; 0 is an explicit opt-out. Purge the cache and
# the script cache after changing it. See "Legacy grid" below.
#KIWI_BOOTSTRAP_LEGACY_GRID=1

Implementation

Step 1: (Re-)store themes

Go to themes (/contao?do=themes) and create or edit a theme. Choose those bootstrap components that shall be loaded or chose none to load all – in any case you need to restore the theme so that some theme files can be generated.

Step 2: (Re-)store layouts

Afterwards you go to layouts (/contao?do=themes&table=tl_layout&id={{theme_id}}) and create or edit one. Apply container widths to row sections (header, footer), custom sections (by choosing a correspondig template) and sidebars (main area will fill available space). If you don't want to manually load Bootstrap you can activate automatic loading in your CSS-framework selection (recommended).

Templates for custom sections:

Template Container Horizontal padding
block_section_container fixed width default (cx-default-main)
block_section_container_flush fixed width none
block_section_container_fluid full width none
block_section_container_fluid_padded full width default (cx-default-main)
block_section_container_header / _footer as the layout's header / footer as the layout's header / footer

Padding is for modules placed directly in a section; articles placed in a section bring their own. The defaults keep the look of earlier versions: a fixed-width container was inset, a full-width one was flush.

CSS-Classes and their styles will now be applied, when you define bootstrap layout properties in your contents (forms, modules, articles & content elements)

Step 3: Use in articles, content element_groups and modules listing elements

Articles are used as bootstrap containers.

  • Container Width: You can choose between container-fluid (whole viewport) and limited width upwards a specifix breakpoint.
  • Elements per row: Defines how many content elements will be shown in one row.
  • Direction [flex-diretion]: Define block (vertical and horizontal) and inline alignment.
  • Alignment [justify-content, align-content, align-items]: Define block (vertical and horizontal) and inline alignment.
  • Wrapping [flex-wrap]: Allow or disallow row breaks.
  • Spacing [padding-top, padding-bottom]: Choose how much distance an article should have to its previous and next sibling (Take a look at the spacing section for further information).

To define further content elements as a wrapper for nested fragments, you can add an entry to $GLOBALS['responsive']['tl_content']['includePalettes']['container'] within your config file.

To define further modules as a wrapper for nested fragments, you can add an entry to $GLOBALS['responsive']['tl_module']['includePalettes']['container'] within your config file.

To define further content elements or modules as having their own custom children, you can add an entry to $GLOBALS['responsive']['tl_content']['includePalettes']['children'] respectively $GLOBALS['responsive']['tl_module']['includePalettes']['children'] within your config file.

Since such an element renders its child items itself, it also provides the element the settings apply to: wrap the items in one element and output items_attributes on it – those attributes are filled with the resolved classes. Own attributes are merged in, as everywhere else:

{% set items_attributes = attrs().mergeWith(items_attributes|default) %}

{% block content %}
    <div{{ items_attributes }}>{# the items #}</div>
{% endblock %}

Step 4: Use in content elements, form fields and modules

  • Overwrite Width: Check to overwrite "Elements per row" setting of parent element.
  • Width: Set elements width.
  • Offset: Define the positioning of the element.
  • Order: Change the order of an element.
  • Alignment: Overwrite "Alignment" setting of parent element.

To remove the settings from a specific content element, add an entry to $GLOBALS['responsive']['tl_content']['excludePalettes']['column'] within your config file.

To remove the settings from a specific module, add an entry to $GLOBALS['responsive']['tl_module']['excludePalettes']['column'] within your config file.

To remove the settings from a specific form field, add an entry to $GLOBALS['responsive']['tl_form_field']['excludePalettes']['column'] within your config file.

Gutters, spacings and gaps

Gutters, container padding, row gaps and the vertical spacing of articles and element groups use a value-derived scale: space-N is N × 0.25rem (space-4 = 1rem, space-12 = 3rem). Which steps each setting offers, and its defaults, are configured per subsystem under kiwi_bootstrap.grid (gutter, container-padding-x, row-gap, vertical-spacing):

kiwi_bootstrap:
    grid:
        gutter:
            options: ['-space-2', 'space-16']   # additive: "-key" removes a shipped option, a plain key adds one
            defaults:
                default: space-6                # generic default
                header: space-4                 # per section (partial)
                footer: { xs: space-4, lg: space-6 }   # responsive: xs is required

Bootstrap's $spacers map is separate: it drives Bootstrap's own utilities (.p-*, .m-*, .gap-*, .g*-*) and the backported .row-gap-*. The bundle redefines keys 1, 2 and 5 and adds 6–10 (see assets/customization/extend-spacers.scss), so these utilities differ from stock Bootstrap. You can modify and add values via $modify-spacers, or completely customize the scale by overwriting $spacers.

Legacy grid

The bundle replaces Bootstrap's negative-margin grid with a marginless one: rows space their columns with column-gap, columns carry no padding, and containers only pad themselves through the container padding setting (.cx-*). How far content used to be inset came from the CSS rather than from any setting (theme rules padding columns, backgrounds reaching past the container padding, nested containers cancelling each other's padding), so it cannot be migrated reliably from the stored records.

Installations upgrading from a release before the marginless grid therefore keep Bootstrap's stock grid: on the first contao:migrate the bundle writes KIWI_BOOTSTRAP_LEGACY_GRID=1 to .env.local (detected by tl_content having the bundle's responsiveRowCols column but not yet responsiveContainerPaddingX; a first-time install of the bundle is not affected). In that mode

  • assets/customization/grid-overrides.scss is not loaded, so rows, columns, offsets and row-cols behave like stock Bootstrap
  • containers keep Bootstrap's max-widths (the marginless grid reduces them by the dropped padding)
  • the .cx-* rules are not emitted, so the container padding setting has no effect
  • the nested-container padding reset and .row.in-grid-bg from earlier releases are active again

The spacing scale, row gap and $spacers are the same in both modes; the gutter setting applies as column padding instead of a gap.

The container padding is back-filled in legacy mode too; the values have no effect until the project switches.

Switching to the marginless grid (or back) is a per-project step: set the variable to 0 (or 1), regenerate the styles and review every page. Theme rules that pad columns now add to the gap, and content of fluid containers may become flush. Regenerating takes both the Symfony cache and Contao's script cache, as the compiled layout CSS does not notice the regenerated imports:

vendor/bin/contao-console cache:clear
vendor/bin/contao-console contao:automator purgeScriptCache

Legacy spacings (deprecated)

The named options none, gap, gap-half, xxs, xs, sm, md, lg, xl and xxl are deprecated in favour of the space-N scale and will be removed in a future major release. Existing installations that still rely on them can re-enable the legacy set via KIWI_BOOTSTRAP_DEPRECATED_SPACINGS.

default is not deprecated: it stays available in every mode as the dynamic default, resolving to the configured kiwi_bootstrap.grid.vertical-spacing default of the field's partial. Only its old, unpartialed class name is on the way out - see below.

The default class alias

default is the one spacing option that predates the grid subsystem layer. It used to render as pt-default / pb-default; now that the option is partial-aware it renders as pt-default-articleTop / pb-default-articleBottom (and the groupTop / groupBottom equivalents for element groups). Project CSS written against the old spelling silently stops matching.

Which spelling an installation gets is decided by the mode:

mode rendered class
0 / 3 pt-default-articleTop (new name only)
1 pt-default (old name only)
2 both, e.g. pt-default-articleTop pt-default

Mode 1 renders the old name instead of, not alongside, the new one. That is deliberate: the partialed class sets --spacing-top / --spacing-bottom, and custom properties inherit, so any descendant carrying [data-spacing-top] without a spacing class of its own picks the value up and applies it — which is how background articles are wired, the classes on the wrapper and the attributes moved to mod_article__main. On a legacy install those variables were never set, so that indirection resolved to nothing; emitting both names revives it and adds spacing a project's own rules never covered, because those only ever matched the class.

This is a migration crutch and will be removed. One legacy class cannot express two partials: while the alias is active and a project styles .pt-default / .pb-default with !important (the usual legacy pattern, as that beats the --spacing-top / [data-spacing-top] indirection outright), setting --kiwi-vertical-spacing-default-articleTop differently from --kiwi-vertical-spacing-default-articleBottom will have no visible effect.

Migrating off the alias

  1. Extend your selectors to match both spellings, so the CSS is correct under either mode:
    .your-selector {
      @each $breakpoint in map-keys($grid-breakpoints) {
        @include media-breakpoint-up($breakpoint) {
          $infix: breakpoint-infix($breakpoint, $grid-breakpoints);
    
          > .pt#{$infix}-default,
          > .pt#{$infix}-default-articleTop { --spacing-top: 0; }
        }
      }
    }
    (and the -articleBottom counterpart for pb, setting --spacing-bottom). Override --spacing-top / --spacing-bottom, the variables the spacing classes set and [data-spacing-*] applies as padding.
  2. Drop the old spelling once nothing references it.
  3. Set KIWI_BOOTSTRAP_DEPRECATED_SPACINGS=3 to opt out of the alias and the deprecated buckets for good.

The alias disappears with the deprecated named buckets in the next major release, when modes 1 and 2 are removed.

For simple customization of the legacy options, you can overwrite the following variables in you (s)css file. The value of default is not among them: set it via kiwi_bootstrap.grid.vertical-spacing.defaults, or override --kiwi-vertical-spacing-default-<partial> (articleTop, articleBottom; groupTop / groupBottom only exist when their default isn't noop, which it is out of the box).

:root {
  --spacing-none: your_size; /* "0" recommended */
  --spacing-gap: your_size; /* grid gutter width recommended */
  --spacing-gap-half: your_size; /* half of grid gutter width recommended */
  --spacing-xxs: your_size;
  --spacing-xs: your_size;
  --spacing-sm: your_size;
  --spacing-md: your_size;
  --spacing-lg: your_size;
  --spacing-xl: your_size;
  --spacing-xxl: your_size;
}

Form templates

Every form template ships both as .html.twig and as .html5, except form_fieldsetStop: it only closes the markup opened by form_fieldsetStart, so the bundle ships just the .html5 and the Twig variant comes from contao-responsive-base. Both sets are required — do not drop the legacy one while projects may still override form templates as .html5.

Contao decides per widget whether to render the Twig or the legacy chain, and a single legacy override anywhere in that chain decides for the whole of it. Most form templates extend another identifier (form_row, form_row_double, form_textfield); when that identifier resolves to an .html5, ContaoFilesystemLoader substitutes the legacy source and the rest of the chain renders as legacy templates (on Contao 5.7, Widget::renderLegacyFromTwig() restarts legacy inheritance from the widget's own strTemplate; on 5.3 the .html5 is rendered through a proxy). From there on only .html5 files are consulted, so a missing one falls through to the core-bundle template without any error.

That is rarely harmless. form_text.html5 exists solely to re-route text fields to form_textfield; core's version extends form_row instead and emits a bare class="text", silently dropping every class the project's own form_textfield adds.

The legacy templates render pre-Twig markup and are not guaranteed to carry every feature added to the Twig set. A project that wants those should migrate its own form overrides to Twig, which keeps the chain in Twig end to end.

Widgets

There a different types of responsive widgets allowing you to adjust a setting for different viewports (Values will be inherited from smaller to bigger device). When using them, define the input type that shall be replicated as 'responsiveInputType'.

Fully Responsive Widget [responsive]

Directly shows a setting for every viewport

Example usage:

$GLOBALS['TL_DCA'][{your table}]['fields'][{your first field}}] = [
    'inputType' => 'responsive',
    'responsiveInputType' => 'select',
    'options_callback' => [{your first options}, {your second option}}],
    'sql' => "blob NULL"
];

Optionally Responsive Widget [optionalResponsive]

Only shows a global setting but can be set responsively by activating the checkbox.

Example usage:

$GLOBALS['TL_DCA'][{your table}]['fields'][{your second field}}] = [
    'inputType' => 'optionalResponsive',
    'responsiveInputType' => 'text',
    'sql' => "blob NULL"
];