Struct CssLength
A length (or percentage, or unitless number) with its unit, such as 12px or 50%.
public readonly record struct CssLength : IEquatable<CssLength>
- Implements
- Inherited Members
Examples
bool isLength = CssLength.TryParse("1.5em", out CssLength length); // (1.5, CssUnit.Em)
new CssLength(1, CssUnit.In).TryToPixels(out double px); // 96
bool isAngle = CssAngle.TryParse("0.25turn", out CssAngle angle); // 90 degrees
bool isUrl = CssUrl.TryParse("url('pattern.svg#dots')", out CssUrl url);
// calc(), min(), max() and clamp(): the consumer says what each unit is worth.
bool isCalc = CssCalc.TryParse("calc(100% - 2em)", out CssCalc? calc);
double width = calc!.Resolve(unit => unit switch
{
CssUnit.Percent => 3, // a percent of a 300px box
CssUnit.Em => 16,
CssUnit.Px => 1,
_ => double.NaN,
}); // 268
Constructors
CssLength(double, CssUnit)
A length (or percentage, or unitless number) with its unit, such as 12px or 50%.
public CssLength(double Value, CssUnit Unit)
Parameters
Examples
bool isLength = CssLength.TryParse("1.5em", out CssLength length); // (1.5, CssUnit.Em)
new CssLength(1, CssUnit.In).TryToPixels(out double px); // 96
bool isAngle = CssAngle.TryParse("0.25turn", out CssAngle angle); // 90 degrees
bool isUrl = CssUrl.TryParse("url('pattern.svg#dots')", out CssUrl url);
// calc(), min(), max() and clamp(): the consumer says what each unit is worth.
bool isCalc = CssCalc.TryParse("calc(100% - 2em)", out CssCalc? calc);
double width = calc!.Resolve(unit => unit switch
{
CssUnit.Percent => 3, // a percent of a 300px box
CssUnit.Em => 16,
CssUnit.Px => 1,
_ => double.NaN,
}); // 268
Properties
IsAbsolute
Gets whether the unit is absolute (px, cm, mm, Q, in, pt, pc), so TryToPixels(out double) succeeds.
public bool IsAbsolute { get; }
Property Value
Unit
The unit.
public CssUnit Unit { get; init; }
Property Value
Value
The number.
public double Value { get; init; }
Property Value
Zero
Gets a zero length in pixels.
public static CssLength Zero { get; }
Property Value
Methods
ToString()
Formats the length as CSS text, such as 12px.
public override string ToString()
Returns
- string
The text.
TryParse(CssComponentValue, out CssLength)
Parses one component value as a length, percentage or unitless number.
public static bool TryParse(CssComponentValue value, out CssLength length)
Parameters
valueCssComponentValueThe component value.
lengthCssLengthThe length.
Returns
- bool
True when the value is a length, percentage or number.
TryParse(IReadOnlyList<CssComponentValue>, out CssLength)
Parses a length, percentage or unitless number from component values (whitespace around it is ignored).
public static bool TryParse(IReadOnlyList<CssComponentValue> values, out CssLength length)
Parameters
valuesIReadOnlyList<CssComponentValue>The component values.
lengthCssLengthThe length.
Returns
- bool
True when the values are one length, percentage or number.
TryParse(string, out CssLength)
Parses a length, percentage or unitless number, such as 1.5em.
public static bool TryParse(string text, out CssLength length)
Parameters
Returns
- bool
True when the text is one length, percentage or number.
TryToPixels(out double)
Converts an absolute length to CSS pixels.
public bool TryToPixels(out double pixels)
Parameters
pixelsdoubleThe length in pixels.
Returns
- bool
True when the unit is absolute; false for relative units, percentages and unitless numbers.