Heim >Web-Frontend >HTML-Tutorial >Codierungsspezifikationen für HTML und CSS
<code class="hljs xml"><span class="hljs-doctype">8b05045a5be5764f313ed5b9168a17e6 <span class="hljs-tag">fd1c7470616d86278840d6b7f184939d <span class="hljs-tag">544891a773924c17cba16fcfae7625a3 <span class="hljs-tag">d373320a9b31d54a158ab0c769e5981d <span class="hljs-tag">e8039b8767e5ac2709e79e7579269c11 <span class="hljs-tag">5cdbdac6b254fb497e2050d51cee3b1e .... <span class="hljs-tag">3df78ee2da90f6777fae316d12dd978c </span></span></span></span></span></span></span></code>
<code class="hljs xml"><span class="hljs-comment"><!-- Use link elements --> <span class="hljs-tag"><<span class="hljs-title">link <span class="hljs-attribute">rel=<span class="hljs-value">"stylesheet" <span class="hljs-attribute">href=<span class="hljs-value">"core.css"> <span class="hljs-comment"><!-- Avoid @imports --> <span class="hljs-tag"><<span class="hljs-title">style><span class="css"> <span class="hljs-at_rule">@<span class="hljs-keyword">import <span class="hljs-function">url(<span class="hljs-string">"more.css"); <span class="hljs-tag"></<span class="hljs-title">style> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
<code class="hljs xml"><span class="hljs-comment">5c6c51bc41886c301ce58ec77317e826 <span class="hljs-tag">d06eb969412e9d4a2552018f00a5f964 <span class="hljs-comment">48df608d85346f715d811edbe2b24159 <span class="hljs-tag">dabd89fc18491d0568a782031b6411c7<span class="css"> <span class="hljs-comment">/* ... */ <span class="hljs-tag">d046036d3cc984786d6dec3577099da1 <span class="hljs-comment">f43272291475af4b6bd8a33a495f4618 <span class="hljs-tag">ab71ecaf05cdb791f516c43a54a9638a<span class="undefined"><span class="hljs-tag">ec5b9b155d57a51cb2f2cc4c7a39dc30 </span></span></span></span></span></span></span></span></span></span></span></code>
zB:
063cdccfbe2daa58849aece81acd0f27
13aefc90698bf74a1bf5dce7790c6a5d
ist eine Schreibvorgabe, es ist am besten, jeden Kommentar einzuschließen Das muss am Anfang und Ende gemacht werden, um spätere Probleme und Fehler zu reduzieren. Wickeln Sie die Leine nicht um. Das Gleiche gilt für CSS- und JS-Kommentare. b72ab2265678c9b794e8f5715841cf631747ec4207cc61892609c7cd8d763393
be76a34f08e132e86d3bcaf96c56926a
ist eine Schreibspezifikation, vorzugsweise alle benötigten Kommentare Sie müssen einen Anfang und ein Ende haben, um spätere Probleme und Fehler zu vermeiden. Wickeln Sie die Leine nicht um. Das Gleiche gilt für CSS- und JS-Kommentare. 3f813babe36e3927dff1365350928610
zB: class="tb-head"; id="tb_head";4. Attributsortierung
<code class="hljs scala"> <a <span class="hljs-class"><span class="hljs-keyword">class=<span class="hljs-string">"..." id=<span class="hljs-string">"..." data-modal=<span class="hljs-string">"toggle" href=<span class="hljs-string">"#"> <span class="hljs-type">Example link </a> <input <span class="hljs-class"><span class="hljs-keyword">class=<span class="hljs-string">"form-control" <span class="hljs-class"><span class="hljs-keyword">type=<span class="hljs-string">"text"> <img src=<span class="hljs-string">"..." alt=<span class="hljs-string">"..."> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
<code class="hljs scala"><a href=<span class="hljs-string">"#" <span class="hljs-class"><span class="hljs-keyword">class=<span class="hljs-string">"..." id=<span class="hljs-string">"..."</a> <span class="hljs-type">Example link </a> <input <span class="hljs-class"><span class="hljs-keyword">type=<span class="hljs-string">"text" <span class="hljs-class"><span class="hljs-keyword">class=<span class="hljs-string">"..."> </span></span></span></span></span></span></span></span></span></span></span></span></code>
zB:
<code class="hljs cs">35a5735c4d13aab1f19a6a6608f5be135db79b134e9f6b82c0b36e0489ee08ed </code>
6. Boolesche Attribute<code class="hljs xml"><span class="hljs-comment">4c76ac2c1023151a8e2d3158076fa24e <span class="hljs-tag">ad790d93725fb3106baa46853aff6ac0 <span class="hljs-tag">b932ae0017108b7fe3e1fd31c5a0da59 <span class="hljs-tag">56f9acf840e115a35ebba60d6f9fe6ca <span class="hljs-comment">6b209e841d0ff16fd57e804718252b1f <span class="hljs-tag">ccefbc2b1384a73e539b822ef19a6b0f </span></span></span></span></span></span></code>
<code class="hljs xml"><span class="hljs-tag">1552813772daa9ad6d0d1ceb6d7a1cdd <span class="hljs-tag">42afc11feee76d1f773281d7b3788f91 <span class="hljs-tag">fb0b45e61002348bececde7921721666 <span class="hljs-tag">c86c43aa558cb7d0f6be6102cdc1e49e1<span class="hljs-tag">16148725ba3c64870356b0d518612457 <span class="hljs-tag">543154cbec1f6ea40fdaf0a76c8cde59 </span></span></span></span></span></span></code>
2. CSS-Spezifikationen
<code class="hljs cpp">[强制]使用 <span class="hljs-number">4 个空格做为一个缩进层级,不允许使用 <span class="hljs-number">2 个空格 或 tab 字符。 </span></span></code>
[建议] 选择器的嵌套层级应不大于 3 级,位置靠后的限定条件应尽可能精确。
eg:
<code class="hljs css"><span class="hljs-comment">/* good */ <span class="hljs-id">#error, <span class="hljs-class">.danger-message <span class="hljs-rules">{ <span class="hljs-rule"><span class="hljs-attribute">font-color:<span class="hljs-value"> <span class="hljs-hexcolor">#c00; } <span class="hljs-id">#username <span class="hljs-tag">input <span class="hljs-rules">{} <span class="hljs-class">.comment <span class="hljs-class">.avatar <span class="hljs-rules">{} <span class="hljs-comment">/* bad */ <span class="hljs-tag">dialog<span class="hljs-id">#error, <span class="hljs-tag">p<span class="hljs-class">.danger-message <span class="hljs-rules">{ <span class="hljs-rule"><span class="hljs-attribute">font-color:<span class="hljs-value"> <span class="hljs-hexcolor">#c00; } <span class="hljs-class">.page <span class="hljs-class">.header <span class="hljs-class">.login <span class="hljs-id">#username <span class="hljs-tag">input <span class="hljs-rules">{} <span class="hljs-class">.comment <span class="hljs-tag">div * <span class="hljs-rules">{} </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
<code class="hljs css"><span class="hljs-comment">/* Bad CSS */ <span class="hljs-tag">main><span class="hljs-tag">nav <span class="hljs-rules">{ <span class="hljs-rule"><span class="hljs-attribute">padding:<span class="hljs-value"> <span class="hljs-number">10px; } <span class="hljs-class">.selector, <span class="hljs-class">.selector-secondary, <span class="hljs-class">.selector<span class="hljs-attr_selector">[type=text] <span class="hljs-rules">{ <span class="hljs-rule"><span class="hljs-attribute">padding:<span class="hljs-value"><span class="hljs-number">15px; <span class="hljs-rule"><span class="hljs-attribute">margin:<span class="hljs-value"><span class="hljs-number">0px <span class="hljs-number">0px <span class="hljs-number">15px; <span class="hljs-rule"><span class="hljs-attribute">background-color:<span class="hljs-value"><span class="hljs-function">rgba(<span class="hljs-number">0, <span class="hljs-number">0, <span class="hljs-number">0, <span class="hljs-number">0.5); <span class="hljs-rule"><span class="hljs-attribute">box-shadow:<span class="hljs-value"><span class="hljs-number">0px <span class="hljs-number">1px <span class="hljs-number">2px <span class="hljs-hexcolor">#CCC,inset <span class="hljs-number">0 <span class="hljs-number">1px <span class="hljs-number">0 <span class="hljs-hexcolor">#FFFFFF } <span class="hljs-class">.selector, <span class="hljs-class">.selector-secondary, <span class="hljs-class">.selector<span class="hljs-attr_selector">[type=text] <span class="hljs-rules">{<span class="hljs-rule"><span class="hljs-attribute">padding:<span class="hljs-value"><span class="hljs-number">15px;<span class="hljs-rule"><span class="hljs-attribute">margin:<span class="hljs-value"><span class="hljs-number">0px <span class="hljs-number">0px <span class="hljs-number">15px;} <span class="hljs-comment">/* Good CSS */ <span class="hljs-tag">main > <span class="hljs-tag">nav <span class="hljs-rules">{ <span class="hljs-rule"><span class="hljs-attribute">padding:<span class="hljs-value"> <span class="hljs-number">10px; } <span class="hljs-class">.selector, <span class="hljs-class">.selector-secondary, <span class="hljs-class">.selector<span class="hljs-attr_selector">[type="text"] <span class="hljs-rules">{ <span class="hljs-rule"><span class="hljs-attribute">padding:<span class="hljs-value"> <span class="hljs-number">15px; <span class="hljs-rule"><span class="hljs-attribute">margin-bottom:<span class="hljs-value"> <span class="hljs-number">15px; <span class="hljs-rule"><span class="hljs-attribute">background-color:<span class="hljs-value"> <span class="hljs-function">rgba(<span class="hljs-number">0,<span class="hljs-number">0,<span class="hljs-number">0,.<span class="hljs-number">5); <span class="hljs-rule"><span class="hljs-attribute">background:<span class="hljs-value"> transparent <span class="hljs-function">url(aVeryVeryVeryLongUrlIsPlacedHere) no-repeat <span class="hljs-number">0 <span class="hljs-number">0; <span class="hljs-rule"><span class="hljs-attribute">box-shadow:<span class="hljs-value"> <span class="hljs-number">0 <span class="hljs-number">1px <span class="hljs-number">2px <span class="hljs-hexcolor">#ccc, inset <span class="hljs-number">0 <span class="hljs-number">1px <span class="hljs-number">0 <span class="hljs-hexcolor">#fff; } </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
eg: /* S 注释内容 */
为书写规范,最好每一个需要注释都需要开头和结尾,减少后续麻烦和错误。不要翻行。CSS、JS注释亦然。
/* E 注释内容 */
在为 Sass 和 Less 变量命名是也可以参考上面列出的各项规范。
<code class="hljs actionscript"><span class="hljs-comment">/* Bad example */ .t { ... } .red { ... } .header { ... } <span class="hljs-comment">/* Good example */ .tweet { ... } .important { ... } .tweet-header { ... } </span></span></code>
<code class="hljs cpp"><span class="hljs-comment">/* Bad example */ span { ... } .page-container <span class="hljs-preprocessor">#stream .stream-item .tweet .tweet-header .username { ... } .avatar { ... } <span class="hljs-comment">/* Good example */ .avatar { ... } .tweet-header .username { ... } <span class="hljs-comment">//不要超过3个 .tweet .avatar { ... } </span></span></span></span></code>
<code class="hljs markdown"><span class="hljs-bullet">* Positioning <span class="hljs-bullet">* Box model <span class="hljs-bullet">* Typographic <span class="hljs-bullet">* Visual </span></span></span></span></code>
由于定位(positioning)可以从正常的文档流中移除元素,并且还能覆盖盒模型(box model)相关的样式,因此排在首位。盒模型排在第二位,因为它决定了组件的尺寸和位置。
其他属性只是影响组件的内部(inside)或者是不影响前两组属性,因此排在后面。 eg:
<code class="hljs css"><span class="hljs-class">.declaration-order <span class="hljs-rules">{ <span class="hljs-comment">/* Positioning */ <span class="hljs-rule"><span class="hljs-attribute">position:<span class="hljs-value"> absolute; <span class="hljs-rule"><span class="hljs-attribute">top:<span class="hljs-value"> <span class="hljs-number">0; <span class="hljs-rule"><span class="hljs-attribute">right:<span class="hljs-value"> <span class="hljs-number">0; <span class="hljs-rule"><span class="hljs-attribute">bottom:<span class="hljs-value"> <span class="hljs-number">0; <span class="hljs-rule"><span class="hljs-attribute">left:<span class="hljs-value"> <span class="hljs-number">0; <span class="hljs-rule"><span class="hljs-attribute">z-index:<span class="hljs-value"> <span class="hljs-number">100; <span class="hljs-comment">/* Box-model */ <span class="hljs-rule"><span class="hljs-attribute">display:<span class="hljs-value"> block; <span class="hljs-rule"><span class="hljs-attribute">float:<span class="hljs-value"> right; <span class="hljs-rule"><span class="hljs-attribute">width:<span class="hljs-value"> <span class="hljs-number">100px; <span class="hljs-rule"><span class="hljs-attribute">height:<span class="hljs-value"> <span class="hljs-number">100px; <span class="hljs-comment">/* Typography */ <span class="hljs-rule"><span class="hljs-attribute">font:<span class="hljs-value"> normal <span class="hljs-number">13px <span class="hljs-string">"Helvetica Neue", sans-serif; <span class="hljs-rule"><span class="hljs-attribute">line-height:<span class="hljs-value"> <span class="hljs-number">1.5; <span class="hljs-rule"><span class="hljs-attribute">color:<span class="hljs-value"> <span class="hljs-hexcolor">#333; <span class="hljs-rule"><span class="hljs-attribute">text-align:<span class="hljs-value"> center; <span class="hljs-comment">/* Visual */ <span class="hljs-rule"><span class="hljs-attribute">background-color:<span class="hljs-value"> <span class="hljs-hexcolor">#f5f5f5; <span class="hljs-rule"><span class="hljs-attribute">border:<span class="hljs-value"> <span class="hljs-number">1px solid <span class="hljs-hexcolor">#e5e5e5; <span class="hljs-rule"><span class="hljs-attribute">border-radius:<span class="hljs-value"> <span class="hljs-number">3px; <span class="hljs-comment">/* Misc */ <span class="hljs-rule"><span class="hljs-attribute">opacity:<span class="hljs-value"> <span class="hljs-number">1; } </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
注意兼容性写法(纯属个人习惯)
每个范围可由首字母开头为排序:
eg:盒子模型中的第2条中,可由background,border,color等首字母为先排序; 但有些例外,如position肯定排先,后定义left等方向位属性
ps:需要用到媒体查询,将媒体查询放在尽可能相关规则的附近
eg:
<code class="hljs cpp">.declaration-order { display: block; <span class="hljs-keyword">float: right; background-color: <span class="hljs-preprocessor">#f5f5f5; border: <span class="hljs-number">1px solid <span class="hljs-preprocessor">#e5e5e5; border-radius: <span class="hljs-number">3px; color: <span class="hljs-preprocessor">#<span class="hljs-number">333; font: normal <span class="hljs-number">13px <span class="hljs-string">"Helvetica Neue", sans-serif; width: <span class="hljs-number">100px; height: <span class="hljs-number">100px; text-align: center; line-height: <span class="hljs-number">100px; margin: <span class="hljs-number">0 <span class="hljs-keyword">auto; padding: <span class="hljs-number">10px <span class="hljs-number">0; position: absolute; top: <span class="hljs-number">0; right: <span class="hljs-number">0; bottom: <span class="hljs-number">0; left: <span class="hljs-number">0; z-index: <span class="hljs-number">100; opacity: <span class="hljs-number">1; transtion: all <span class="hljs-number">.5s ease <span class="hljs-number">0s; animation: name initial <span class="hljs-number">2s; } @keyframes name{ <span class="hljs-number">0%{} <span class="hljs-number">100{} <span class="hljs-comment">/// from{} to{} } @<span class="hljs-function">media screen <span class="hljs-title">and <span class="hljs-params">(min-width) <span class="hljs-title">and <span class="hljs-params">(max-width){ and左右之间要留空格 } </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
当使用特定厂商的带有前缀的属性时,通过缩进的方式,让每个属性的值在垂直方向对齐,这样便于多行编辑。
在 Textmate 中,使用 Text → Edit Each Line in Selection (⌃⌘A)。在 Sublime Text 2 中,使用 Selection → Add Previous Line (⌃⇧↑) 和 Selection → Add Next Line (⌃⇧↓)。
Prefixed properties */ lector { webkit-box-shadow: 0 1px 2px rgba(0,0,0,.15); box-shadow: 0 1px 2px rgba(0,0,0,.15);
<code class="hljs css"><span class="hljs-comment">/* Bad example */ <span class="hljs-class">.element <span class="hljs-rules">{ <span class="hljs-rule"><span class="hljs-attribute">margin:<span class="hljs-value"> <span class="hljs-number">0 <span class="hljs-number">0 <span class="hljs-number">10px; <span class="hljs-rule"><span class="hljs-attribute">background:<span class="hljs-value"> red; <span class="hljs-rule"><span class="hljs-attribute">background:<span class="hljs-value"> <span class="hljs-function">url(<span class="hljs-string">"image.jpg"); <span class="hljs-rule"><span class="hljs-attribute">border-radius:<span class="hljs-value"> <span class="hljs-number">3px <span class="hljs-number">3px <span class="hljs-number">0 <span class="hljs-number">0; } <span class="hljs-comment">/* Good example */ <span class="hljs-class">.element <span class="hljs-rules">{ <span class="hljs-rule"><span class="hljs-attribute">margin-bottom:<span class="hljs-value"> <span class="hljs-number">10px; <span class="hljs-rule"><span class="hljs-attribute">background-color:<span class="hljs-value"> red; <span class="hljs-rule"><span class="hljs-attribute">background-image:<span class="hljs-value"> <span class="hljs-function">url(<span class="hljs-string">"image.jpg"); <span class="hljs-rule"><span class="hljs-attribute">border-top-left-radius:<span class="hljs-value"> <span class="hljs-number">3px; <span class="hljs-rule"><span class="hljs-attribute">border-top-right-radius:<span class="hljs-value"> <span class="hljs-number">3px; } </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
<code class="hljs lua">// Without nesting .<span class="hljs-built_in">table > thead > tr > th { … } .<span class="hljs-built_in">table > thead > tr > td { … } // With nesting .<span class="hljs-built_in">table > thead > tr { > th { … } > td { … } } </span></span></span></code>
了解更多请点击,本文更多参照此文章
<code class="hljs swift">头:header 内容:content/container 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整体佈局宽度:wrapper 左右中:<span class="hljs-keyword">left <span class="hljs-keyword">right center 登录条:loginbar 标志:logo 广告:banner 页面主体:main 热点:hot 新闻:news 下载:download 子导航:subnav 菜单:menu 子菜单:submenu 搜索:search 友情链接:friendlink 页脚:footer 版权:copyright 滚动:scroll 内容:content 标签:tags 文章列表:list 提示信息:msg 小技巧:tips 栏目标题:title 加入:joinus 指南:guide 服务:service 注册:regsiter 状态:status 投票:vote 合作伙伴:partner </span></span></code>
1)页面结构
容器: container 页头:header 内容:content/container 页面主体:main 页尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整体佈局宽度:wrapper 左右中:left right center
(2)导航
导航:nav 主导航:mainnav 子导航:subnav 顶导航:topnav 边导航:sidebar 左导航:leftsidebar 右导航:rightsidebar 菜单:menu 子菜单:submenu 标题: title 摘要: summary
(3)功能
标志:logo 广告:banner 登陆:login 登录条:loginbar 注册:register 搜索:search 功能区:shop 标题:title 加入:joinus 状态:status 按钮:btn 滚动:scroll 标籤页:tab 文章列表:list 提示信息:msg 当前的: current 小技巧:tips 图标: icon 注释:note 指南:guild 服务:service 热点:hot 新闻:news 下载:download 投票:vote 合作伙伴:partner 友情链接:link 版权:copyright
<code class="hljs css">主要的 <span class="hljs-tag">master<span class="hljs-class">.css 模块 <span class="hljs-tag">module<span class="hljs-class">.css 基本共用 <span class="hljs-tag">base<span class="hljs-class">.css 布局、版面 <span class="hljs-tag">layout<span class="hljs-class">.css 主题 <span class="hljs-tag">themes<span class="hljs-class">.css 专栏 <span class="hljs-tag">columns<span class="hljs-class">.css 文字 <span class="hljs-tag">font<span class="hljs-class">.css 表单 <span class="hljs-tag">forms<span class="hljs-class">.css 补丁 <span class="hljs-tag">mend<span class="hljs-class">.css 打印 <span class="hljs-tag">print<span class="hljs-class">.css </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
项目命名 全部采用小写方式,以下划线分隔 例:my_project_name 目录命名
参照项目命名规则;
有复数结构时,要采用复数命名法。
例:scripts, styles, images, data_models JS文件命名
参照项目命名规则。
例:account_model.js CSS, SCSS文件命名
参照项目命名规则。
例:retina_sprites.scss HTML文件命名
参照项目命名规则。
例:error_report.html