npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

line-height

v0.3.1

Published

Calculate line-height of an HTML element (IE6 compatible)

Downloads

975,095

Readme

line-height Build status

Calculate line-height of an HTML element (IE6 compatible)

This was created for provide a well-tested module for calculating line-height in pixels for trunkata, a line-based truncation library for HTML.

Getting Started

line-height is available via the following:

For npm and component, you can load it in as follows:

var lineHeight = require('line-height');

For bower and http, you can use vanilla JS

<script src="components/line-height.js"></script>
window.lineHeight; // `line-height` is defined on `window` in camelCase

or you can use AMD

require(['line-height'], funtion (lineHeight) { /* code */ });

or CommonJS syntax (see npm/component section).

Once you have the module loaded, you can get the line-height of any node in the DOM.

// Calculate the `line-height` of the body
lineHeight(document.body); // 19

// Calculate the `line-height` of an h2
var h2 = document.createElement('h2');
document.body.appendChild(h2);
lineHeight(h2); // 29

// Calculate how many lines tall an element is
var div = document.createElement('div');
div.innerHTML = '<p>1</p><p>2</p>';
(lineHeight(div) / div.offsetHeight); // 2, how trunkata performs its calculations

Donations

Support this project and others by twolfson via donations

Documentation

line-height provides a single function.

lineHeight(node);
/**
 * Calculate the `line-height` of a given node
 * @param {HTMLElement} node Element to calculate line height of. Must be in the DOM.
 * @returns {Number} `line-height` of the element in pixels
 */

Solved problems

line-height: normal

In a large amount of browsers, the computed style for an element's line-height is normal by default.

If it is specified by any other means (e.g. ancestor has a line-height or the element has a line-height specified), it is either a CSS length.

To solve this problem, we create a vanilla element of the same nodeName (e.g. h2 if it is an h2), apply the original element's font-size, and return the element offsetHeight. This is the height of 1 line of the element (i.e. line-height).

Converting pt, pc, in, cm, mm to px

In most browsers, when the line-height is specified in pt, pc, in, cm or mm, the computedStyle value is in the same unit.

To solve this problem, we use the standard ratios of conversion to pixels to make a conversion to pixels.

  • 3pt to 4px
  • 1pc to 16px
  • 1in to 96px
  • 2.54cm to 96px
  • 25.4mm to 96px

numeric font-size in IE6

In IE6, numeric font-sizes (e.g. font-size: 2.3) are returned without a unit.

To solve this problem, we treat this number as an em since it is relative as well. To do that, we set the element's style to "numeric value" + "em", compute and save the font-size, remove the temporary style. This conversion gives us the unit in pt which we know how to deal with from before.

Development

Testing

Tests can be run once via:

npm test
# Or with Karma directly via
# npm run test-karma-single

Tests can also be run continuously via:

npm run test-karma-single

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint via npm run lint and test via npm test.

License

Copyright (c) 2013 Todd Wolfson

Licensed under the MIT license.