2

I am trying to understand the math behind creating a solid baseline grid in CSS/SASS.

So far I am able to set the text to a grid for a given page, but as soon as a heading changes it's length and wraps, all following elements are off the grid.

Take this simple HTML code:

<h1>A big greyish rounded bulk, the size, perhaps, of a bear, was</h1>
<p>A big greyish rounded bulk, the size, perhaps, of a bear, was rising slowly and painfully out of the cylinder.  As it bulged up and caught the light, it glistened like wet leather.</p>
<p>Two large dark-coloured eyes were regarding me steadfastly.  The mass that framed them, the head of the thing, was rounded, and had, one might say, a face.  There was a mouth under the eyes, the lipless brim of which quivered and panted, and dropped saliva.  The whole creature heaved and pulsated convulsively.  A lank tentacular appendage gripped the edge of the cylinder, another swayed in the air.</p>

Together with this SASS code:

@import url("http://fonts.googleapis.com/css?family=Open+Sans:400,600");

// These are the basic values to set up a baseline & vertical rhythm
$font-size: 16px;
$line-height: 24px;
$rhythm-unit: $line-height;

html {
    background-image: url(http://basehold.it/i/24);
    font-family: "Open Sans";
    // Converting the font size to em
    font-size: ($font-size / 16px) * 1em;
    // Converting the line height to a unitless number
    line-height: $line-height / $font-size;
}

// Vertical rhythm & single margin direction
h1,
p {
  margin-top: 0;
  margin-bottom: $rhythm-unit;
}

h1 {
  // Arbitrary values that look good
  font-size: 3.375em;
  line-height: 1.3;

  // To now restore the baseline grid, we need to apply
  // a margin bottom value that accomodates for font size
  // and line height.
  //
  // Font size in px: 3.375 * $font-size = 54px
  // The next multiple of our $rhythm-unit is 56. That
  // means 2px need to be shifted.
  // 2px in em: 2 / 54px = 0.03703703704;
  margin-bottom: 0.03703703704em;

  // Now that we have accomodated for font size, we need
  // to do the same with line height.
  // Line height in px: 1.3 * 54px = 70.2px
  // The next multiple of our $rhythm-unit is 72. That
  // means 1.8px need to be shifted.
  // 1.8px in em: 1.8 / 54px = 0.03333333333;
  margin-bottom: 0.03703703704em + 0.03333333333em;
}

As you can see, I heavily commented it hoping it's easier to understand the Live Example.

To see the behavior described, just change the length of the <h1> and look what happens to the following paragraphs:

OK enter image description here

NOT OK enter image description here

In this example the effect may be minimal, but for longer texts it starts to shift stronger and stronger.

I really don't understand why it either works for multi line headings, or for one line headings. But much more important: Is it even possible to set a baseline grid following my requirements:

  • All text related measurements in em
  • font-size & line-height need to be set freely, not following some multiples or the like
  • line-height needs to be unitless
  • I don't know how much lines a heading or paragraph will wrap.
Sven
  • 12,997
  • 27
  • 90
  • 148
  • first of all, I can't see the difference between the "OK" screenshot and the "not ok" screenshot. But I would dispense with the silly `font-size: ($font-size / 16px) * 1em;` and just write the size in pixels that you want. (Either that, or respect the user's default preferences and don't change the font size at all!) – Mr Lister Dec 28 '14 at 13:29

1 Answers1

0

Thankfully this math has been figured out for you. You can:

Good luck!

xiwcx
  • 227
  • 1
  • 4