Table of Contents

Struct CssLength

Namespace
Egami.Css
Assembly
Egami.Css.dll

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

Value double

The number.

Unit CssUnit

The unit.

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

bool

Unit

The unit.

public CssUnit Unit { get; init; }

Property Value

CssUnit

Value

The number.

public double Value { get; init; }

Property Value

double

Zero

Gets a zero length in pixels.

public static CssLength Zero { get; }

Property Value

CssLength

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

value CssComponentValue

The component value.

length CssLength

The 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

values IReadOnlyList<CssComponentValue>

The component values.

length CssLength

The 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

text string

The CSS text.

length CssLength

The length.

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

pixels double

The length in pixels.

Returns

bool

True when the unit is absolute; false for relative units, percentages and unitless numbers.