All Projects → alexreardon → Css Box Model

alexreardon / Css Box Model

Licence: mit
Get accurate and well named css box model information about an Element 📦

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Css Box Model

Calendarhtml Javascript
Simple Calendar built with Pure JavaScript (No Libraries) http://iamnitinpatel.com/projects/calendar/
Stars: ✭ 113 (-16.91%)
Mutual labels:  dom
Dom7
Minimalistic JavaScript library for DOM manipulation, with a jQuery-compatible API
Stars: ✭ 119 (-12.5%)
Mutual labels:  dom
Holmes
Fast and easy searching inside a page
Stars: ✭ 1,679 (+1134.56%)
Mutual labels:  dom
Elemental2
Type checked access to browser APIs for Java code.
Stars: ✭ 115 (-15.44%)
Mutual labels:  dom
Jsx Dom
Use JSX to create DOM elements.
Stars: ✭ 117 (-13.97%)
Mutual labels:  dom
Femtojs
femtoJS - Really small JavaScript (ES6) library for DOM manipulation.
Stars: ✭ 122 (-10.29%)
Mutual labels:  dom
Interview Problem Summary
🎤 Prepare for the interviews and sum up the most popular interview problems for front-end(HTML/CSS/Javascript), Web development, full-stack. Also did some typical coding practice questions, such as UI caculator
Stars: ✭ 112 (-17.65%)
Mutual labels:  dom
Nodom
fake DOM for RE:DOM
Stars: ✭ 133 (-2.21%)
Mutual labels:  dom
Binding.scala
Reactive data-binding for Scala
Stars: ✭ 1,539 (+1031.62%)
Mutual labels:  dom
Tempy
Python Object Oriented Html Templating System
Stars: ✭ 126 (-7.35%)
Mutual labels:  dom
Marko
A declarative, HTML-based language that makes building web apps fun
Stars: ✭ 10,796 (+7838.24%)
Mutual labels:  dom
Box Cli Maker
Make Highly Customized Boxes for your CLI
Stars: ✭ 115 (-15.44%)
Mutual labels:  box
Dom I18n
Provides a very basic HTML multilingual support using JavaScript
Stars: ✭ 125 (-8.09%)
Mutual labels:  dom
Dna.js
🧬 An uncomplicated user interface library for building data-driven semantic templates
Stars: ✭ 114 (-16.18%)
Mutual labels:  dom
Chai Webdriver
Build more expressive integration tests with webdriver sugar for chai.js
Stars: ✭ 128 (-5.88%)
Mutual labels:  dom
Knowledge
文档着重构建一个完整的「前端技术架构图谱」,方便 F2E(Front End Engineering又称FEE、F2E) 学习与进阶。
Stars: ✭ 1,620 (+1091.18%)
Mutual labels:  dom
Test State
Scala Test-State.
Stars: ✭ 119 (-12.5%)
Mutual labels:  dom
Robojs
RoboJS is a library that aims to dynamically load JS modules depending on how the DOM is composed.
Stars: ✭ 133 (-2.21%)
Mutual labels:  dom
Box
Python dictionaries with advanced dot notation access
Stars: ✭ 1,804 (+1226.47%)
Mutual labels:  box
Learnvue
Vue.js 源码解析
Stars: ✭ 11,516 (+8367.65%)
Mutual labels:  dom

css-box-model 📦

Get accurate and well named CSS Box Model information about a Element.

Build Status npm dependencies Downloads per month min minzip

Any time you are using Element.getBoundingClientRect() you might want to consider using css-box-model instead to get more detailed box model information.

Usage

// @flow
import { getBox } from 'css-box-model';

const el: HTMLElement = document.getElementById('foo');
const box: BoxModel = getBox(el);

// profit

Installation

## yarn
yarn add css-box-model

# npm
npm install css-box-model --save

The CSS Box Model

the box model

Box type Composition
Margin box margin + border + padding + content
Border box border + padding + content
Padding box padding + content
Content box content

This our returned BoxModel:

