Documentation
¶
Overview ¶
Package units implements the CSS Values and Units Module Level 4 specification.
This package provides type-safe value types for CSS units including:
- Absolute lengths (px, cm, mm, in, pt, pc)
- Font-relative lengths (em, rem, ch, ex, cap, ic, lh)
- Viewport-relative lengths (vw, vh, vmin, vmax, vb, vi)
- Container-relative lengths (cqw, cqh, cqi, cqb, cqmin, cqmax)
Originally implemented in github.com/SCKelemen/layout and extracted as a standalone package for reuse across layout engines, text rendering, and other CSS-based projects.
References:
- CSS Values and Units Module Level 4: https://www.w3.org/TR/css-values-4/
- MDN Web Docs - CSS values and units: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Values_and_Units
- web.dev - Learn CSS - Sizing Units: https://web.dev/learn/css/sizing/
See also:
- CSS Text Module Level 3: https://www.w3.org/TR/css-text-3/
- CSS Writing Modes Level 4: https://www.w3.org/TR/css-writing-modes-4/
- CSS Logical Properties: https://www.w3.org/TR/css-logical-1/
Index ¶
- Variables
- type Angle
- func (a Angle) Add(other Angle) Angle
- func (a Angle) Div(scalar float64) Angle
- func (a Angle) GreaterThan(other Angle) bool
- func (a Angle) IsZero() bool
- func (a Angle) LessThan(other Angle) bool
- func (a Angle) Mul(scalar float64) Angle
- func (a Angle) Normalize() Angle
- func (a Angle) Raw() float64
- func (a Angle) String() string
- func (a Angle) Sub(other Angle) Angle
- func (a Angle) To(targetUnit AngleUnit) Angle
- func (a Angle) ToDeg() Angle
- func (a Angle) ToGrad() Angle
- func (a Angle) ToRad() Angle
- func (a Angle) ToTurns() Angle
- type AngleUnit
- type Context
- type Frequency
- func (f Frequency) Add(other Frequency) Frequency
- func (f Frequency) Div(scalar float64) Frequency
- func (f Frequency) GreaterThan(other Frequency) bool
- func (f Frequency) IsZero() bool
- func (f Frequency) LessThan(other Frequency) bool
- func (f Frequency) Mul(scalar float64) Frequency
- func (f Frequency) Raw() float64
- func (f Frequency) String() string
- func (f Frequency) Sub(other Frequency) Frequency
- func (f Frequency) To(targetUnit FrequencyUnit) Frequency
- func (f Frequency) ToHz() Frequency
- func (f Frequency) ToKHz() Frequency
- type FrequencyUnit
- type Integer
- func (i Integer) Abs() Integer
- func (i Integer) Add(other Integer) Integer
- func (i Integer) Clamp(minValue, maxValue int64) Integer
- func (i Integer) Div(other Integer) Integer
- func (i Integer) Equals(other Integer) bool
- func (i Integer) Float() float64
- func (i Integer) GreaterThan(other Integer) bool
- func (i Integer) IsEven() bool
- func (i Integer) IsNegative() bool
- func (i Integer) IsOdd() bool
- func (i Integer) IsPositive() bool
- func (i Integer) IsZero() bool
- func (i Integer) LessThan(other Integer) bool
- func (i Integer) Maximum(other Integer) Integer
- func (i Integer) Minimum(other Integer) Integer
- func (i Integer) Mod(other Integer) Integer
- func (i Integer) Mul(other Integer) Integer
- func (i Integer) Pow(exponent int64) Integer
- func (i Integer) Raw() int64
- func (i Integer) String() string
- func (i Integer) Sub(other Integer) Integer
- type Length
- func Cap(value float64) Length
- func Ch(value float64) Length
- func Cm(value float64) Length
- func Cqb(value float64) Length
- func Cqh(value float64) Length
- func Cqi(value float64) Length
- func Cqmax(value float64) Length
- func Cqmin(value float64) Length
- func Cqw(value float64) Length
- func Em(value float64) Length
- func Ex(value float64) Length
- func Ic(value float64) Length
- func In(value float64) Length
- func Lh(value float64) Length
- func Mm(value float64) Length
- func Pc(value float64) Length
- func Pt(value float64) Length
- func Px(value float64) Length
- func Q(value float64) Length
- func Rcap(value float64) Length
- func Rch(value float64) Length
- func Rem(value float64) Length
- func Rex(value float64) Length
- func Ric(value float64) Length
- func Rlh(value float64) Length
- func Vb(value float64) Length
- func Vh(value float64) Length
- func Vi(value float64) Length
- func Vmax(value float64) Length
- func Vmin(value float64) Length
- func Vw(value float64) Length
- func (l Length) Add(other Length) Length
- func (l Length) Div(scalar float64) Length
- func (l Length) GreaterThan(other Length) bool
- func (l Length) IsAbsolute() bool
- func (l Length) IsContainerRelative() bool
- func (l Length) IsFontRelative() bool
- func (l Length) IsViewportRelative() bool
- func (l Length) IsZero() bool
- func (l Length) LessThan(other Length) bool
- func (l Length) Mul(scalar float64) Length
- func (l Length) Raw() float64
- func (l Length) Resolve(ctx *Context) (Length, error)
- func (l Length) String() string
- func (l Length) Sub(other Length) Length
- func (l Length) To(targetUnit LengthUnit) (Length, error)
- func (l Length) ToPx() (Length, error)
- type LengthUnit
- type Number
- func (n Number) Abs() Number
- func (n Number) Add(other Number) Number
- func (n Number) Ceil() Number
- func (n Number) Clamp(minValue, maxValue float64) Number
- func (n Number) Div(other Number) Number
- func (n Number) Equals(other Number) bool
- func (n Number) Floor() Number
- func (n Number) GreaterThan(other Number) bool
- func (n Number) IsNegative() bool
- func (n Number) IsPositive() bool
- func (n Number) IsZero() bool
- func (n Number) LessThan(other Number) bool
- func (n Number) Mul(other Number) Number
- func (n Number) Pow(exponent float64) Number
- func (n Number) Raw() float64
- func (n Number) Round() Number
- func (n Number) Sqrt() Number
- func (n Number) String() string
- func (n Number) Sub(other Number) Number
- type Percentage
- func (p Percentage) Add(other Percentage) Percentage
- func (p Percentage) Clamp(minValue, maxValue float64) Percentage
- func (p Percentage) Div(scalar float64) Percentage
- func (p Percentage) Equals(other Percentage) bool
- func (p Percentage) Fraction() float64
- func (p Percentage) GreaterThan(other Percentage) bool
- func (p Percentage) IsZero() bool
- func (p Percentage) LessThan(other Percentage) bool
- func (p Percentage) Mul(scalar float64) Percentage
- func (p Percentage) Of(value float64) float64
- func (p Percentage) Raw() float64
- func (p Percentage) String() string
- func (p Percentage) Sub(other Percentage) Percentage
- type Ratio
- func (r Ratio) ApplyToHeight(height float64) float64
- func (r Ratio) ApplyToWidth(width float64) float64
- func (r Ratio) Equals(other Ratio) bool
- func (r Ratio) FitHeight(maxHeight float64) (width, height float64)
- func (r Ratio) FitWidth(maxWidth float64) (width, height float64)
- func (r Ratio) GreaterThan(other Ratio) bool
- func (r Ratio) Inverse() Ratio
- func (r Ratio) IsSquare() bool
- func (r Ratio) IsTall() bool
- func (r Ratio) IsWide() bool
- func (r Ratio) LessThan(other Ratio) bool
- func (r Ratio) Simplify() Ratio
- func (r Ratio) String() string
- func (r Ratio) Value() float64
- type Resolution
- func (r Resolution) Add(other Resolution) Resolution
- func (r Resolution) Div(scalar float64) Resolution
- func (r Resolution) GreaterThan(other Resolution) bool
- func (r Resolution) IsZero() bool
- func (r Resolution) LessThan(other Resolution) bool
- func (r Resolution) Mul(scalar float64) Resolution
- func (r Resolution) Raw() float64
- func (r Resolution) String() string
- func (r Resolution) Sub(other Resolution) Resolution
- func (r Resolution) To(targetUnit ResolutionUnit) Resolution
- func (r Resolution) ToDpcm() Resolution
- func (r Resolution) ToDpi() Resolution
- func (r Resolution) ToDppx() Resolution
- type ResolutionUnit
- type Time
- func (t Time) Add(other Time) Time
- func (t Time) Div(scalar float64) Time
- func (t Time) GreaterThan(other Time) bool
- func (t Time) IsZero() bool
- func (t Time) LessThan(other Time) bool
- func (t Time) Mul(scalar float64) Time
- func (t Time) Raw() float64
- func (t Time) String() string
- func (t Time) Sub(other Time) Time
- func (t Time) To(targetUnit TimeUnit) Time
- func (t Time) ToMs() Time
- func (t Time) ToSec() Time
- type TimeUnit
Constants ¶
This section is empty.
Variables ¶
var ( // Common aspect ratios Ratio16x9 = NewRatio(16, 9) // Standard widescreen Ratio16x10 = NewRatio(16, 10) // Computer display Ratio4x3 = NewRatio(4, 3) // Traditional TV/monitor Ratio3x2 = NewRatio(3, 2) // Classic 35mm film Ratio21x9 = NewRatio(21, 9) // Ultrawide Ratio1x1 = NewRatio(1, 1) // Square GoldenRatio = NewRatio(1.618, 1) // Golden ratio (φ) )
Functions ¶
This section is empty.
Types ¶
type Angle ¶
Angle represents a CSS angle value.
Angles are used in various CSS properties like transforms, gradients, and animations. CSS supports four angle units: degrees, gradians, radians, and turns.
References:
- CSS Values Level 4 - Angles: https://www.w3.org/TR/css-values-4/#angles
- MDN - CSS angle: https://developer.mozilla.org/en-US/docs/Web/CSS/angle
- web.dev - CSS transforms: https://web.dev/learn/css/transforms/
func Grad ¶
Grad creates an angle value in gradians.
Example:
angle := units.Grad(200) // 200 gradians (half circle)
func Rad ¶
Rad creates an angle value in radians.
Example:
angle := units.Rad(math.Pi) // π radians (half circle)
func Turns ¶
Turns creates an angle value in turns.
Example:
angle := units.Turns(0.25) // 0.25 turns (quarter circle)
func (Angle) GreaterThan ¶
GreaterThan returns true if this angle is greater than the other. Panics if the units are different.
func (Angle) LessThan ¶
LessThan returns true if this angle is less than the other. Panics if the units are different.
func (Angle) Normalize ¶
Normalize returns an equivalent angle in the range [0, full circle). This is useful for angles that may have wrapped around multiple times.
Example:
angle := units.Deg(450) norm := angle.Normalize() // Returns units.Deg(90)
func (Angle) Sub ¶
Sub subtracts another angle with the same unit. Panics if the units are different.
func (Angle) To ¶
To converts the angle to another unit.
Example:
angle := units.Deg(90) rad := angle.To(units.Radian) // Returns units.Rad(π/2)
func (Angle) ToDeg ¶
ToDeg converts any angle to degrees.
Example:
angle := units.Turns(0.5) deg := angle.ToDeg() // Returns units.Deg(180)
func (Angle) ToGrad ¶
ToGrad converts any angle to gradians.
Example:
angle := units.Deg(180) grad := angle.ToGrad() // Returns units.Grad(200)
type AngleUnit ¶
type AngleUnit string
AngleUnit represents a CSS angle unit type.
const ( Degree AngleUnit = "deg" // Degrees: 360deg = full circle Gradian AngleUnit = "grad" // Gradians: 400grad = full circle Radian AngleUnit = "rad" // Radians: 2π rad = full circle Turn AngleUnit = "turn" // Turns: 1turn = full circle )
CSS angle units per CSS Values Level 4.
Relationship:
- 1turn = 360deg = 400grad = 2π rad
- 1deg = 1/360 turn
- 1grad = 1/400 turn
- 1rad = 1/(2π) turn
type Context ¶
type Context struct {
// Font metrics for font-relative units
FontSize float64 // For em, ch, ex, etc. (in px)
RootFontSize float64 // For rem, rch, rex, etc. (in px)
XHeight float64 // For ex (in px)
CapHeight float64 // For cap (in px)
ChWidth float64 // For ch - width of "0" glyph (in px)
IcWidth float64 // For ic - width of "水" glyph (in px)
LineHeight float64 // For lh (in px)
RootLineHeight float64 // For rlh (in px)
// Viewport dimensions for viewport-relative units
ViewportWidth float64 // For vw, vi, etc. (in px)
ViewportHeight float64 // For vh, vb, etc. (in px)
// Container dimensions for container-relative units
ContainerWidth float64 // For cqw, cqi (in px)
ContainerHeight float64 // For cqh, cqb (in px)
}
Context provides the necessary information to resolve relative lengths to absolute values.
type Frequency ¶
type Frequency struct {
Value float64
Unit FrequencyUnit
}
Frequency represents a CSS frequency value.
Frequency values are used in CSS for audio properties and other frequency-based features. CSS supports hertz and kilohertz.
References:
- CSS Values Level 4 - Frequency: https://www.w3.org/TR/css-values-4/#frequency
- MDN - CSS frequency: https://developer.mozilla.org/en-US/docs/Web/CSS/frequency
func (Frequency) Add ¶
Add adds two frequency values with the same unit. Panics if the units are different.
func (Frequency) GreaterThan ¶
GreaterThan returns true if this frequency is greater than the other. Panics if the units are different.
func (Frequency) LessThan ¶
LessThan returns true if this frequency is less than the other. Panics if the units are different.
func (Frequency) Sub ¶
Sub subtracts another frequency value with the same unit. Panics if the units are different.
func (Frequency) To ¶
func (f Frequency) To(targetUnit FrequencyUnit) Frequency
To converts the frequency to another unit.
Example:
freq := units.Hz(1500) khz := freq.To(units.Kilohertz) // Returns units.KHz(1.5)
type FrequencyUnit ¶
type FrequencyUnit string
FrequencyUnit represents a CSS frequency unit type.
const ( Hertz FrequencyUnit = "Hz" // Hertz Kilohertz FrequencyUnit = "kHz" // Kilohertz )
CSS frequency units per CSS Values Level 4.
Relationship:
- 1kHz = 1000Hz
type Integer ¶
type Integer struct {
Value int64
}
Integer represents a CSS integer value.
Integer values are whole numbers (no fractional component). They're used in properties like z-index, column-count, grid-row-start, and anywhere a count or index is needed.
References:
- CSS Values Level 4 - Integers: https://www.w3.org/TR/css-values-4/#integers
- MDN - CSS integer: https://developer.mozilla.org/en-US/docs/Web/CSS/integer
func Int ¶
Int creates an integer value.
Example:
zIndex := units.Int(10) // z-index: 10 columns := units.Int(3) // column-count: 3
func (Integer) Clamp ¶
Clamp returns an integer clamped to the given range.
Example:
num := units.Int(150) clamped := num.Clamp(0, 100) // Returns units.Int(100)
func (Integer) GreaterThan ¶
GreaterThan returns true if this integer is greater than the other.
func (Integer) IsNegative ¶
IsNegative returns true if the integer is less than zero.
func (Integer) IsPositive ¶
IsPositive returns true if the integer is greater than zero.
type Length ¶
type Length struct {
Value float64
Unit LengthUnit
}
Length represents a CSS length value with an explicit unit.
Per CSS Values spec, lengths can be:
- Absolute: px, cm, mm, Q, in, pt, pc
- Font-relative: em, rem, ex, rex, cap, rcap, ch, rch, ic, ric, lh, rlh
- Viewport-relative: vw, vh, vmin, vmax, vb, vi, svw, svh, lvw, lvh, dvw, dvh
- Container-relative: cqw, cqh, cqi, cqb, cqmin, cqmax
Example:
width := units.Px(400) // 400 pixels margin := units.Em(1.5) // 1.5em height := units.Vh(100) // 100vh (full viewport height)
func (Length) GreaterThan ¶
GreaterThan returns true if this length is greater than another. Panics if units don't match.
func (Length) IsAbsolute ¶
IsAbsolute returns true if the unit is an absolute length unit.
func (Length) IsContainerRelative ¶
IsContainerRelative returns true if the unit is container-relative.
func (Length) IsFontRelative ¶
IsFontRelative returns true if the unit is font-relative.
func (Length) IsViewportRelative ¶
IsViewportRelative returns true if the unit is viewport-relative.
func (Length) LessThan ¶
LessThan returns true if this length is less than another. Panics if units don't match.
func (Length) Raw ¶
Raw returns the raw float64 value, discarding unit information. Use this when interfacing with APIs that need raw numbers.
func (Length) Resolve ¶
Resolve converts any length (absolute or relative) to pixels using the provided context.
Example:
ctx := &units.Context{
FontSize: 16.0,
ViewportWidth: 1920.0,
}
length := units.Em(2)
px, err := length.Resolve(ctx) // Returns units.Px(32)
type LengthUnit ¶
type LengthUnit string
LengthUnit specifies the unit of measurement for a length value.
const ( // PX - Absolute length, anchor unit // 1px = 1/96 of 1 inch // Reference: https://www.w3.org/TR/css-values-4/#absolute-lengths PX LengthUnit = "px" // CM - Absolute length // 1cm = 96px/2.54 // Reference: https://www.w3.org/TR/css-values-4/#absolute-lengths CM LengthUnit = "cm" // MM - Absolute length // 1mm = 1/10 of 1cm // Reference: https://www.w3.org/TR/css-values-4/#absolute-lengths MM LengthUnit = "mm" // QQ - Absolute length (quarter-millimeter) // 1Q = 1/40 of 1cm // Reference: https://www.w3.org/TR/css-values-4/#absolute-lengths QQ LengthUnit = "Q" // IN - Absolute length // 1in = 2.54cm = 96px // Reference: https://www.w3.org/TR/css-values-4/#absolute-lengths IN LengthUnit = "in" // PT - Absolute length (typography) // 1pt = 1/72 of 1in // Reference: https://www.w3.org/TR/css-values-4/#absolute-lengths PT LengthUnit = "pt" // PC - Absolute length (typography) // 1pc = 1/6 of 1in = 12pt // Reference: https://www.w3.org/TR/css-values-4/#absolute-lengths PC LengthUnit = "pc" // Convenience aliases (long-form names only, short names conflict with constructors) Pixel LengthUnit = PX Centimeter LengthUnit = CM Millimeter LengthUnit = MM QuarterMillimeter LengthUnit = QQ Inch LengthUnit = IN Point LengthUnit = PT Pica LengthUnit = PC )
Absolute length units are fixed in relation to each other and anchored to some physical measurement. They are mainly useful when the output environment is known.
The anchor unit is the pixel (px). The reference pixel is the visual angle of one pixel on a device with a pixel density of 96dpi and a distance from the reader of an arm's length.
const ( // EM - Font-relative length // Equal to the computed value of font-size on the element EM LengthUnit = "em" // REM - Font-relative length // Equal to the computed value of font-size on the root element REM LengthUnit = "rem" // EX - Font-relative length // Equal to the x-height of the font (height of lowercase 'x') EX LengthUnit = "ex" // REX - Font-relative length // Equal to the x-height of the root element's font REX LengthUnit = "rex" // CAP - Font-relative length // Equal to the cap-height of the font (height of capital letters) CAP LengthUnit = "cap" // RCAP - Font-relative length // Equal to the cap-height of the root element's font RCAP LengthUnit = "rcap" // CH - Font-relative length // Equal to the advance measure of the "0" (ZERO, U+0030) glyph // Used for monospace width calculations (terminal cells) CH LengthUnit = "ch" // RCH - Font-relative length // Equal to the advance measure of "0" in the root element's font RCH LengthUnit = "rch" // IC - Font-relative length // Equal to the advance measure of the "水" (CJK water ideograph, U+6C34) glyph // Used for ideographic character width IC LengthUnit = "ic" // RIC - Font-relative length // Equal to the advance measure of "水" in the root element's font RIC LengthUnit = "ric" // LH - Font-relative length // Equal to the computed value of line-height on the element LH LengthUnit = "lh" // RLH - Font-relative length // Equal to the computed value of line-height on the root element RLH LengthUnit = "rlh" // Convenience aliases (Unit suffix only, short names conflict with constructors) EmUnit LengthUnit = EM RemUnit LengthUnit = REM ExUnit LengthUnit = EX RexUnit LengthUnit = REX CapUnit LengthUnit = CAP RcapUnit LengthUnit = RCAP ChUnit LengthUnit = CH RchUnit LengthUnit = RCH IcUnit LengthUnit = IC RicUnit LengthUnit = RIC LhUnit LengthUnit = LH RlhUnit LengthUnit = RLH )
Font-relative lengths define length values in terms of the font metrics of the element on which they are used (or, for the "root" variants, the root element).
const ( // VW - Viewport width // Equal to 1% of viewport width VW LengthUnit = "vw" // VH - Viewport height // Equal to 1% of viewport height VH LengthUnit = "vh" // VMIN - Viewport minimum // Equal to the smaller of vw or vh VMIN LengthUnit = "vmin" // VMAX - Viewport maximum // Equal to the larger of vw or vh VMAX LengthUnit = "vmax" // VB - Viewport block size // Equal to 1% of viewport size in block axis VB LengthUnit = "vb" // VI - Viewport inline size // Equal to 1% of viewport size in inline axis VI LengthUnit = "vi" // Small viewport units (smallest possible viewport) SVW LengthUnit = "svw" SVH LengthUnit = "svh" SVB LengthUnit = "svb" SVI LengthUnit = "svi" // Large viewport units (largest possible viewport) LVW LengthUnit = "lvw" LVH LengthUnit = "lvh" LVB LengthUnit = "lvb" LVI LengthUnit = "lvi" // Dynamic viewport units (current viewport, accounting for dynamic UI) DVW LengthUnit = "dvw" DVH LengthUnit = "dvh" DVB LengthUnit = "dvb" DVI LengthUnit = "dvi" // Convenience aliases (Unit suffix only, short names conflict with constructors) VwUnit LengthUnit = VW VhUnit LengthUnit = VH VminUnit LengthUnit = VMIN VmaxUnit LengthUnit = VMAX VbUnit LengthUnit = VB ViUnit LengthUnit = VI SvwUnit LengthUnit = SVW SvhUnit LengthUnit = SVH SvbUnit LengthUnit = SVB SviUnit LengthUnit = SVI LvwUnit LengthUnit = LVW LvhUnit LengthUnit = LVH LvbUnit LengthUnit = LVB LviUnit LengthUnit = LVI DvwUnit LengthUnit = DVW DvhUnit LengthUnit = DVH DvbUnit LengthUnit = DVB DviUnit LengthUnit = DVI )
Viewport-percentage lengths define length values relative to the size of the initial containing block (viewport).
const ( // CQW - Container query width // Equal to 1% of query container's width CQW LengthUnit = "cqw" // CQH - Container query height // Equal to 1% of query container's height CQH LengthUnit = "cqh" // CQI - Container query inline size // Equal to 1% of query container's inline size CQI LengthUnit = "cqi" // CQB - Container query block size // Equal to 1% of query container's block size CQB LengthUnit = "cqb" // CQMIN - Container query minimum // Equal to the smaller of cqi or cqb CQMIN LengthUnit = "cqmin" // CQMAX - Container query maximum // Equal to the larger of cqi or cqb CQMAX LengthUnit = "cqmax" // Convenience aliases (Unit suffix only, short names conflict with constructors) CqwUnit LengthUnit = CQW CqhUnit LengthUnit = CQH CqiUnit LengthUnit = CQI CqbUnit LengthUnit = CQB CqminUnit LengthUnit = CQMIN CqmaxUnit LengthUnit = CQMAX )
Container query length units specify lengths relative to the dimensions of a query container.
type Number ¶
type Number struct {
Value float64
}
Number represents a CSS number value.
Number values are dimensionless real numbers, possibly with a fractional component. They're used in properties like line-height, opacity, flex-grow, and as multipliers in various calculations.
References:
- CSS Values Level 4 - Numbers: https://www.w3.org/TR/css-values-4/#numbers
- MDN - CSS number: https://developer.mozilla.org/en-US/docs/Web/CSS/number
func Num ¶
Num creates a number value.
Example:
lineHeight := units.Num(1.5) // line-height: 1.5 opacity := units.Num(0.8) // opacity: 0.8
func (Number) Clamp ¶
Clamp returns a number clamped to the given range.
Example:
num := units.Num(1.8) clamped := num.Clamp(0, 1) // Returns units.Num(1)
func (Number) GreaterThan ¶
GreaterThan returns true if this number is greater than the other.
func (Number) IsNegative ¶
IsNegative returns true if the number is less than zero.
func (Number) IsPositive ¶
IsPositive returns true if the number is greater than zero.
type Percentage ¶
type Percentage struct {
Value float64
}
Percentage represents a CSS percentage value.
Percentage values are always relative to another quantity. The meaning of 100% depends on the context in which the percentage is used. For example, width: 50% means 50% of the containing block's width.
References:
- CSS Values Level 4 - Percentages: https://www.w3.org/TR/css-values-4/#percentages
- MDN - CSS percentage: https://developer.mozilla.org/en-US/docs/Web/CSS/percentage
func Percent ¶
func Percent(value float64) Percentage
Percent creates a percentage value.
Example:
width := units.Percent(50) // 50%
func (Percentage) Add ¶
func (p Percentage) Add(other Percentage) Percentage
Add adds two percentage values.
func (Percentage) Clamp ¶
func (p Percentage) Clamp(minValue, maxValue float64) Percentage
Clamp returns a percentage clamped to the given range.
Example:
pct := units.Percent(150) clamped := pct.Clamp(0, 100) // Returns units.Percent(100)
func (Percentage) Div ¶
func (p Percentage) Div(scalar float64) Percentage
Div divides the percentage by a scalar value.
func (Percentage) Equals ¶
func (p Percentage) Equals(other Percentage) bool
Equals returns true if this percentage equals the other.
func (Percentage) Fraction ¶
func (p Percentage) Fraction() float64
Fraction returns the percentage as a fraction (e.g., 50% -> 0.5).
func (Percentage) GreaterThan ¶
func (p Percentage) GreaterThan(other Percentage) bool
GreaterThan returns true if this percentage is greater than the other.
func (Percentage) IsZero ¶
func (p Percentage) IsZero() bool
IsZero returns true if the percentage value is zero.
func (Percentage) LessThan ¶
func (p Percentage) LessThan(other Percentage) bool
LessThan returns true if this percentage is less than the other.
func (Percentage) Mul ¶
func (p Percentage) Mul(scalar float64) Percentage
Mul multiplies the percentage by a scalar value.
func (Percentage) Of ¶
func (p Percentage) Of(value float64) float64
Of calculates the percentage of a given value.
Example:
pct := units.Percent(50) result := pct.Of(200) // Returns 100 (50% of 200)
func (Percentage) Raw ¶
func (p Percentage) Raw() float64
Raw returns the raw numeric value of the percentage.
func (Percentage) String ¶
func (p Percentage) String() string
String returns the string representation of the percentage.
func (Percentage) Sub ¶
func (p Percentage) Sub(other Percentage) Percentage
Sub subtracts another percentage value.
type Ratio ¶
Ratio represents a CSS ratio value.
Ratio values consist of two positive numbers separated by a slash (/), representing the ratio of the first number to the second. Common uses include aspect-ratio property for maintaining proportions.
If the second number is omitted, it defaults to 1.
References:
- CSS Values Level 4 - Ratios: https://www.w3.org/TR/css-values-4/#ratios
- MDN - CSS ratio: https://developer.mozilla.org/en-US/docs/Web/CSS/ratio
- MDN - aspect-ratio: https://developer.mozilla.org/en-US/docs/Web/CSS/aspect-ratio
func NewRatio ¶
NewRatio creates a ratio value from two numbers.
Example:
aspectRatio := units.NewRatio(16, 9) // 16:9 aspect ratio square := units.NewRatio(1, 1) // 1:1 aspect ratio
func RatioFrom ¶
RatioFrom creates a ratio from a single number (equivalent to n/1).
Example:
ratio := units.RatioFrom(2) // 2/1 ratio
func (Ratio) ApplyToHeight ¶
ApplyToHeight calculates the width for a given height based on this ratio.
Example:
ratio := units.NewRatio(16, 9) // 16:9 width := ratio.ApplyToHeight(1080) // Returns 1920
func (Ratio) ApplyToWidth ¶
ApplyToWidth calculates the height for a given width based on this ratio.
Example:
ratio := units.NewRatio(16, 9) // 16:9 height := ratio.ApplyToWidth(1920) // Returns 1080
func (Ratio) FitHeight ¶
FitHeight returns the dimensions that fit within maxHeight while maintaining the ratio.
func (Ratio) FitWidth ¶
FitWidth returns the dimensions that fit within maxWidth while maintaining the ratio.
func (Ratio) GreaterThan ¶
GreaterThan returns true if this ratio is greater than the other.
func (Ratio) Inverse ¶
Inverse returns the inverse of the ratio (swaps first and second).
Example:
ratio := units.NewRatio(16, 9) inverse := ratio.Inverse() // Returns 9/16
func (Ratio) Simplify ¶
Simplify returns a simplified version of the ratio by dividing both numbers by their greatest common divisor.
Example:
ratio := units.NewRatio(16, 8) simplified := ratio.Simplify() // Returns 2/1
type Resolution ¶
type Resolution struct {
Value float64
Unit ResolutionUnit
}
Resolution represents a CSS resolution value.
Resolution values are used in CSS media queries and image properties to describe the density of pixels. CSS supports dots per inch (dpi), dots per centimeter (dpcm), and dots per pixel unit (dppx).
References:
- CSS Values Level 4 - Resolution: https://www.w3.org/TR/css-values-4/#resolution
- MDN - CSS resolution: https://developer.mozilla.org/en-US/docs/Web/CSS/resolution
- web.dev - Responsive images: https://web.dev/learn/design/responsive-images/
func Dpcm ¶
func Dpcm(value float64) Resolution
Dpcm creates a resolution value in dots per centimeter.
Example:
res := units.Dpcm(37.795) // Equivalent to 96dpi
func Dpi ¶
func Dpi(value float64) Resolution
Dpi creates a resolution value in dots per inch.
Example:
res := units.Dpi(96) // Standard screen resolution
func Dppx ¶
func Dppx(value float64) Resolution
Dppx creates a resolution value in dots per pixel unit.
Example:
res := units.Dppx(2) // 2x "Retina" display
func (Resolution) Add ¶
func (r Resolution) Add(other Resolution) Resolution
Add adds two resolution values with the same unit. Panics if the units are different.
func (Resolution) Div ¶
func (r Resolution) Div(scalar float64) Resolution
Div divides the resolution by a scalar value.
func (Resolution) GreaterThan ¶
func (r Resolution) GreaterThan(other Resolution) bool
GreaterThan returns true if this resolution is greater than the other. Panics if the units are different.
func (Resolution) IsZero ¶
func (r Resolution) IsZero() bool
IsZero returns true if the resolution value is zero.
func (Resolution) LessThan ¶
func (r Resolution) LessThan(other Resolution) bool
LessThan returns true if this resolution is less than the other. Panics if the units are different.
func (Resolution) Mul ¶
func (r Resolution) Mul(scalar float64) Resolution
Mul multiplies the resolution by a scalar value.
func (Resolution) Raw ¶
func (r Resolution) Raw() float64
Raw returns the raw numeric value of the resolution.
func (Resolution) String ¶
func (r Resolution) String() string
String returns the string representation of the resolution.
func (Resolution) Sub ¶
func (r Resolution) Sub(other Resolution) Resolution
Sub subtracts another resolution value with the same unit. Panics if the units are different.
func (Resolution) To ¶
func (r Resolution) To(targetUnit ResolutionUnit) Resolution
To converts the resolution to another unit.
Example:
res := units.Dpi(96) dppx := res.To(units.DotsPerPixel) // Returns units.Dppx(1)
func (Resolution) ToDpcm ¶
func (r Resolution) ToDpcm() Resolution
ToDpcm converts any resolution value to dots per centimeter.
Example:
res := units.Dpi(96) dpcm := res.ToDpcm() // Returns units.Dpcm(37.795)
func (Resolution) ToDpi ¶
func (r Resolution) ToDpi() Resolution
ToDpi converts any resolution value to dots per inch.
Example:
res := units.Dppx(2) dpi := res.ToDpi() // Returns units.Dpi(192)
func (Resolution) ToDppx ¶
func (r Resolution) ToDppx() Resolution
ToDppx converts any resolution value to dots per pixel unit.
Example:
res := units.Dpi(192) dppx := res.ToDppx() // Returns units.Dppx(2)
type ResolutionUnit ¶
type ResolutionUnit string
ResolutionUnit represents a CSS resolution unit type.
const ( DotsPerInch ResolutionUnit = "dpi" // Dots per inch DotsPerCentimeter ResolutionUnit = "dpcm" // Dots per centimeter DotsPerPixel ResolutionUnit = "dppx" // Dots per pixel unit )
CSS resolution units per CSS Values Level 4.
Relationship:
- 1dppx = 96dpi
- 1dpcm = 96dpi / 2.54 ≈ 37.795dpi
- 1in = 2.54cm
type Time ¶
Time represents a CSS time value.
Time values are used in CSS animations, transitions, and other time-based properties. CSS supports seconds and milliseconds.
References:
- CSS Values Level 4 - Time: https://www.w3.org/TR/css-values-4/#time
- MDN - CSS time: https://developer.mozilla.org/en-US/docs/Web/CSS/time
- web.dev - CSS animations: https://web.dev/learn/css/animations/
func Ms ¶
Ms creates a time value in milliseconds.
Example:
duration := units.Ms(500) // 500 milliseconds
func (Time) GreaterThan ¶
GreaterThan returns true if this time is greater than the other. Panics if the units are different.
func (Time) LessThan ¶
LessThan returns true if this time is less than the other. Panics if the units are different.
func (Time) Sub ¶
Sub subtracts another time value with the same unit. Panics if the units are different.
func (Time) To ¶
To converts the time to another unit.
Example:
time := units.Sec(1.5) ms := time.To(units.Millisecond) // Returns units.Ms(1500)