SharpStyles 1.0.1

dotnet add package SharpStyles --version 1.0.1
                    
NuGet\Install-Package SharpStyles -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SharpStyles" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SharpStyles" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="SharpStyles" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SharpStyles --version 1.0.1
                    
#r "nuget: SharpStyles, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=SharpStyles&version=1.0.1
                    
Install SharpStyles as a Cake Addin
#tool nuget:?package=SharpStyles&version=1.0.1
                    
Install SharpStyles as a Cake Tool

SharpStyles

.NET Nuget The Standard - COMPLIANT The Standard Community The Standard Licensed

SharpStyles

TDD-able Styles for Blazor

This library offers the capability to write CSS rules in C#. This capability allows C#.NET developers especially for Blazor applications to test-drive the CSS rules in their components effectively.

How to Use

To use SharpStyles; all you need to do is to inherit the SharpStyles model to your local Component Style models as follows:

⚠️ Important: .ToCss() is obsolete and only supports legacy nested style serialization. Use ToStyleCss() extension method instead.

Setup

public class MyComponentStyle: SharpStyle
{
	[CssElement]
	public SharpStyle Td {get; set;}
	
	[CssClass]
	public SharpStyle PrimaryButton {get; set;}

	[CssId]
	public SharpStyle SubmitButton {get; set;}
}

Now you can use MyComponentStyle as follows:

var myComponentStyle = new MyComponentStyle
{
	Td = new SharpStyle
	{
		BackgroundColor = "red"
	},

	PrimaryButton = new SharpStyle
	{
		BackgroundColor = "blue",
		Color = "white"
	},

	SubmitButton = new SharpStyle
	{
		Width = "12px"
	}
}

You can now use this object myComponentStyle to generate CSS rules for your Blazor component just as follows:

	myComponentStyle.ToStyleCss();

This code will generate the following rules:

td {
	background-color: "red";
}

.primary-button {
	background-color: "blue";
	color: "white";
}

#submit-button {
	width: "12px";
}

You can also customize your selectors as follows:

public class MyComponentStyle: SharpStyle
{
	[CssElement(Selector="my-custom-td")]
	public SharpStyle Td {get; set;}
	
	[CssClass(Selector=".my-custom-primary-button")]
	public SharpStyle PrimaryButton {get; set;}

	[CssId(Selector="#my-custom-submit-button")]
	public SharpStyle SubmitButton {get; set;}
}

which will produce the following CSS:

my-custom-td {
	background-color: "red";
}

.my-custom-primary-button {
	background-color: "blue";
	color: "white";
}

#my-custom-submit-button {
	width: "12px";
}

Media Query Support

SharpStyles also supports building CSS media queries using C# — allowing you to write responsive styles in a testable, fluent format.

Example Usage

var mediaQuery = new MediaQuery
{
    Only = true,
    MediaType = "screen",
    MaxWidth = 768,
    Styles = new SharpStyle
    {
        BackgroundColor = "red"
    }
};

string css = mediaQuery.ToStyleCss();

This generates the following CSS:

@media only screen and (max-width: 768px) {
	background-color: red;
}

You can also use additional features like min-width, not, or custom conditions such as orientation:

var mediaQuery = new MediaQuery
{
    MediaType = "screen",
    MinWidth = 600,
    MaxWidth = 1024,
    CustomFeature = "(orientation: landscape)",
    Styles = new SharpStyle
    {
        Color = "blue"
    }
};

Which generates the following CSS:

@media screen and (min-width: 600px) and (max-width: 1024px) and (orientation: landscape) {
  color: blue;
}

Support of Keyframes

Highlights
  • New model types:
    • SharpKeyframes
    • SharpKeyframe
    • SharpKeyframeProperty
  • Clean, valid @keyframes CSS generation
  • Fully compatible with existing SharpStyles design
  • Includes edge case validation
  • Thorough unit test coverage

Example 1: Fade In Animation
var keyframes = new SharpKeyframes
{
    Name = "fadeIn",

    Keyframes = new List<SharpKeyframe>
    {
        new SharpKeyframe()
        {
            Selector = "from",

            Properties = new List<SharpKeyframeProperty>()
            {
                new SharpKeyframeProperty()
                {
                    Name = "opacity",
                    Value = "0"
                }
            }
        },
        new SharpKeyframe()
        {
            Selector = "to",

            Properties = new List<SharpKeyframeProperty>()
            {
                new SharpKeyframeProperty()
                {
                    Name = "opacity",
                    Value = "1"
                }
            }
        }
    }
};

string css = keyframes.ToStyleCss();
Generates:
@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

Example 2: Pulse Animation
var keyframes = new SharpKeyframes
{
    Name = "pulse",

    Keyframes = new List<SharpKeyframe>
    {
        new SharpKeyframe()
        {
            Selector = "0%",

            Properties = new List<SharpKeyframeProperty>()
            {
                new SharpKeyframeProperty()
                {
                    Name = "transform",
                    Value = "scale(1)"
                },
                new SharpKeyframeProperty()
                {
                    Name = "opacity",
                    Value = "1"
                }
            }
        },
        new SharpKeyframe()
        {
            Selector = "50%",

            Properties = new List<SharpKeyframeProperty>()
            {
                new SharpKeyframeProperty()
                {
                    Name = "transform",
                    Value = "scale(1.1)"
                },
                new SharpKeyframeProperty()
                {
                    Name = "opacity",
                    Value = "0.7"
                }
            }
        },
        new SharpKeyframe()
        {
            Selector = "100%",

            Properties = new List<SharpKeyframeProperty>()
            {
                new SharpKeyframeProperty()
                {
                    Name = "transform",
                    Value = "scale(1)"
                },
                new SharpKeyframeProperty()
                {
                    Name = "opacity",
                    Value = "1"
                }
            }
        }
    }
};
Generates:
@keyframes pulse {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.1);
    opacity: 0.7;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

Follow-up

Here's a video introduction to this library:

IMAGE ALT TEXT HERE

<br />

If you have any suggestions, comments or questions, please feel free to contact me on: <br /> Twitter: @hassanrezkhabib <br /> LinkedIn: hassanrezkhabib <br /> E-Mail: hassanhabib@live.com <br />

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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 was computed.  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 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 147 6/1/2025
1.0.0 138 5/31/2025
0.8.0 924 5/17/2025
0.7.0 3,344 3/25/2025
0.6.0 2,380 1/22/2025
0.5.0 5,264 11/18/2024
0.4.0 3,918 6/21/2024
0.3.0 8,291 6/24/2022
0.2.0 461 6/20/2022

Fixes serialization issues