第 27 集:Bootstrap 客制化 reboot 重置

此篇会介绍 Bootstrap 使用的 css reset 档案 _reboot.scss

事前准备

还不懂 css reset 的朋友,建议先阅读事前准备的文章,再继续阅读会更好消化呦。

Normalize

  • Normalize 是一个开源的 project
  • Bootstrap reboot 样式,是建立於 Normalize.css 基础之上。

优点

  • 保留一些预设的 HTML 标签的样式。(ex:body paddingh1~h6
  • 详细的文件以及清楚明了的注解。

缺点

  • 样式清的不够乾净。

原始码

使用到一只档案:

_reboot.scss:主要清除 css 样式。

介绍几个有趣的样式设置。

box-sizing

计算元素尺寸时不受 paddingborder 影响。

*,
*::before,
*::after {
  box-sizing: border-box;
}

scroll-behavior

使用锚点达到网页滚动到某个指定地方,smooth 让网页能平滑滚动到指定地方。(预设 auto,立即滚动)

@if $enable-smooth-scroll {
  @media (prefers-reduced-motion: no-preference) {
    scroll-behavior: smooth;
  }
}


Body

常用於网页全站(全域)样式设置。

<body> 标签包住所有网页元素,通常会在 body 样式中设置预设样式。

编译後的 css

body {
  margin: 0;
  font-family: var(--bs-font-family-noto-sans);
  font-size: 1rem;
  font-weight: 400;
  line-height: 1.5;
  color: #494846;
  background-color: white;
}

依照上方范例介绍

  • 假设我们在撰写样式时,没有设置 font-size,则预设大小就会是 1rem
// 保留预设样式 font-size 为 1rem。
.title {
  color: #000;
}
  • 则依照权重高低来决定覆盖顺序。(css 权重概念欢迎参考此篇介绍
// 覆盖 font-size 样式为 0.85rem。
.title {
  font-size: 0.85rem;
  color: #000;
}


Typography

举例 h1~h6 标签

预设变数

$headings-margin-bottom:      $spacer / 2 !default;
$headings-font-family:        null !default;
$headings-font-style:         null !default;
$headings-font-weight:        500 !default;
$headings-line-height:        1.2 !default;
$headings-color:              null !default;

预设样式 %heading

%heading {
  margin-top: 0; // 1
  margin-bottom: $headings-margin-bottom;
  font-family: $headings-font-family;
  font-style: $headings-font-style;
  font-weight: $headings-font-weight;
  line-height: $headings-line-height;
  color: $headings-color;
}

// h1~h6 都是使用 %heading 样式,指差别在於 font-size 值不同
h1 {
  @extend %heading;
  @include font-size($h1-font-size);
}

编译後的 css

// 由下样式可以得知 %heading 样式设置结果
h1, .h1, h2, .h2, h3, .h3, h4, .h4, h5, .h5, h6, .h6 {
  margin-top: 0;
  margin-bottom: 0.5rem;
  font-weight: 500;
  line-height: 1.2;
}

// 透过 @include font-size() 函式,来设置彼此的 font-size 样式

h1, .h1 {
  font-size: calc(1.375rem + 1.5vw);
}

@media (min-width: 1200px) {
  h1, .h1 {
    font-size: 2.5rem;
  }
}

上述介绍了几个 _reboot.scss 的样式,大家想了解更多欢迎从原始码下手,复制样式出来编译,并查看编译後的 css 样式为何,透过这种方式可以加速了解 _reboot.scss 的结果以及作用。


建议

  • 通常不会去更动 _reboot.scss 原始码。
  • 会新增一支档案去撰写全站(全域)样式,以及复写 _reboot.scss 样式。(我会命名为 _base.scss
  • 也可以自己撰写一只 css reset,若样式足够自己使用并替代 _reboot.scss,那就不需要引入 _reboot.scss


<<:  Day27 interrupt 的处理程序

>>:  D27 - 彭彭的课程# Python 实体物件的建立与使用 - 下篇 - 实体方法 - Instance Method(1)

Day27【CSS】伪类 & 伪元素

伪类和伪元素皆是从 DOM tree 抽象出来的概念, 用以修饰实际上不在 DOM tree 上的部...

[新手教学]如何使用Line Notify

首先登入到 LINE Notify 并进入到个人页面 接着选择要接收通知的聊天室,也可以透过一对一接...

Day 3 我要开始Mock了

Mock What's mock? 先想像一个画面,当我们要隔离我们的元件时,一个component...

Day 29 | 很像 Vue 的 AlpineJS(四): x-on

x-on x-on 可以简单的用从 DOM 中来触发事件,像是最常用的按钮: <button ...

[进阶指南] 深入 JSX( Day25 )

基本上,JSX 单纯只是 React.createElement(component, props...