CssInCSharp 0.1.0-nightly-250508055741
dotnet add package CssInCSharp --version 0.1.0-nightly-250508055741
NuGet\Install-Package CssInCSharp -Version 0.1.0-nightly-250508055741
<PackageReference Include="CssInCSharp" Version="0.1.0-nightly-250508055741" />
<PackageVersion Include="CssInCSharp" Version="0.1.0-nightly-250508055741" />
<PackageReference Include="CssInCSharp" />
paket add CssInCSharp --version 0.1.0-nightly-250508055741
#r "nuget: CssInCSharp, 0.1.0-nightly-250508055741"
#addin nuget:?package=CssInCSharp&version=0.1.0-nightly-250508055741&prerelease
#tool nuget:?package=CssInCSharp&version=0.1.0-nightly-250508055741&prerelease
CssInCSharp
A lib for generating Style Sheets with C#.
Getting started
dotnet add package CssInCSharp
Usage
- Used within a native
<style>
tag.
<div class="basic">
<div class="title">Title</div>
<button class="button">Click</button>
</div>
<style>
@_css
</style>
@code
{
private string _css = string.Empty;
protected override void OnInitialized()
{
_css = new CSSObject
{
[".basic"] = new CSSObject
{
Width = 300,
Height = 300,
Border = "1px solid #DDD",
["& .title"] = new CSSObject
{
LineHeight = 20,
Color = "red"
},
["& .button"] = new CSSObject
{
Width = "100%",
Height = "20px",
TextAlign = "center",
["&:hover"] = new CSSObject
{
Color = "blue"
}
}
}
}.ToString();
}
}
- Use the
StyleContent
andStyle
components on the page.
<div class="div1">
Style In Head
</div>
<div class="div2">
Style In Body
</div>
<StyleContent>
<Style StyleFn="@StyleInHead" Path="Basic|StyleTag"></Style>
</StyleContent>
<Style StyleFn="@StyleInBody"></Style>
@code {
private CSSInterpolation StyleInHead()
{
return new CSSObject
{
[".div1"] = new CSSObject
{
Width = "200px",
Height = "200px",
Border = "1px solid #DDD",
}
};
}
private CSSInterpolation StyleInBody()
{
return new CSSObject
{
[".div2"] = new CSSObject
{
Width = "200px",
Height = "200px",
Border = "1px solid #DDD",
BackgroundColor = "#EFEFEF",
}
};
}
}
- Use StyleHelper to inject styles in custom components.
<div class="@_token.HashId @Name">
@foreach (var item in Items)
{
<div class="item">@item</div>
}
</div>
@code {
[Parameter]
public string[] Items { get; set; }
[Parameter]
public int Width { get; set; } = 500;
protected override void OnInitialized()
{
// use style helper to register style
StyleHelper.Register(new StyleInfo
{
HashId = _token.HashId,
Path = new string[]{ "component", "demo" },
StyleFn = UseStyle
});
}
private CSSInterpolation UseStyle()
{
...
}
}
- Inject a
CSSObject
orCSSString
into the head.
@_node
@code {
private RenderFragment _node;
protected override void OnInitialized()
{
var styles = new
{
container = new CSSObject
{
BackgroundColor = token.ColorBgLayout,
BorderRadius = token.BorderRadiusLG,
MaxWidth = 400,
Width = "100%",
Height = 180,
Display = "flex",
AlignItems = "center",
JustifyContent = "center",
FlexDirection = "column",
MarginLeft = "auto",
MarginRight = "auto",
},
card = CSS($$""""
box-shadow: {{token.BoxShadow}};
padding: {{token.Padding}}px;
border-radius: {{token.BorderRadius}}px;
color: {{token.ColorTextTertiary}};
background: {{token.ColorBgContainer}};
transition: all 100ms {{token.MotionEaseInBack}};
margin-bottom: 8px;
cursor: pointer;
&:hover {
color: {{token.ColorTextSecondary}};
box-shadow: {{token.BoxShadowSecondary}};
}
""""),
};
// The CX and CSS methods are defined in the StyleHelper class.
// @using static CssInCSharp.StyleHelper
_node = @<div class="@CX("a-simple-create-style-demo-classname", styles.container)">
<div class="@styles.card">createStyles Demo</div>
<div>Current theme mode: dark</div>
</div>;
}
}
For other examples, you can check out the example code. For more information, please refer to the document.
Css Compiler
The CssInCSharp is similar to less or sass. You can simply convert you style file into C# class, so that you can make full use of the C# language features to generate style content.
Css Converter
For those projects that are already using css or ts styles, you can use the converter provided by CssInCSharp to convert css or ts code into C# code, which improves style control and code reuse.
For details about the style migration of antd-v5, see the Antd v5 Style Migration Document.
Benchmark
BenchmarkDotNet v0.13.12, Windows 11 (10.0.26100.3775)
AMD Ryzen 7 5700G with Radeon Graphics, 1 CPU, 16 logical and 8 physical cores
.NET SDK 9.0.200
[Host] : .NET 9.0.2 (9.0.225.6610), X64 RyuJIT AVX2
.NET 5.0 : .NET 5.0.17 (5.0.1722.21314), X64 RyuJIT AVX2
.NET 6.0 : .NET 6.0.28 (6.0.2824.12007), X64 RyuJIT AVX2
.NET 7.0 : .NET 7.0.17 (7.0.1724.11508), X64 RyuJIT AVX2
.NET 8.0 : .NET 8.0.13 (8.0.1325.6609), X64 RyuJIT AVX2
.NET 9.0 : .NET 9.0.2 (9.0.225.6610), X64 RyuJIT AVX2
Method | Job | Runtime | Mean | Error | StdDev | Median | Ratio | RatioSD | Gen0 | Gen1 | Allocated | Alloc Ratio |
---|---|---|---|---|---|---|---|---|---|---|---|---|
CreateCss | .NET 5.0 | .NET 5.0 | 55.68 μs | 1.112 μs | 1.795 μs | 55.56 μs | 1.00 | 0.00 | 20.0806 | 1.0986 | 164.32 KB | 1.00 |
CreateCss | .NET 6.0 | .NET 6.0 | 51.66 μs | 1.024 μs | 2.374 μs | 52.28 μs | 0.91 | 0.05 | 19.3481 | 1.0986 | 158.04 KB | 0.96 |
CreateCss | .NET 7.0 | .NET 7.0 | 48.51 μs | 0.969 μs | 1.450 μs | 48.49 μs | 0.87 | 0.03 | 19.3481 | 1.0376 | 158.11 KB | 0.96 |
CreateCss | .NET 8.0 | .NET 8.0 | 41.71 μs | 1.585 μs | 4.391 μs | 40.90 μs | 0.81 | 0.08 | 19.2871 | 1.0986 | 157.98 KB | 0.96 |
CreateCss | .NET 9.0 | .NET 9.0 | 33.33 μs | 0.660 μs | 1.840 μs | 32.80 μs | 0.62 | 0.03 | 19.2871 | 1.0986 | 157.98 KB | 0.96 |
Special Thanks
cssinjs: Component level cssinjs solution used in ant.design.
stylis: A Light–weight CSS Preprocessor.
csstype: TypeScript and Flow definitions for CSS.
tinycolor: A small library for color manipulation and conversion.
TypeScriptAST: .NET port of TypeScript parser.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Microsoft.AspNetCore.Components.Web (>= 3.1.0)
-
net5.0
- Microsoft.AspNetCore.Components.Web (>= 5.0.0)
-
net6.0
- Microsoft.AspNetCore.Components.Web (>= 6.0.0)
-
net7.0
- Microsoft.AspNetCore.Components.Web (>= 7.0.0)
-
net8.0
- Microsoft.AspNetCore.Components.Web (>= 8.0.0)
-
net9.0
- Microsoft.AspNetCore.Components.Web (>= 9.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on CssInCSharp:
Package | Downloads |
---|---|
AntDesign
🌈 A set of enterprise-class UI components based on Ant Design and Blazor. |
GitHub repositories
This package is not used by any popular GitHub repositories.