or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-css-box-model

Get accurate and well named CSS Box Model information about DOM elements

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/css-box-model@1.2.x

To install, run

npx @tessl/cli install tessl/npm-css-box-model@1.2.0

index.mddocs/

CSS Box Model

CSS Box Model provides accurate and well-named CSS Box Model information about DOM elements. It serves as an enhanced alternative to getBoundingClientRect() with more detailed information, including all box model layers (margin, border, padding, content) with proper positioning data and scroll-aware calculations.

Package Information

  • Package Name: css-box-model
  • Package Type: npm
  • Language: JavaScript (with TypeScript definitions)
  • Installation: npm install css-box-model

Core Imports

import { getBox, createBox, withScroll, calculateBox, offset, getRect, expand, shrink } from "css-box-model";

For CommonJS:

const { getBox, createBox, withScroll, calculateBox, offset, getRect, expand, shrink } = require("css-box-model");

Import types (TypeScript):

import type { BoxModel, Rect, Position, Spacing, AnyRectType, CreateBoxArgs } from "css-box-model";

Basic Usage

import { getBox, withScroll } from "css-box-model";

// Get box model for a DOM element
const element = document.getElementById("my-element");
const boxModel = getBox(element);

// Access different box types
console.log(boxModel.contentBox);   // Content area
console.log(boxModel.paddingBox);   // Content + padding
console.log(boxModel.borderBox);    // Content + padding + border
console.log(boxModel.marginBox);    // Content + padding + border + margin

// Get page-relative positioning (with scroll offset)
const pageRelativeBox = withScroll(boxModel);

Architecture

CSS Box Model is built around the CSS box model specification:

  • Box Model Types: Four distinct box areas (content, padding, border, margin) as Rect objects
  • Rect Objects: Extended rectangle with ClientRect/DOMRect compatibility plus center point calculation
  • Spacing Objects: Uniform spacing representation for margins, borders, and padding
  • Calculation Engine: Direct DOM measurement and programmatic box model creation
  • Scroll Integration: Page-relative positioning with automatic or manual scroll offset handling

Capabilities

DOM Element Analysis

Direct analysis of DOM elements to extract complete box model information.

/**
 * Get complete box model for a DOM element
 * @param el - DOM element to analyze
 * @returns Complete box model with all box types and spacing
 */
function getBox(el: Element): BoxModel;

/**
 * Calculate box model from borderBox and styles without DOM queries
 * @param borderBox - Border box dimensions (from getBoundingClientRect)
 * @param styles - Computed styles (from getComputedStyle)
 * @returns Complete box model
 */
function calculateBox(
  borderBox: AnyRectType,
  styles: CSSStyleDeclaration
): BoxModel;

Box Model Creation

Programmatic creation of box models from dimensions and spacing values.

/**
 * Create box model from provided dimensions and spacing
 * @param boxArgs - Box dimensions and optional spacing values
 * @returns Complete box model
 */
function createBox(boxArgs: CreateBoxArgs): BoxModel;

interface CreateBoxArgs {
  borderBox: AnyRectType;
  margin?: Spacing;
  border?: Spacing;
  padding?: Spacing;
}

Position and Scroll Management

Tools for handling element positioning relative to page and viewport.

/**
 * Apply scroll offset to box model for page-relative positioning
 * @param original - Original box model
 * @param scroll - Scroll offset (defaults to window scroll)
 * @returns Box model adjusted for scroll position
 */
function withScroll(
  original: BoxModel,
  scroll?: Position
): BoxModel;

/**
 * Apply position offset to box model
 * @param original - Original box model
 * @param change - Position change to apply
 * @returns Box model with applied offset
 */
function offset(original: BoxModel, change: Position): BoxModel;

Utility Functions

Helper functions for working with rect and spacing objects.

/**
 * Convert any rect-like object to full Rect with calculated dimensions
 * @param spacing - Object with top, right, bottom, left properties
 * @returns Complete Rect with width, height, center, and position
 */
function getRect({ top, right, bottom, left }: Spacing): Rect;

