萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> Div CSS 教學

CSS 基礎重置與通用工具類 base.css 完整版(現代化整理)

base.css 定位與價值

base.css(或稱 reset.css、normalize.css)是項目樣式系統的基石,核心目標:

  1. 重置瀏覽器預設樣式:消除跨瀏覽器差異(margin、padding、border、list-style、font-size 等)
  2. 建立統一基準:為後續組件樣式提供可預測的起點
  3. 提供通用工具類:佈局、間距、字體、定位等高頻原子類,提升開發效率

現代化 Reset 區塊(推薦:Normalize.css 思想)

/* ===== 1. 現代化 Reset ===== */
/* 參考 Normalize.css + 現代項目實踐 */
*, *::before, *::after {
    box-sizing: border-box;           /* 統一盒模型,避免 padding 撐大 */
}

html {
    line-height: 1.5;                 /* 統一行高基準 */
    -webkit-text-size-adjust: 100%;   /* 防止 iOS 橫屏字體放大 */
    -moz-tab-size: 4; tab-size: 4;    /* 統一 Tab 寬度 */
}

body {
    margin: 0;                        /* 移除默認 margin */
    font-family: system-ui, -apple-system, BlinkMacSystemFont, 
                 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
    color: #1a1a1a;
    background: #fff;
}

/* 移除列表默認樣式 */
ol, ul, menu { list-style: none; margin: 0; padding: 0; }

/* 表格邊框合併 */
table { border-collapse: collapse; border-spacing: 0; }

/* 圖片/媒體預設響應式 */
img, video, canvas, svg {
    display: block;
    max-width: 100%;
    height: auto;
    vertical-align: middle;
}

/* 表單元素字體繼承 */
input, button, textarea, select {
    font: inherit;
    color: inherit;
}

/* 移除按鈕默認樣式 */
button, [type="button"], [type="reset"], [type="submit"] {
    background: none;
    border: none;
    cursor: pointer;
}

/* 連結無文字裝飾 */
a { text-decoration: none; color: inherit; }
a:hover { text-decoration: underline; }

/* 語義化標籤顯式 display:block(舊瀏覽器) */
article, aside, footer, header, nav, section, main, figure, figcaption {
    display: block;
}

通用工具類系統(原子化 CSS 思想)

字體與文字

/* 字號階梯 */
.fs-xs { font-size: 0.75rem; }   /* 12px */
.fs-sm { font-size: 0.875rem; }  /* 14px */
.fs-base { font-size: 1rem; }    /* 16px */
.fs-lg { font-size: 1.125rem; }  /* 18px */
.fs-xl { font-size: 1.25rem; }   /* 20px */
.fs-2xl { font-size: 1.5rem; }   /* 24px */
.fs-3xl { font-size: 1.875rem; } /* 30px */
.fs-4xl { font-size: 2.25rem; }  /* 36px */

.fw-normal { font-weight: 400; }
.fw-medium { font-weight: 500; }
.fw-semibold { font-weight: 600; }
.fw-bold { font-weight: 700; }

.text-left { text-align: left; }
.text-center { text-align: center; }
.text-right { text-align: right; }
.text-justify { text-align: justify; }

