Responsive design
Wrap utilities in a media query builder. FluentCss prefixes the class names so the browser can apply them conditionally.
Min and max helpers
FluentCss ships with the common breakpoints in Breakpoints
(XSmall 20rem through
XXLarge 96rem), exposed as
MinSm, MinMd,
MinLg … and the matching
Max* helpers. Min is inclusive
(>=) and max is exclusive
(<), so the exact breakpoint belongs to the min side and
the two never overlap.
| Utility | Generated CSS |
|---|---|
MinSm(sm => ...) |
@media (width >= 40rem), class prefix min-sm: |
MinMd(md => ...) |
@media (width >= 48rem), class prefix min-md: |
MinLg(lg => ...) |
@media (width >= 64rem), class prefix min-lg: |
MaxMd(md => ...) |
@media (width < 48rem), class prefix max-md: |
Min(Length.Px(700), x => ...) |
@media (width >= 700px), class prefix min-700px: |
Media(query, prefix, x => ...) |
any query, custom prefix |
<div class="@Css.Grid().GridCols(1).Gap(4)
@Css.MinMd(md => md.GridCols(3))">
...
</div>
<!-- generated classes -->
<!-- grid grid-cols-1 gap-4 min-md:grid-cols-3 -->
The Length type
For anything outside Breakpoints use
Min(Length) and
Max(Length) with any
Length value. A
Length is just a CSS value plus an optional name; the
factories cover the common units and the constructor handles everything else.
| Utility | Generated CSS |
|---|---|
Length.Px(400) |
400px |
Length.Rem(1.5) |
1.5rem |
Length.Em(1.25) |
1.25em |
Length.Percent(80) |
80% |
Length.Vh(100) |
100vh |
Length.Vw(100) |
100vw |
new Length(value, name?) |
any CSS value, optional class name |
Length.Px(400); // "400px"
Length.Rem(1.5); // "1.5rem"
Length.Em(1.25); // "1.25em"
Length.Percent(80); // "80%"
Length.Vh(100); // "100vh"
Length.Vw(100); // "100vw"
new Length("90rem"); // value used as the class fragment
new Length("90rem", "wide"); // "wide" used as the class fragment
To reuse a length, put it in a static class of your own — the same pattern as custom colors. The
Name becomes the class fragment, so a named value gives
a readable class (min-tablet:) instead of the raw value
(min-700px:).
public static class MyLengths
{
public static readonly Length Tablet = new("700px", "tablet");
public static readonly Length Wide = new("90rem", "wide");
}
Css
.Grid()
.GridCols(1)
.Min(MyLengths.Tablet, md => md.GridCols(3))
.Min(MyLengths.Wide, lg => lg.GridCols(4));
// classes: grid grid-cols-1 min-tablet:grid-cols-3 min-wide:grid-cols-4
The name is the identity
Length name it must be unique and stable: two lengths
with the same name are treated as the same class, and the first value registered wins. When the name
is omitted the value is used, which can never collide.
Raw media queries
The Media method is the underlying primitive and accepts
any media query and prefix, for queries that are not width comparisons.
Css.Media("(prefers-color-scheme: dark)", "scheme-dark",
x => x.Background(Color.Slate900).Color(Color.White));
// class prefix scheme-dark:
Modern range syntax
@media (width >= 48rem). Make sure your target
browsers support it, which all current versions of Chrome, Edge, Firefox and Safari do.
Priority follows the breakpoint
Combining with states
Responsive helpers take a full style builder, so states can be nested inside them. The class name lists media before pseudo.
Css.Color(Color.Slate900)
.MinMd(md => md.Color(Color.Blue700)
.Hover(h => h.Color(Color.Blue500)));
// classes:
// text-slate-900
// min-md:text-blue-700
// min-md:hover:text-blue-500