ホームページ >ウェブフロントエンド >htmlチュートリアル >htmlとcssのコーディング仕様
リーリー
リーリー
例:
99598100062d9a4d9a907784b46d2acc
72b7f80341f58a3fd41ecaec48315776
書き方の基準としては、コメントを減らすために必要なすべてのコメントに始まりと終わりを設けるのが最善です。その後のトラブルやミス。線を折り返さないでください。 CSS や JS のコメントも同様です。 7a8588c23088ed06bd93c0515736d91616dc0b2f613c9236f6576f45a8b61404
121cdc96f166a5fbdf255a86e1519b50
は、必要なすべてのコメントに始まりと終わりがあることが最善です。その後のトラブルやミスを軽減します。線を折り返さないでください。 CSS や JS のコメントも同様です。 7547317c45e4590ca27da0d99421ac3a
例: class="tb-head";4. 属性の並べ替え
リーリー
リーリー
例:
リーリー
リーリー6.ブール属性
リーリー
2.CSS仕様
[建议] 选择器的嵌套层级应不大于 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