美文网首页前端
前端编码规范

前端编码规范

作者: 饥人谷_远方 | 来源:发表于2018-08-18 17:15 被阅读30次

一、命名技巧

1、语义化

即合适标签+合适命名

(1)语义化标签优先

(2)基于功能命名、内容命名、表现命名

(3)简略、明了、无后患

tips:可自己改名字、翻译成英文单词

2、范例

(1)

<!-- 不好  -->
<div class="article">
  <div class="article_title">编码规范</div>
  <div class="the_content">今天讲的内容是编码规范,讲师
     <div class="darkbold">若愚</div> @饥人谷</div>
</div>

<!-- 好 -->
<article>
  <h1>编码规范</h1>
  <p>今天讲的内容是编码规范,讲师
     <b>若愚</b> @饥人谷</p>
</article>

(2)

<!-- 不好  -->
<div class="left"></div>
<div class="red"></div>
<div class="s"></div>
<a class="link" href="#"></a>

<!-- 好 -->
<div class="success"></div>
<div class="theme-color"></div>
<a class="login" href="#"></a>

(3)

<!-- 好 -->
<article class="movies">...</article>
<article class="news">...</article>

<!-- 不好 -->
<article class="blue">...</article>
<article class="redBg mt30 bigText">...</article>

3、命名范例