/**
 * Expand spacing dimensions by adding to each side
 * @param target - Original spacing
 * @param expandBy - Amount to expand by on each side
 * @returns Expanded spacing
 */
function expand(target: Spacing, expandBy: Spacing): Spacing;

/**
 * Shrink spacing dimensions by subtracting from each side
 * @param target - Original spacing
 * @param shrinkBy - Amount to shrink by on each side
 * @returns Shrunk spacing
 */
function shrink(target: Spacing, shrinkBy: Spacing): Spacing;

Types

Core Types

interface Position {
  x: number;
  y: number;
}

interface Rect {
  // ClientRect compatibility
  top: number;
  right: number;
  bottom: number;
  left: number;
  width: number;
  height: number;
  // DOMRect compatibility
  x: number;
  y: number;
  // Enhanced features
  center: Position;
}

interface Spacing {
  top: number;
  right: number;
  bottom: number;
  left: number;
}

interface BoxModel {
  // Box model layers
  marginBox: Rect;   // content + padding + border + margin
  borderBox: Rect;   // content + padding + border
  paddingBox: Rect;  // content + padding
  contentBox: Rect;  // content only
  // Spacing values
  border: Spacing;
  padding: Spacing;
  margin: Spacing;
}

Utility Types

type AnyRectType = ClientRect | DOMRect | Rect | Spacing;

interface CreateBoxArgs {
  borderBox: AnyRectType;
  margin?: Spacing;
  border?: Spacing;
  padding?: Spacing;
}

Usage Examples

Basic Box Model Access

import { getBox } from "css-box-model";

const element = document.querySelector(".my-element");
const box = getBox(element);

// Access different box areas
console.log("Content dimensions:", box.contentBox.width, box.contentBox.height);
console.log("Total element size:", box.marginBox.width, box.marginBox.height);
console.log("Element position:", box.borderBox.x, box.borderBox.y);
console.log("Element center:", box.borderBox.center.x, box.borderBox.center.y);

// Access spacing values
console.log("Padding:", box.padding);
console.log("Margin:", box.margin);
console.log("Border:", box.border);

Page-Relative Positioning

import { getBox, withScroll } from "css-box-model";

const element = document.querySelector(".floating-element");
const viewportBox = getBox(element);
const pageBox = withScroll(viewportBox);

console.log("Position relative to viewport:", viewportBox.borderBox.x, viewportBox.borderBox.y);
console.log("Position relative to page:", pageBox.borderBox.x, pageBox.borderBox.y);

// Custom scroll offset
const customScrollBox = withScroll(viewportBox, { x: 100, y: 200 });

Creating Box Models Programmatically

import { createBox, getRect } from "css-box-model";

// Create a box model from dimensions
const borderBox = { top: 10, left: 20, bottom: 60, right: 120 };
const padding = { top: 5, right: 10, bottom: 5, left: 10 };
const margin = { top: 10, right: 15, bottom: 10, left: 15 };

const box = createBox({
  borderBox,
  padding,
  margin
});

console.log("Created box:", box);

// Convert spacing to full rect
const rect = getRect(borderBox);
console.log("Full rect:", rect);
// Output includes width: 100, height: 50, center: { x: 70, y: 35 }

Performance Optimization

import { calculateBox } from "css-box-model";

// If you already have getBoundingClientRect and getComputedStyle results
const element = document.querySelector(".performance-critical");
const borderBox = element.getBoundingClientRect();
const styles = window.getComputedStyle(element);

// Skip redundant DOM queries
const box = calculateBox(borderBox, styles);

Working with Spacing

import { expand, shrink } from "css-box-model";

const originalSpacing = { top: 10, right: 20, bottom: 10, left: 20 };

// Expand the spacing
const expanded = expand(originalSpacing, { top: 5, right: 5, bottom: 5, left: 5 });
console.log(expanded); // { top: 5, right: 25, bottom: 15, left: 15 }

// Shrink the spacing
const shrunk = shrink(originalSpacing, { top: 2, right: 2, bottom: 2, left: 2 });
console.log(shrunk); // { top: 12, right: 18, bottom: 8, left: 22 }