Density and Scopes

Change the density or the theme of one part of a page

A theme sets its theme variables on :root, so they apply to the whole page. You can also set them on a single element. The content of that element then uses the new values, and the rest of the page keeps the theme. For example, you can make a table denser than the rest of the page, or give a panel your brand colors.

Changing the density

To change the spacing and the size of the controls inside an element, add the data-sa-density attribute to it, with one of the following values:

ValueDensity
compactThe theme's density multiplied by 0.8
comfortableThe theme's density multiplied by 1.2

The size of the text stays the same. The cards below use the compact density, the default density and the comfortable density:

<div class="grid w-full items-start gap-6 sm:grid-cols-3">
    <sa-card data-sa-density="compact">
        <sa-card-header>
            <sa-card-title>Lisbon city break</sa-card-title>
            <sa-card-description>data-sa-density="compact"</sa-card-description>
        </sa-card-header>
        <sa-card-content>
            <sa-field>
                <sa-field-label for="lead-compact">Lead traveller</sa-field-label>
                <sa-input id="lead-compact" value="Maria Santos"/>
            </sa-field>
        </sa-card-content>
        <sa-card-footer class="flex-wrap gap-2">
            <sa-button variant="ButtonVariant.Outline">Save draft</sa-button>
            <sa-button>Book trip</sa-button>
        </sa-card-footer>
    </sa-card>
    <sa-card>
        <sa-card-header>
            <sa-card-title>Lisbon city break</sa-card-title>
            <sa-card-description>Default density</sa-card-description>
        </sa-card-header>
        <sa-card-content>
            <sa-field>
                <sa-field-label for="lead-default">Lead traveller</sa-field-label>
                <sa-input id="lead-default" value="Maria Santos"/>
            </sa-field>
        </sa-card-content>
        <sa-card-footer class="flex-wrap gap-2">
            <sa-button variant="ButtonVariant.Outline">Save draft</sa-button>
            <sa-button>Book trip</sa-button>
        </sa-card-footer>
    </sa-card>
    <sa-card data-sa-density="comfortable">
        <sa-card-header>
            <sa-card-title>Lisbon city break</sa-card-title>
            <sa-card-description>data-sa-density="comfortable"</sa-card-description>
        </sa-card-header>
        <sa-card-content>
            <sa-field>
                <sa-field-label for="lead-comfortable">Lead traveller</sa-field-label>
                <sa-input id="lead-comfortable" value="Maria Santos"/>
            </sa-field>
        </sa-card-content>
        <sa-card-footer class="flex-wrap gap-2">
            <sa-button variant="ButtonVariant.Outline">Save draft</sa-button>
            <sa-button>Book trip</sa-button>
        </sa-card-footer>
    </sa-card>
</div>

The example below makes a table of bookings compact, so it fits more rows, and leaves the search above it at the theme's density:

<div class="flex w-full flex-col gap-6">
    <div class="flex flex-wrap items-end gap-3">
        <sa-field class="w-64">
            <sa-field-label for="booking-search">Search bookings</sa-field-label>
            <sa-input id="booking-search" placeholder="Destination or booking number"/>
        </sa-field>
        <sa-button>
            <sa-icon name="search"/>
            Search
        </sa-button>
    </div>

    <section data-sa-density="compact">
        <sa-table>
            <sa-table-header>
                <sa-table-row>
                    <sa-table-head>Booking #</sa-table-head>
                    <sa-table-head>Status</sa-table-head>
                    <sa-table-head>Destination</sa-table-head>
                    <sa-table-head class="text-right">Amount</sa-table-head>
                </sa-table-row>
            </sa-table-header>
            <sa-table-body>
                @foreach (var booking in StaticData.Bookings)
                {
                    <sa-table-row>
                        <sa-table-cell class="font-medium">@booking.Id</sa-table-cell>
                        <sa-table-cell>
                            <sa-badge variant="@(booking.Status == BookingStatus.Cancelled ? BadgeVariant.Destructive : BadgeVariant.Secondary)">
                                @booking.Status.ToString()
                            </sa-badge>
                        </sa-table-cell>
                        <sa-table-cell>@booking.Destination</sa-table-cell>
                        <sa-table-cell class="text-right">@booking.Amount.ToString("N2")</sa-table-cell>
                    </sa-table-row>
                }
            </sa-table-body>
        </sa-table>
    </section>
</div>

Setting a specific density

To set a density other than compact and comfortable, add the data-sa-scope attribute to the element, and set --sa-density on it. We suggest a value between 0.7 and 1.4, the range of the theme builder.

The example below sets --sa-density to 0.7, 1 and 1.4:

<div class="flex w-full flex-col gap-6">
    @foreach (var density in new[] { "0.7", "1", "1.4" })
    {
        <div data-sa-scope style="--sa-density: @density;" class="flex flex-wrap items-center gap-2">
            <sa-badge variant="BadgeVariant.Outline">--sa-density: @density</sa-badge>
            <sa-input class="w-56" placeholder="Where to next?"/>
            <sa-button variant="ButtonVariant.Outline">Flexible dates</sa-button>
            <sa-button>Search trips</sa-button>
        </div>
    }
</div>

--sa-density on a data-sa-scope element replaces the theme's density. A data-sa-density attribute multiplies the density of the element it's in, including a data-sa-scope element's.

Theming part of a page

To give an element its own theme, add the data-sa-scope attribute to it, and set the theme variables on it, either in a style attribute or in your own CSS. Everything inside the element uses them, such as the accent color, the grays, the corner radius and the density.

The example below gives the rewards card a green accent, green-tinted grays and pill-shaped controls and badges, and leaves the card next to it in the theme:

<div class="grid w-full items-start gap-6 sm:grid-cols-2">
    <sa-card>
        <sa-card-header>
            <sa-card-title>Your next trip</sa-card-title>
            <sa-card-description>Kyoto, Japan · 12–19 April</sa-card-description>
        </sa-card-header>
        <sa-card-content class="flex flex-wrap gap-2">
            <sa-badge>Confirmed</sa-badge>
            <sa-badge variant="BadgeVariant.Secondary">2 travellers</sa-badge>
        </sa-card-content>
        <sa-card-footer class="gap-2">
            <sa-button variant="ButtonVariant.Outline">View itinerary</sa-button>
            <sa-button>Check in</sa-button>
        </sa-card-footer>
    </sa-card>

    <sa-card data-sa-scope
             style="--sa-accent: oklch(0.55 0.15 150); --sa-neutral-hue: 150; --sa-neutral-chroma: 0.02; --sa-radius: 9999px; --sa-pills: 1;">
        <sa-card-header>
            <sa-card-title>Voyager Rewards</sa-card-title>
            <sa-card-description>You have 12,400 points to spend on your next adventure.</sa-card-description>
        </sa-card-header>
        <sa-card-content class="flex flex-wrap gap-2">
            <sa-badge>Gold member</sa-badge>
            <sa-badge variant="BadgeVariant.Secondary">2,600 points to Platinum</sa-badge>
        </sa-card-content>
        <sa-card-footer class="gap-2">
            <sa-button variant="ButtonVariant.Outline">How it works</sa-button>
            <sa-button>Redeem points</sa-button>
        </sa-card-footer>
    </sa-card>
</div>

StellarAdmin.UI adjusts the contrast inside the element too. For example, it makes the text on the green accent white or near-black, as it does for the theme's accent.

On this page