Code Monkey home page Code Monkey logo

plumber-sass's Introduction

Plumber

https://jamonserrano.github.io/plumber-sass

Create better looking documents and speed up CSS development by adding vertical rhythm to your page.

Looking for the postcss plugin? Go to https://github.com/jamonserrano/postcss-plumber.

What is it?

Using a baseline grid on the web is not easy. For every font family and size you have to measure where the letters sit so you can shift the text to the nearest gridline. After this, you have to precariously add margins and paddings to keep the vertical rhythm.

Plumber provides a single SASS mixin that does all the hard work for you.

Installation

Manual

Download and extract the latest release, move _plumber.scss into the vendor folder of your project and include it:

@import "vendor/plumber";

NPM / Yarn

Install:

# NPM
$ npm install plumber-sass --save-dev

#Yarn
$ yarn add plumber-sass --dev

And import it in your project:

@import "node_modules/plumber-sass/plumber";

Bower

Install:

$ bower install plumber-sass --save-dev

And import it in your project:

@import "bower_components/plumber-sass/plumber";

Usage

1. Decide on the vertical grid height you will use in the unit of your choice (pixels or rems are recommended).

2. Look up the baseline ratio of your font family in the table or use the measure tool. For example the value for Roboto is 0.158203.

3. Include the plumber mixin in your styles – specify font size as a fraction, line height, top and bottom leadings as multiples of the grid height:

p {
	@include plumber(
		$grid-height: 1rem,
		$baseline: 0.158203,
		$font-size: 1.75,
		$line-height: 3,
		$leading-top: 1,
		$leading-bottom: 2
	);
	font-family: Roboto, sans-serif;
}

This will output the following CSS:

p {
	font-size: 1.75rem;
	line-height: 3rem;
	margin-top: 0;
	padding-top: 0.901855rem;
	padding-bottom: 0.098145rem;
	margin-bottom: 2rem;
	font-family: Roboto, sans-serif;
}

For a real-world example take a look at the code of the documentation page.

Default settings

To avoid repetition set up default values before using the mixin:

@include plumber-set-defaults(
	$grid-height: 1rem,
	$baseline: 0.158203,
	$font-size: 1.75,
	$line-height: 3,
	$leading-top: 1,
	$leading-bottom: 2
);

p {
	// using only default values
	@include plumber();
}

li {
	// override leadings
	@include plumber(
		$leading-top: 0,
		$leading-bottom: 1
	);
}

Using multiple fonts

When using multiple font families just add different $baseline parameters:

$body-font: Roboto, sans-serif;
$body-baseline: 0.158203;

$quote-font: Georgia, serif;
$quote-baseline: 0.151123;

p {
	@include plumber($baseline: $body-baseline);
	font-family: $body-font;
}

blockquote {
	@include plumber($baseline: $quote-baseline);
	font-family: $quote-font;
}

Responsive typography

For responsive typography define the grid height in rems or other relative units, and metrics will change along.

@include plumber-set-defaults(
	$grid-height: 1rem,
	$font-size: 1.75
);

html {
	font-size: 8px;
	// grid height => 8px, font size => 14px
	
	@media screen and (min-width: 641px) {
		font-size: 12px;
		// grid-height => 12px, font size => 21px
	}
}

Alternative leading calculation

Leadings are measured from the top and bottom edges of the text block by default. To measure them from the baseline, set $use-baseline-origin: true before using the mixin:

@include plumber-set-defaults($use-baseline-origin: true);

Considerations

Precision

Due to rounding and browser rendering it’s entirely possible that the text will not sit exactly on the baseline. Following these guidelines will get you closer to pixel perfection:

  • Define grid height in pixels, or as a multiple of the base font height.
  • Use a grid height with many divisors.
  • Use font sizes that produce whole numbers with the grid height.

If you have access to the OpenType metrics of the font you can calculate a more precise baseline ratio with the following formula: (UnitsPerEm − hhea.Ascender − hhea.Descender) / (2 × UnitsPerEm)

Varying baseline among fonts in the same family

Although some weights or styles in the same family can sit on different baselines, it’s generally fine to use the one for the regular font. If pixel perfection is important, set individual baselines for each font.

Viewport-specific units

While supported, using vh, vw, vmin, vmax for the grid height can lead to catastrophic results.

