bootstrap 中的 less

转载:http://www.waibo.wang/bible/bootstrap3/html/2/2.11.1.html
Bootstrap 的 CSS 文件是通过 Less 源码编译而来的。Less 是一门预处理语言,支持变量、mixin、函数等额外功能。

对于希望使用 Less 源码而非编译而来的 CSS 文件的用户,Bootstrap 框架中包含的大量变量、mixin 将非常有价值。

可以通过两种方式使用 Bootstrap:使用编译后的 CSS 文件,或者使用 Less 源码文件。若要编译 Less 文件,请参考编译Bootstrap的内容,以了解如何设置开发环境并运行必须的编译指令。

变量

整个 Bootstrap 项目中使用了大量的变量,这些变量被用来代表颜色、内边距、外边距、字体等。详细内容请参考定制工具

1、颜色

Bootstrap 使用了两种颜色模式:灰度颜色和语义颜色。

灰度颜色用于快速获取常用的黑色色调。如:

<pre class="prettyprint linenums" style="padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 13px; color: rgb(51, 51, 51); border-radius: 4px; display: block; margin: 0px 0px 20px; line-height: 20px; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap; background: rgb(255, 255, 255); border: 1px solid rgb(245, 245, 245); box-shadow: rgb(251, 251, 252) 44px 0px 0px inset, rgb(245, 245, 245) 45px 0px 0px inset; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 1.4px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

  1. @gray-darker: lighten(#000, 13.5%); // #222
  2. @gray-dark: lighten(#000, 20%); // #333
  3. @gray: lighten(#000, 33.5%); // #555
  4. @gray-light: lighten(#000, 46.7%); // #777
  5. @gray-lighter: lighten(#000, 93.5%); // #eee

</pre>

得到的颜色如图 2‑84所示:

灰度颜色

<figcaption style="display: block; margin-top: 1em; font-size: 0.86em; font-family: Georgia, SimHei; white-space: pre;">图2-84 灰度颜色</figcaption>

语义颜色包含了各种赋予语义的颜色值。如:

<pre class="prettyprint linenums" style="padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 13px; color: rgb(51, 51, 51); border-radius: 4px; display: block; margin: 0px 0px 20px; line-height: 20px; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap; background: rgb(255, 255, 255); border: 1px solid rgb(245, 245, 245); box-shadow: rgb(251, 251, 252) 44px 0px 0px inset, rgb(245, 245, 245) 45px 0px 0px inset; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 1.4px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

  1. @brand-primary: darken(#428bca, 6.5%); // #337ab7
  2. @brand-success: #5cb85c;
  3. @brand-info: #5bc0de;
  4. @brand-warning: #f0ad4e;
  5. @brand-danger: #d9534f;

</pre>

得到的颜色如图 2‑85所示:

语义颜色

<figcaption style="display: block; margin-top: 1em; font-size: 0.86em; font-family: Georgia, SimHei; white-space: pre;">图2-85 语义颜色</figcaption>

在你的项目中,你可以直接使用这些预定义的颜色变量,或者重新为其赋予别名,使其更有语义。如:

<pre class="prettyprint linenums" style="padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 13px; color: rgb(51, 51, 51); border-radius: 4px; display: block; margin: 0px 0px 20px; line-height: 20px; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap; background: rgb(255, 255, 255); border: 1px solid rgb(245, 245, 245); box-shadow: rgb(251, 251, 252) 44px 0px 0px inset, rgb(245, 245, 245) 45px 0px 0px inset; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 1.4px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

  1. // 直接使用

  2. .masthead {

  3. background-color: @brand-primary;

  4. }

  5. // 使用别名

  6. @alert-message-background: @brand-info;

  7. .alert {

  8. background-color: @alert-message-background;

  9. }

</pre>

2、脚手架

某几个变量是改变网站外观的关键要素。如:

<pre class="prettyprint linenums" style="padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 13px; color: rgb(51, 51, 51); border-radius: 4px; display: block; margin: 0px 0px 20px; line-height: 20px; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap; background: rgb(255, 255, 255); border: 1px solid rgb(245, 245, 245); box-shadow: rgb(251, 251, 252) 44px 0px 0px inset, rgb(245, 245, 245) 45px 0px 0px inset; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 1.4px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

  1. @body-bg: #fff;
  2. @text-color: @black-50;

</pre>

3、链接

仅仅通过改变一个变量,可以很容易地为链接赋予正确的颜色。如:

<pre class="prettyprint linenums" style="padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 13px; color: rgb(51, 51, 51); border-radius: 4px; display: block; margin: 0px 0px 20px; line-height: 20px; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap; background: rgb(255, 255, 255); border: 1px solid rgb(245, 245, 245); box-shadow: rgb(251, 251, 252) 44px 0px 0px inset, rgb(245, 245, 245) 45px 0px 0px inset; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 1.4px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

  1. // 变量

  2. @link-color: @brand-primary;

  3. @link-hover-color: darken(@link-color, 15%);

  4. // 使用

  5. a {

  6. color: @link-color;

  7. text-decoration: none;

  8. &:hover {

  9. color: @link-hover-color;

  10. text-decoration: underline;

  11. }

  12. }

</pre>

注意:@link-hover-color使用了 Less 提供的一个内置函数,用于自动为鼠标悬停设置合适的颜色。你还可以使用darkenlightensaturatedesaturate等 Less 内置的函数。

4、排版

通过几个变量就能轻松的设置字体、字号、行距等。Bootstrap 利用这些变量提供了简单地定制排版的功能。

<pre class="prettyprint linenums" style="padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 13px; color: rgb(51, 51, 51); border-radius: 4px; display: block; margin: 0px 0px 20px; line-height: 20px; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap; background: rgb(255, 255, 255); border: 1px solid rgb(245, 245, 245); box-shadow: rgb(251, 251, 252) 44px 0px 0px inset, rgb(245, 245, 245) 45px 0px 0px inset; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 1.4px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

  1. @font-family-sans-serif: "Helvetica Neue", Helvetica, Arial, sans-serif;

  2. @font-family-serif: Georgia, "Times New Roman", Times, serif;

  3. @font-family-monospace: Menlo, Monaco, Consolas, "Courier New", monospace;

  4. @font-family-base: @font-family-sans-serif;

  5. @font-size-base: 14px;

  6. @font-size-large: ceil((@font-size-base * 1.25)); // ~18px

  7. @font-size-small: ceil((@font-size-base * 0.85)); // ~12px

  8. @font-size-h1: floor((@font-size-base * 2.6)); // ~36px

  9. @font-size-h2: floor((@font-size-base * 2.15)); // ~30px

  10. @font-size-h3: ceil((@font-size-base * 1.7)); // ~24px

  11. @font-size-h4: ceil((@font-size-base * 1.25)); // ~18px

  12. @font-size-h5: @font-size-base;

  13. @font-size-h6: ceil((@font-size-base * 0.85)); // ~12px

  14. @line-height-base: 1.428571429; // 20/14

  15. @line-height-computed: floor((@font-size-base * @line-height-base)); // ~20px

  16. @headings-font-family: inherit;

  17. @headings-font-weight: 500;

  18. @headings-line-height: 1.1;

  19. @headings-color: inherit;

</pre>

5、图标

以下两个变量用于设置图标文件的位置和文件名。

<pre class="prettyprint linenums" style="padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 13px; color: rgb(51, 51, 51); border-radius: 4px; display: block; margin: 0px 0px 20px; line-height: 20px; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap; background: rgb(255, 255, 255); border: 1px solid rgb(245, 245, 245); box-shadow: rgb(251, 251, 252) 44px 0px 0px inset, rgb(245, 245, 245) 45px 0px 0px inset; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 1.4px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

  1. @icon-font-path: "../fonts/";
  2. @icon-font-name: "glyphicons-halflings-regular";

</pre>

6、组件

组件贯穿整个 Bootstrap 框架,他们通过一些变量来设置默认值。下面列出的是常用的几个。

<pre class="prettyprint linenums" style="padding: 8px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 13px; color: rgb(51, 51, 51); border-radius: 4px; display: block; margin: 0px 0px 20px; line-height: 20px; word-break: break-all; overflow-wrap: break-word; white-space: pre-wrap; background: rgb(255, 255, 255); border: 1px solid rgb(245, 245, 245); box-shadow: rgb(251, 251, 252) 44px 0px 0px inset, rgb(245, 245, 245) 45px 0px 0px inset; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: 1.4px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

  1. @padding-base-vertical: 6px;

  2. @padding-base-horizontal: 12px;

  3. @padding-large-vertical: 10px;

  4. @padding-large-horizontal: 16px;

  5. @padding-small-vertical: 5px;

  6. @padding-small-horizontal: 10px;

  7. @padding-xs-vertical: 1px;

  8. @padding-xs-horizontal: 5px;

  9. @line-height-large: 1.33;

  10. @line-height-small: 1.5;

  11. @border-radius-base: 4px;

  12. @border-radius-large: 6px;

  13. @border-radius-small: 3px;

  14. @component-active-color: #fff;

  15. @component-active-bg: @brand-primary;

  16. @caret-width-base: 4px;

  17. @caret-width-large: 5px;

</pre>

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,451评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,172评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,782评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,709评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,733评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,578评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,320评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,241评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,686评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,878评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,992评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,715评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,336评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,912评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,040评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,173评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,947评论 2 355

推荐阅读更多精彩内容