css-modularscale
v1.1.1
Published
Modular scale calculator built into your CSS
Downloads
1,331
Maintainers
Readme
Modular Scale
A modular scale is a list of values that share the same relationship. These values are often used to size type and create a sense of harmony in a design. Proportions within modular scales are all around us from the spacing of the joints on our fingers to branches on trees. These natural proportions have been used since the time of the ancient Greeks in architecture and design and can be a tremendously helpful tool to leverage for web designers.
Ems work especially well with modular scales as their recursive properties mimic modular scales making them more predictable and easier to manage. Pixels and other units work just fine and breakpoints in responsive web design can naturally fall on your scale to create better relevance to your text as all the values in your layout harmonize with each other.
To get started, you need to select a ratio and a base value. The base value is usually your text font size or 1em. This base size paired with a ratio such as the golden ratio or any musical proportion will create your scale of values which all share this proportion.
Install
npm install css-modularscale
- then, in your css:
@import "css-modularscale"
Compatability
css-modularscale
is created with future CSS syntax in mind. For more information about CSS variables and the spec, consult The W3C's Working Draft on the topic.
You can use any CSS "postprocessor" that has support for custom properties, such as PostCSS or Rework. If you want to get started fast, I recommend checking out the CSSNext project.
Usage
Modular Scale has two default variables that you should place with your other site wide variables. --ms-base
is usually your font size or 1rem
. --ms-ratio
is the factor of change between each number so if the ratio is 1.5
then each number in the sequence will be 1.5 times that of the previous number.
:root {
--ms-base: 1em;
--ms-ratio: var(--golden);
}
Modular-scale is used as a custom property. Simply pass it through in place of any value to use a value based on a modular scale.
font-size: var(--ms2); // two up the modular scale
Ratios
Modular scale includes functions for a number of classic design and musical scale ratios. You can add your own ratios as well.
By default, the variable --ms-ratio
is set to --golden
.
Add your own ratio in CSS by setting a variable and passing that to modular-scale.
:root {
--my-ratio: calc(1 / 3.14159265);
--ms-ratio: var(--my-ratio);
}