CSS基线网格:标题&包皮

时间:2014-12-27 11:53:15

标签: html css sass grid-computing baseline

我试图了解在CSS / SASS中创建实体基线网格背后的数学。

到目前为止,我可以将文本设置为给定页面的网格,但是一旦标题更改了它的长度并换行,所有后续元素都将离开网格。

采用这个简单的HTML代码:

<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>

与此SASS代码一起:

@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;
}

正如你所看到的,我对它进行了大量评论,希望能更容易理解Live Example

要查看所描述的行为,只需更改<h1>的长度,然后查看以下段落会发生什么:

enter image description here

不行 enter image description here

在这个例子中,效果可能很小,但对于较长的文本,它开始变得越来越强大。

我真的不明白为什么它适用于多行标题或一行标题。但更为重要的是:甚至可以按照我的要求设置基准网格:

  • em中所有与文本相关的测量
  • font-size&amp; line-height需要自由设定,而不是遵循一些倍数或类似情况
  • line-height需要无单位
  • 我不知道标题或段落会包裹多少行。

1 个答案:

答案 0 :(得分:0)

幸好这个数学已经为你找到了。你可以:

祝你好运!