bootstrap 中的 less

2019-07-05 10:02:22 浏览数 (2)

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>

0 人点赞