Installation
Pick the package for your hosting model, wire FluentCss up once and start composing styles.
Choose a package
Install the integration package that matches your app. ASP.NET Core MVC and Razor Pages apps also need the middleware package.
| Package | Hosting model | What it gives you |
|---|---|---|
FluentCss |
Blazor, and MVC / Razor Pages |
The CssStyles component, the
<fluent-css /> and theme tag helpers and the
preflight stylesheet. Use it for Blazor static SSR, Blazor Server, Blazor WebAssembly
(standalone) and Blazor Web Apps using the Interactive Server, Interactive WebAssembly or Auto
render mode, as well as for the server-rendered markup of MVC and Razor Pages.
|
FluentCss.Middleware |
ASP.NET Core MVC and Razor Pages |
The stylesheet middleware and the AddFluentCss() /
UseFluentCss() extensions.
|
FluentCss.Core is transitive dependency
FluentCss.Core and bring it in
automatically.
Blazor
Add the Blazor package:
dotnet add package FluentCss
For the shortest syntax add the following to your _Imports.razor.
Importing the static builder means you can start a chain with Css or
Dark directly.
@using static FluentCss.Core.FluentCssBuilder
@using FluentCss.Core
Drop the CssStyles component into your layout. It renders a
<style> block with every class registered while the page is
built.
<!-- MainLayout.razor or App.razor -->
...
<head>
...
<!-- Add Tailwind CSS preflight, optional but strongly recommended -->
<link rel="stylesheet" href="_content/FluentCss/preflight.css" />
<FluentCss.CssStyles />
...
</head>
<body>...</body>
Keep the loading and error styles as plain CSS
CssStyles has emitted its stylesheet. Put those few rules
in a separate plain CSS file (for example
wwwroot/app.css) and link it from
index.html or
App.razor. This documentation site does exactly that:
the loading and error rules live in
wwwroot/css/site.css. You do not need the form
validation or form-floating rules unless you use
EditForm validation.
Interactive rendering
CssStyles is more than a snapshot of the classes seen so far: it
subscribes to the class registry and re-renders its <style>
block whenever a new class is registered. A component that renders later on the client - a dialog, a tab or
anything that only appears after a click - is therefore styled as soon as it renders, with no page reload.
For that to work CssStyles has to live inside the
interactive render boundary. In a Blazor Web App that means the layout, or anything else under
<Routes>, rather than the statically rendered
App.razor head:
<!-- App.razor: the render mode decides where interactivity runs -->
<Routes @rendermode="InteractiveWebAssembly" />
<!-- MainLayout.razor: CssStyles is part of the interactive tree -->
@inherits LayoutComponentBase
<FluentCss.CssStyles />
<main>@Body</main>
Registration follows the process
CssStyles instance and re-emits the stylesheet when that runtime
registers new classes. The Interactive Server, Interactive WebAssembly and Auto render modes are all
supported.
Prefer the script-link path?
CssStyles is the Blazor-native way to emit the stylesheet, but a
server-hosted Blazor app (Blazor Server or a Blazor Web App) can use the same middleware and script-link
setup as MVC and Razor Pages instead. The tag helpers ship in the
FluentCss package next to
CssStyles; the middleware itself is in
FluentCss.Middleware. See the next section for the setup.
MVC and Razor Pages
Add the FluentCss package for the tag helpers and the
FluentCss.Middleware package for the middleware:
dotnet add package FluentCss
dotnet add package FluentCss.Middleware
Register the middleware in Program.cs. It serves the current hash at
/fluentcss/hash and the stylesheet at
/fluentcss/{hash}.css, with gzip and an ETag.
builder.Services.AddFluentCss();
var app = builder.Build();
app.UseRouting();
app.UseFluentCss(); // serve /fluentcss/hash and /fluentcss/{hash}.css
app.UseAuthorization();
app.MapStaticAssets();
app.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
Razor Pages apps map their pages instead with app.MapRazorPages();
the middleware and the tag helper are identical for both models.
Register the tag helper and the namespaces in _ViewImports.cshtml
(or Pages/_ViewImports.cshtml):
@using static FluentCss.Core.FluentCssBuilder
@using FluentCss.Core
@addTagHelper *, FluentCss
Finally add the tag helper and the preflight stylesheet to your layout. The tag helper renders the
stylesheet <link> and a small script that keeps it in sync
after client-side DOM changes, such as a partial view loaded over AJAX.
<head>
...
<link rel="stylesheet" href="/_content/FluentCss/preflight.css" />
<fluent-css />
</head>
No need to add hash or timestamp
/fluentcss/hash on load and
watches the document for later changes, swapping the stylesheet when it changes.
Any other .NET project
Outside of Blazor and MVC/Razor Pages there is no integration package, so add the core library directly:
dotnet add package FluentCss.Core
You can then build classes and read the generated CSS from any .NET application.
ToString() returns the space separated class names for your
class attribute, while
ClassRegistry.DefaultRegistry.Css gives you the stylesheet.
using FluentCss.Core;
var classes = FluentCssBuilder.Css
.Background(Color.Emerald500)
.Color(Color.White)
.Px(6)
.Py(3)
.RoundedMd();
// "bg-emerald-500 text-white px-6 py-3 rounded-md"
string classAttr = classes.ToString();
// The complete, deduplicated stylesheet
string css = ClassRegistry.DefaultRegistry.Css;