Collapsing margins

Plumber’s use of collapsing margins makes it possible to set the minimum distance between blocks of texts. If you don’t need this, you can set either $leading-top or $leading-bottom to 0.

API

plumber

The main mixin.

Parameters: All parameters are optional, default values can be changed with plumber-set-defaults.

Name Description Type Default value
$baseline Baseline ratio Fraction between 0 and 1 —*
$font-size Font size as a fraction of grid height Positive number 2
$grid-height Grid height Any unit 1rem
$leading-top Top leading as a multiple of grid height Integer 0
$leading-bottom Bottom leading as a multiple of grid height Integer 0
$line-height Line height as a multiple of grid height Positive integer 3
$use-baseline-origin Set the origin of leadings to the baseline Boolean false

* Baseline must be provided either in the defaults or in the mixin parameters.

† Leadings are measured from either the baseline or the edges of the text block, depending on the $use-baseline-origin setting.

‡ The default value is always calculated so there will be no visible gap above or below the text block.

Output: font-size, line-height, margin-top, padding-top, padding-bottom, margin-bottom properties with the same unit as the grid height.

plumber-set-defaults

Sets up or changes default parameters to use.

Parameters: Same as the main mixin.

License

MIT License

plumber-sass's People

Contributors

garyjones avatar jamonserrano avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

plumber-sass's Issues

Add baseline ratio calculation example

Hey.
Thank you for building this project.
I have used sassline before.
Right now i am trying to implement your tool in a real life project.

The font i want to use is called Lelo

I got the following information from the Typedesigner:
Ascender: 1032
Descender: -328
Caps height: 648
x height: 468
1000 Units pro EM

So if i want to do the math.
(UnitsPerEm − hhea.Ascender − hhea.Descender) / (2 × UnitsPerEm)

[1000 - 1032 - (-328)] / [2 x 1000]
[1000 - 1360] / [2 x 1000]
-360 / 2000
= -0.18

Using the measure tool i get a value of 0.268

Maybe you can point me to a solution. Thank you!

plumber breaks with node-sass 4.9.4 on macOS Mojave

Hello jamonserrano!

I've been using plumber for a while now using both grunt (node-sass, older version) and gulp (gulp-sass). In my current project I'm using strictly npm scripts to build my front-end layer and I'm having trouble with plumber causing node-sass to crash when compiling.

The first to issues was regarding variables. I had to move line 97 and 98 to be above line 96 eding up with this order

$line-height: $line-height * $grid-height;
$baseline-from-bottom: ($line-height - $font-size) / 2 + ($font-size * $baseline);
$corrected-baseline: round($baseline-from-bottom);
$baseline-difference: $corrected-baseline - $baseline-from-bottom;
`

But I also get a more serious error on line 115 and 117 saying

"message": "0.943remrem isn't a valid CSS value.",
"formatted": "Error: 0.23575rem
rem isn't a valid CSS value.\n on line 106 of src/vendor/baseline/plumber.scss\n from line 13 of src/app.scss\n>> \t\t$padding-top: - $baseline-difference * $grid-height;\n\n ----------------^\n"

That's referring to these two lines of code

$padding-top: (1 - $baseline-difference) * $grid-height;
$padding-bottom: $baseline-difference * $grid-height;

It seems like $grid-height is only getting the unit "rem" but without a value.

Have you seen this error before? Not sure how to proceed to fix it.

Thanks in advance!

Examples in production?

Hi there,

Thanks for taking the time to create this awesome plugin - I'm just wondering if you've got any real-world examples to share, specifically with complicated web apps with lots of component?

I'm thinking of using this for a betting application and would be interested to see how it works at scale.

Thanks again!

how bout a post-css version?

it'd be great to see this as a postcss plugin in addition to an scss mixin as many folks have begun to migrate from sass to postcss

Calculation method for webfonts

Hi there,

I'm unable to download webfonts from Typekit/Fonts.com. Any advice on how to calculate baseline ratio with lesser-known typefaces?

Thanks!

Sass version

Hello

I apologise in advance for my lack of knowledge. I wanted to know if it was possible for my website which has been setup using .sass preprocesser to use Plumber? I ask as Plumber is available as a '.scss' or 'post-css', but I could not see a '.sass' version. Thank you

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.