.truncate {                        /* 單行省略號 */
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.line-clamp-2 {                    /* 多行省略號 */
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}
.line-clamp-3 { -webkit-line-clamp: 3; }

間距系統(基於 4px 基準單位)

/* Margin - 外距 */
.m-0 { margin: 0; }
.m-auto { margin: auto; }
.mx-auto { margin-left: auto; margin-right: auto; }
.my-auto { margin-top: auto; margin-bottom: auto; }

.mt-1 { margin-top: 0.25rem; }   /* 4px */
.mt-2 { margin-top: 0.5rem; }    /* 8px */
.mt-3 { margin-top: 0.75rem; }   /* 12px */
.mt-4 { margin-top: 1rem; }      /* 16px */
.mt-6 { margin-top: 1.5rem; }    /* 24px */
.mt-8 { margin-top: 2rem; }      /* 32px */
.mt-12 { margin-top: 3rem; }     /* 48px */
/* mb, ml, mr, mx, my 同理 */

/* Padding - 內距 */
.p-0 { padding: 0; }
.p-1 { padding: 0.25rem; }
.p-2 { padding: 0.5rem; }
.p-3 { padding: 0.75rem; }
.p-4 { padding: 1rem; }
.p-6 { padding: 1.5rem; }
.p-8 { padding: 2rem; }
/* pt, pb, pl, pr, px, py 同理 */

佈局與定位

/* Flex 佈局 */
.flex { display: flex; }
.inline-flex { display: inline-flex; }
.flex-1 { flex: 1 1 0%; }
.flex-auto { flex: 1 1 auto; }
.flex-initial { flex: 0 1 auto; }
.flex-none { flex: none; }
.flex-col { flex-direction: column; }
.flex-wrap { flex-wrap: wrap; }
.items-center { align-items: center; }
.items-start { align-items: flex-start; }
.items-end { align-items: flex-end; }
.justify-center { justify-content: center; }
.justify-between { justify-content: space-between; }
.justify-around { justify-content: space-around; }
.gap-1 { gap: 0.25rem; }
.gap-2 { gap: 0.5rem; }
.gap-3 { gap: 0.75rem; }
.gap-4 { gap: 1rem; }

/* Grid 佈局 */
.grid { display: grid; }
.grid-cols-2 { grid-template-columns: repeat(2, 1fr); }
.grid-cols-3 { grid-template-columns: repeat(3, 1fr); }
.grid-cols-4 { grid-template-columns: repeat(4, 1fr); }

/* 定位 */
.relative { position: relative; }
.absolute { position: absolute; }
.fixed { position: fixed; }
.sticky { position: sticky; }
.inset-0 { top: 0; right: 0; bottom: 0; left: 0; }
.z-10 { z-index: 10; }
.z-50 { z-index: 50; }

/* 浮動清除 */
.clearfix::after {
    content: "";
    display: table;
    clear: both;
}
.float-left { float: left; }
.float-right { float: right; }

尺寸與邊框

/* 寬高 */
.w-full { width: 100%; }
.w-screen { width: 100vw; }
.h-full { height: 100%; }
.h-screen { height: 100vh; }
.min-h-screen { min-height: 100vh; }
.max-w-md { max-width: 28rem; }  /* 448px */
.max-w-lg { max-width: 32rem; }  /* 512px */
.max-w-xl { max-width: 36rem; }  /* 576px */
.max-w-2xl { max-width: 42rem; } /* 672px */

/* 邊框 */
.border { border: 1px solid #e5e7eb; }
.border-0 { border: 0; }
.border-t { border-top: 1px solid #e5e7eb; }
.border-b { border-bottom: 1px solid #e5e7eb; }
.rounded { border-radius: 0.25rem; }
.rounded-md { border-radius: 0.375rem; }
.rounded-lg { border-radius: 0.5rem; }
.rounded-full { border-radius: 9999px; }

/* 陰影 */
.shadow { box-shadow: 0 1px 3px rgba(0,0,0,0.1); }
.shadow-md { box-shadow: 0 4px 6px rgba(0,0,0,0.1); }
.shadow-lg { box-shadow: 0 10px 15px rgba(0,0,0,0.1); }
.shadow-none { box-shadow: none; }

顯示/隱藏/溢出

.block { display: block; }
.inline-block { display: inline-block; }
.inline { display: inline; }
.hidden { display: none; }
.invisible { visibility: hidden; }
.overflow-hidden { overflow: hidden; }
.overflow-auto { overflow: auto; }
.overflow-scroll { overflow: scroll; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

使用建議

  1. 引入順序:normalize.css → base.css → components.css → pages.css
  2. 按需引入:構建工具配合 PurgeCSS/Unocss/Tailwind 按需打包,避免體積膨脹
  3. 語義化類名:業務組件優先用語義類名(.btn-primary),工具類輔助微調
  4. 設計 Token 化:顏色、間距、字號提取為 CSS 變數或設計系統 Token,base.css 引用 Token

推薦成熟方案

copyright © 萬盛學電腦網 all rights reserved