Styling
Styling your emails.
The Styles
class provides a convenient way to build and manage CSS styles in your Blazor email components. You can create a Styles
instance and use the Set
method to define styles in a type-safe manner.
Creating Styles
To create a new instance of Styles
, you can either start from scratch or combine existing styles. Here’s an example of how to initialize a Styles
instance:
Explanation of the Code
-
Set
Method: TheSet
method takes a lambda expression that points to a property in theIStyleOptions
interface and the value you want to assign to that property. The method converts the property name into a CSS property name and adds it to the internal list of styles. -
Using Enums: If you use an enum for a style property, ensure that the enum values have the
CssValueAttribute
applied to them. This attribute maps the enum value to a corresponding CSS value.
Combining Styles
You can combine multiple Styles
instances using the Combine
method. For example:
Raw String Usage
If you prefer to define styles as raw strings, you can also use the style
attribute directly in your components. For example:
Implicit Conversion
The Styles
class supports implicit conversion to a string, so you can directly use it in attributes or for rendering:
Checking for Empty Styles
You can check if a Styles
instance is empty by using the IsEmpty
method:
Summary
The Styles
class provides a robust and type-safe way to manage CSS styles in your Blazor components. You can easily create, combine, and convert styles while maintaining a clean and readable codebase.