export type BoxModel = {|
  // content + padding + border + margin
  marginBox: Rect,
  // content + padding + border
  borderBox: Rect,
  // content + padding
  paddingBox: Rect,
  // content
  contentBox: Rect,
  // for your own consumption
  border: Spacing,
  padding: Spacing,
  margin: Spacing,
|};

// Supporting types

// This is an extension of DOMRect and ClientRect
export type Rect = {|
  // ClientRect
  top: number,
  right: number,
  bottom: number,
  left: number,
  width: number,
  height: number,
  // DOMRect
  x: number,
  y: number,
  // Rect
  center: Position,
|};

export type Position = {|
  x: number,
  y: number,
|};

export type Spacing = {
  top: number,
  right: number,
  bottom: number,
  left: number,
};

API

getBox

(el: HTMLElement) => BoxModel

Use getBox to return the box model for an element

withScroll

(original: BoxModel, scroll?: Position = getWindowScroll()) => BoxModel

This is useful if you want to know the box model for an element relative to a page

const el: HTMLElement = document.getElementById('app');
const box: BoxModel = getBox(el);
const withScroll: BoxModel = withScroll(box);

You are welcome to pass in your own scroll. By default we we use the window scroll:

const getWindowScroll = (): Position => ({
  x: window.pageXOffset,
  y: window.pageYOffset,
});

calculateBox

(borderBox: AnyRectType, styles: CSSStyleDeclaration) => BoxModel

This will do the box model calculations without needing to read from the DOM. This is useful if you have already got a ClientRect / DOMRect and a CSSStyleDeclaration as then we can skip computing these values.

const el: HTMLElement = document.getElementById('app');
const borderBox: ClientRect = el.getBoundingClientRect();
const styles: CSSStyleDeclaration = window.getComputedStyles(el);

const box: BoxModel = calculateBox(borderBox, styles);

AnyRectType allows for simple interoperability with any rect type

type AnyRectType = ClientRect | DOMRect | Rect | Spacing;

createBox

({ borderBox, margin, border, padding }: CreateBoxArgs) => BoxModel

Allows you to create a BoxModel by passing in a Rect like shape (AnyRectType) and optionally your own margin, border and or padding.

type CreateBoxArgs = {|
  borderBox: AnyRectType,
  margin?: Spacing,
  border?: Spacing,
  padding?: Spacing,
|};
const borderBox: Spacing = {
  top: 10,
  right: 100,
  left: 20,
  bottom: 80,
};
const padding: Spacing = {
  top: 10,
  right: 20,
  left: 20,
  bottom: 10,
};

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

Utility API

Functions to help you interact with the objects we provide

getRect

(spacing: AnyRectType) => Rect

Given any Rect like shape, return a Rect. Accepts any object that has top, right, bottom and right (eg ClientRect, DOMRect);

const spacing: Spacing = {
  top: 0,
  right: 100,
  bottom: 50,
  left: 50,
};

const rect: Rect = getRect(spacing);

console.log(rect);

/*
{
  top: 0,
  right: 100,
  bottom: 50,
  left: 50,
  width: 100,
  height: 50,
  x: 0,
  y: 0,
  center: { x: 50, y: 50 },
}
*/

expand

Used to expand a Spacing

(target: Spacing, expandBy: Spacing) => Spacing;
const original: Spacing = {
  top: 10,
  left: 11,
  right: 21,
  bottom: 22,
};

const expandBy: Spacing = {
  top: 1,
  left: 2,
  right: 3,
  bottom: 4,
};

const expanded: Spacing = expand(original, expandBy);

console.log(expanded);

/*
{
  // pulled back
  top: 8,
  left: 8
  // pushed forward
  bottom: 22,
  right: 22,
}
*/

shrink

Used to shrink a Spacing

(target: Spacing, shrinkBy: Spacing) => Spacing;
const original: Spacing = {
  top: 10,
  left: 10,
  right: 20,
  bottom: 20,
};

const shrinkBy: Spacing = {
  top: 2,
  left: 2,
  right: 2,
  bottom: 2,
};

const smaller: Spacing = shrink(original, shrinkBy);

console.log(smaller);

/*
{
  // pushed forward
  top: 12,
  left: 12
  // pulled back
  bottom: 18,
  right: 18,
}
*/
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].