A comprehensive, type-safe Go implementation of the CSS Values and Units Module Level 4 specification. Perfect for layout engines, CSS parsers, rendering systems, and any application that needs to work with CSS-style measurements.
- Complete CSS Unit Coverage: Supports all CSS value types including lengths, angles, time, frequency, resolution, numbers, percentages, and ratios
- Type-Safe API: Strongly-typed units prevent mixing incompatible measurements
- Accurate Conversions: Implements CSS spec-compliant conversion algorithms
- Context-Aware Resolution: Resolves relative units (em, vh, cqw, etc.) using rendering context
- Zero Dependencies: Pure Go implementation with no external dependencies
- Well Documented: Extensive godoc comments with references to CSS specifications and MDN docs
- Fully Tested: Comprehensive test coverage
go get github.com/SCKelemen/unitspackage main
import (
"fmt"
"github.com/SCKelemen/units"
)
func main() {
// Create length values
width := units.Px(400)
margin := units.Em(1.5)
height := units.Vh(100)
// Perform arithmetic operations
doubled := width.Mul(2)
fmt.Println(doubled) // 800.00px
// Convert between absolute units
inches := units.In(1)
pixels, _ := inches.ToPx()
fmt.Println(pixels) // 96.00px
// Work with angles
angle := units.Deg(90)
radians := angle.ToRad()
fmt.Println(radians) // 1.57rad
// Use time values
duration := units.Sec(2.5)
ms := duration.ToMs()
fmt.Println(ms) // 2500.00ms
// Work with ratios
aspectRatio := units.NewRatio(16, 9)
height := aspectRatio.ApplyToWidth(1920)
fmt.Println(height) // 1080
}- px - Pixels (anchor unit)
- cm - Centimeters
- mm - Millimeters
- Q - Quarter-millimeters
- in - Inches
- pt - Points (1/72 inch)
- pc - Picas (12 points)
- em, rem - Font size (element, root)
- ex, rex - X-height (element, root)
- cap, rcap - Cap height (element, root)
- ch, rch - Character width of "0" (element, root)
- ic, ric - Ideographic character width (element, root)
- lh, rlh - Line height (element, root)
- vw, vh - Viewport width/height
- vmin, vmax - Viewport minimum/maximum
- vb, vi - Viewport block/inline size
- svw, svh, svb, svi - Small viewport units
- lvw, lvh, lvb, lvi - Large viewport units
- dvw, dvh, dvb, dvi - Dynamic viewport units
- cqw, cqh - Container query width/height
- cqi, cqb - Container query inline/block size
- cqmin, cqmax - Container query minimum/maximum
- Angle: deg, grad, rad, turn
- Time: s, ms
- Frequency: Hz, kHz
- Resolution: dpi, dpcm, dppx
- Number: Dimensionless values
- Percentage: Relative percentages
- Integer: Whole numbers
- Ratio: Aspect ratios (e.g., 16/9)
Resolve relative units to absolute pixels using a rendering context:
// Create a context with rendering information
ctx := &units.Context{
FontSize: 16.0,
RootFontSize: 16.0,
ViewportWidth: 1920.0,
ViewportHeight: 1080.0,
}
// Resolve relative units
emLength := units.Em(2)
px, _ := emLength.Resolve(ctx)
fmt.Println(px) // 32.00px
vwLength := units.Vw(50)
px, _ = vwLength.Resolve(ctx)
fmt.Println(px) // 960.00px// Convert between absolute units
cm := units.Cm(2.54)
px, _ := cm.ToPx()
fmt.Println(px) // 96.00px
// Use generic To() method
inches := units.In(1)
pt, _ := inches.To(units.PT)
fmt.Println(pt) // 72.00ptdegrees := units.Deg(180)
radians := degrees.ToRad()
turns := degrees.ToTurns()
fmt.Println(radians) // 3.14rad
fmt.Println(turns) // 0.50turn
// Parse CSS angle strings
angle, _ := units.ParseAngle("0.5turn")
fmt.Println(angle.ToDeg()) // 180.00degseconds := units.Sec(2.5)
ms := seconds.ToMs()
fmt.Println(ms) // 2500.00ms// Same-unit operations
a := units.Px(100)
b := units.Px(50)
sum := a.Add(b) // 150.00px
diff := a.Sub(b) // 50.00px
// Scalar operations
doubled := a.Mul(2) // 200.00px
half := a.Div(2) // 50.00px
// Comparisons
isLess := a.LessThan(b) // false
isGreater := a.GreaterThan(b) // true- Layout Engines: Building CSS-compliant layout systems
- CSS Parsers: Parsing and validating CSS values
- Design Tools: Creating design systems with precise measurements
- Rendering Systems: Converting units for different display contexts
- Animation Systems: Working with time-based animations
- Media Queries: Handling viewport and container queries
- Typography: Managing font sizes and line heights
Full API documentation is available at pkg.go.dev/github.com/SCKelemen/units.
Each type includes:
- Constructor functions for easy value creation
- String() methods for CSS-compatible output
- Arithmetic operations (Add, Sub, Mul, Div)
- Comparison operations (LessThan, GreaterThan, Equals)
- Unit conversion methods
- Utility methods specific to each type
This package implements the following specifications:
- CSS Values and Units Module Level 4
- CSS Containment Module Level 3 (Container queries)
- MDN Web Docs - CSS values and units
- web.dev - Learn CSS
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
BearWare 1.0 License - see the LICENSE file for details.
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.