


























以下是一份可直接颁布的《前端代码规范(HTML/CSS)》,基于高认可度的行业实践制定,包含约定细则、示例与自动化配置。你可按需补充公司名称、版本日期后正式发布。
制定部门:前端技术委员会
版本:1.0
生效日期:YYYY-MM-DD
审批人:______
统一团队 HTML/CSS 编码风格,提升代码可读性、可维护性、可访问性,降低协作成本,确保项目健康度。
本规范适用于公司所有 Web 项目的 HTML、CSS(含预处理器)代码编写及审查。第三方库文件除外。
<!DOCTYPE html>,必须声明 lang 属性。<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>页面标题</title>
</head>
<header>, <nav>, <main>, <section>, <article>, <aside>, <footer>。<b>, <i>, <font>),样式交由 CSS 处理。<br>, <img>, <input>)统一不使用 / 闭合(<br>)。<input type="checkbox" checked>。class → id → data-* → 其他属性。<img> 必须拥有 alt 属性,纯装饰图片设为 alt=""。<label>,方式如下:<label for="email">邮箱</label>
<input type="email" id="email">
role="tab", aria-expanded 等。<head> 中(避免 FOUC)。</body> 前,或使用 defer/async 属性。srcset, <picture>。loading="lazy" 进行懒加载。style="..."),样式由 CSS 实现。onclick="..."),行为由 JS 绑定。<div class="main-content">
...
</div><!-- /.main-content -->
强制使用 BEM(Block__Element--Modifier) 命名法,确保模块独立性和可读性。
/* Block */
.card { }
/* Element */
.card__title { }
.card__image { }
/* Modifier */
.card--featured { }
.card__title--large { }
命名空间补充(强烈推荐)
.l- 或 .layout-:布局.js-:JavaScript 钩子(此类名不得用于样式).is-、.has-:状态类(如 .is-active, .has-error).u-:工具类(如 .u-hidden)content: ""; 使用双引号。#fff;具备透明度时可用 rgba() 或 8 位十六进制 #RRGGBBAA。margin: 0;,而非 margin: 0px;。属性排序规则(任选一种,全项目统一)
方案 A - 分组排序(Airbnb 风格):
显示 → 定位 → 盒模型 → 排版 → 视觉 → 其他
display, position, top, width, height, margin, padding, font, color, background, border, opacity, ...
方案 B - 字母排序:
适用于自动格式化(Prettier/Stylelint 可强制)。
& 父引用)。!important,仅允许在工具类 .u-hidden 或覆盖第三方样式时使用,且需注释说明。* 做重设,改用具体选择器。min-width 媒体查询渐进增强。$screen-sm: 576px;
$screen-md: 768px;
$screen-lg: 992px;
@media (min-width: $screen-md) { ... }
/* ==========================================================================
组件:卡片列表 (Card List)
依赖:.card 组件
========================================================================== */
采用分层架构,文件组织遵循 7-1 模式:
styles/
abstracts/ (变量、混合宏、函数)
base/ (重置、排版、通用基础)
components/ (独立组件样式)
layout/ (布局、网格、头部、页脚)
pages/ (页面特有样式,少量)
themes/ (主题、暗色模式)
utilities/ (工具类,如 .u-text-center)
main.scss 仅做文件索引,禁止直接编写样式代码。
box-sizing: border-box。:root {
--color-primary: #1890ff;
--spacing-unit: 8px;
}
约定依赖工具落地,所有项目必须集成以下配置。
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
{
"printWidth": 100,
"tabWidth": 2,
"singleQuote": true,
"trailingComma": "all",
"bracketSpacing": true,
"semi": true
}
核心配置(使用 stylelint-config-standard 或自定义):
{
"extends": "stylelint-config-standard",
"rules": {
"selector-max-id": 0,
"selector-max-depth": 3,
"declaration-no-important": [true, { "severity": "warning" }],
"color-hex-length": "short",
"number-leading-zero": "never",
"plugin/declaration-block-order": [ ... ] // 按分组排序
}
}
{
"tagname-lowercase": true,
"attr-lowercase": true,
"attr-value-double-quotes": true,
"id-unique": true,
"alt-require": true,
"doctype-first": true,
"style-disabled": true
}
在 CI/CD 流水线中强制执行 stylelint, htmlhint 检查,不通过则禁止合并。
alt 属性是否齐全?表单有无关联 label?!important 或魔术数字?颁布即执行,修订需由前端技术委员会评审通过。
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。