(1).所有命名都使用英文小写
推荐:`<div class="main"></div> `
不推荐: `<div class="Main"></div> `
(2).命名用引号包裹
推荐:`<div id="header"></div> `
不推荐: `<div id=header></div> `
(3).用中横线连接
推荐:`<div class="mod-modal"></div> `
不推荐: `<div class="modModal"></div> `
(4).命名体现功能,不涉及表现样式(颜色、字体、边框、背景等
推荐:`<div class="text-lesser"></div>`
不推荐: `<div class="light-grey"></div>`

4、常见命名
第一种:

  • .wrap.wrapper-- 用于外侧包裹
  • .container.ct -- 包裹容器
  • .header -- 用于头部
  • .body -- 页面 body
  • .footer -- 页面尾部
  • asidesidebar -- 用于侧边栏
  • .content -- 和header footer对应,用于主要内容
  • .navigation -- 导航元素
  • .pagination -- 分页
    第二种:
  • .tabs >.tab -- tab 切换
  • .breadcrumbs -- 导航列表、面包屑
  • .dropdown -- 下拉菜单
  • .article -- 文章
  • .main -- 用于主体
  • .thumbnail -- 头像,小图像
  • .media -- 媒体资源
  • .panel -- 面板
  • .tooltip -- 鼠标放置上去的提示
  • .popup -- 鼠标点击弹出的提示
    第三种:
  • .button.btn -- 按钮
  • .ad -- 广告
  • .subnav-- 二级导航
  • .menu -- 菜单
  • .tag -- 标签
  • .message或者.notice -- 提示消息
  • .summary -- 摘要
  • .logo -- logo
  • .search -- 搜索框
  • .login -- 登录
    第四种:
  • .register -- 注册
  • .username -- 用户名
  • .password -- 密码
  • .banner -- 广告条
  • `.copyright -- 版权
  • .modal或者.dialog-- 弹窗
    第五种:
var 名字 = {
  状态: [
    'inverse',
    'toggled',
    'switched',
    'original',
    'initial',
    'identified',
    'disabled',
    'loading',
    'pending',
    'syncing',
    'default'
  ],
  修饰: [
    'dark',
    'light',
    'shaded',
    'flat',
    'ghost',
    'maroon',
    'pale',
    'intense',
    'twisted',
    'narrow',
    'wide',
    'smooth',
    'separate',
    'clean',
    'sharp',
    'aligned'
  ],
  元素: [
    'pagination',
    'modal',
    'popup',
    'article',
    'story',
    'flash',
    'status',
    'state',
    'media',
    'block',
    'card',
    'teaser',
    'badge',
    'label',
    'sheet',
    'poster',
    'notice',
    'record',
    'entry',
    'item',
    'figure',
    'square',
    'module',
    'bar',
    'button',
    'action',
    'knob'
  ],
  布局: [
    'navigation',
    'wrapper',
    'inner',
    'header',
    'footer',
    'aside',
    'section',
    'divider',
    'content',
    'container',
    'panel',
    'pane',
    'construct',
    'composition',
    'spacing',
    'frame'
  ]
}

二、CSS编码规范

1、书写规范

  • tab 用两个空格表示
  • css的 :后加个空格,{前加个空格
  • 每条声明后都加上分号
  • 换行,而不是放到一行
  • 颜色用小写,用缩写, #fff
  • 小数不用写前缀, 0.5s -> .5s;0不用加单位
  • 尽量缩写,margin: 5px 10px 5px 10px ->margin: 5px 10px

2、范例——google编码规范

/* Not recommended */
.test {
  display: block;
  height: 100px
}
/* Recommended */
.test {
  display: block;
  height: 100px;
}


/* Not recommended */
h3 {
  font-weight:bold;
}
/* Recommended */
h3 {
  font-weight: bold;
}


/* Not recommended: missing space */
#video{
  margin-top: 1em;
}

/* Not recommended: unnecessary line break */
#video
{
  margin-top: 1em;
}
/* Recommended */
#video {
  margin-top: 1em;
}


/* Not recommended */
a:focus, a:active {
  position: relative; top: 1px;
}
/* Recommended */
h1,
h2,
h3 {
  font-weight: normal;
  line-height: 1.2;
}


/* Always put a blank line (two line breaks) between rules. */
html {
  background: #fff;
}

body {
  margin: auto;
  width: 50%;
}


/* Not recommended */
@import url("//www.google.com/css/maia.css");

html {
  font-family: "open sans", arial, sans-serif;
}
/* Recommended */
@import url(//www.google.com/css/maia.css);

html {
  font-family: 'open sans', arial, sans-serif;
}

三、参考

  • google html css编码规范

google html css编码规范

  • bootstrap编码规范

bootstrap编码规范

其中,对新手前端写代码较重要的因素:声明顺序。相关的属性声明应当归为一组,并按照下面的顺序排列:

Positioning(定位)

Box model(盒模型)

Typographic(与文字相关,字体大小宽高居中)

Visual(颜色相关)

由于定位(positioning)可以从正常的文档流中移除元素,并且还能覆盖盒模型(box model)相关的样式,因此排在首位。盒模型排在第二位,因为它决定了组件的尺寸和位置。

其他属性只是影响组件的内部(inside)或者是不影响前两组属性,因此排在后面。

  • 命名这货真难

命名这货真难

总结:链接里的文章强烈建议通看一遍

相关文章

  • 前端开发规范(实验室版)

    前端编码规范—— HTML 篇 前端编码规范—— CSS 篇 前端编码规范—— JavaScript 篇 这几天和...

  • 雷铭大前端组件库

    雷铭大前端组件库 包含《雷铭前端开发规范》、《雷铭Android编码规范》、《雷铭iOS编码规范》以及不同技术分类...

  • 前端编码规范

    前端编码规范参考链接 go!!

  • Eslint搭建及使用

    内容 本文将讲解如何在 VSCode 中配合 ESLint 扩展来实践团队内部的前端编码规范。 前端编码规范 ES...

  • 无标题文章

    # CSS 编码规范 此为前端开发团队遵循和约定的 CSS 编码规范,意在提高代码的规范性和可维护性。 ## 代码...

  • 前端编码规范

    一、命名技巧 1、语义化 即合适标签+合适命名 (1)语义化标签优先 (2)基于功能命名、内容命名、表现命名 (3...

  • 前端编码规范

    英文单词保证拼写正确。 尽量不使用中文拼音; 重要常量、变量、方法等保证详细注释,关键业务逻辑进行详细记录; 变量...

  • 前端编码规范

    CSS编码规范 1 前言 2 代码风格 2.1 文件 2.2 缩进 2.3 空格 2.4 行长度 2.5 选择器 ...

  • 前端编码规范

    很久没有更新文章了。。。实在是比较忙啊。。。从本身一个iOS开发,现在要兼顾前端、产品、维护等,每天都有不同的事,...

  • 前端编码规范

    基本原则 结构、样式、行为分离 统一缩进(建议 两个空格) 文件编码统一 不带BOM的UTF-8 一律使用小写字母...

网友评论

    本文标题:前端编码规范

    本文链接:https://www.haomeiwen.com/subject/cdbfiftx.html