命名技巧
語義化
語義化標簽優(yōu)先
基于功能命名锭沟、基于內(nèi)容命名擅威、基于表現(xiàn)命名
簡略、明了冈钦、無后患
tips:
大聲叫出它的名字
翻譯成英文
<!-- 不好 -->
<div class="article">
<div class="article_title">編碼規(guī)范</div>
<div class="the_content">今天講的內(nèi)容是編碼規(guī)范郊丛,講師
<div class="darkbold">若愚</div> @饑人谷</div>
</div>
不符合語義化改為:
<!-- 好 -->
<article>
<h1>編碼規(guī)范</h1>
<p>今天講的內(nèi)容是編碼規(guī)范,講師
<b>若愚</b> @饑人谷</p>
</article>
<!-- 不好 --> :取名容易帶來誤解
<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>
<!-- 不好 -->
<article class="blue">...</article>
<article class="redBg mt30 bigText">...</article>
<!-- 好 -->
<article class="movies">...</article>
<article class="news">...</article>
命名范例
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.命名體現(xiàn)功能瞧筛,不涉及表現(xiàn)樣式(顏色厉熟、字體、邊框较幌、背景等)
推薦:`<div class="text-lesser"></div>`
不推薦: `<div class="light-grey"></div>`
常見命名1
.wrap或.wrapper -- 用于外側(cè)包裹
.container或 .ct -- 包裹容器
.header -- 用于頭部
.body -- 頁面 body
.footer -- 頁面尾部
aside揍瑟、sidebar -- 用于側(cè)邊欄
.content -- 和header footer 對應(yīng),用于主要內(nèi)容
.navigation -- 導航元素
.pagination -- 分頁
常見命名2
.tabs > .tab -- tab 切換
.breadcrumbs -- 導航列表乍炉、面包屑
.dropdown -- 下拉菜單
.article -- 文章
.main -- 用于主體
.thumbnail -- 頭像绢片,小圖像
.media -- 媒體資源
.panel -- 面板
.tooltip -- 鼠標放置上去的提示
.popup -- 鼠標點擊彈出的提示
常見命名3
.button、.btn -- 按鈕
.ad -- 廣告
.subnav -- 二級導航
.menu -- 菜單
.tag -- 標簽
.message或者.notice -- 提示消息
.summary -- 摘要
.logo -- logo
.search -- 搜索框
.login -- 登錄
常見命名4
.register -- 注冊
.username -- 用戶名
.password -- 密碼
.banner -- 廣告條
.copyright -- 版權(quán)
.modal或者 .dialog -- 彈窗
var 名字 = {
狀態(tài): [
'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規(guī)范
書寫規(guī)范
tab 用兩個空格表示
css的 :后加個空格岛琼, {前加個空格
每條聲明后都加上分號
換行底循,而不是放到一行
顏色用小寫,用縮寫, #fff
小數(shù)不用寫前綴, 0.5s -> .5s槐瑞;0不用加單位
盡量縮寫熙涤, margin: 5px 10px 5px 10px -> margin: 5px 10px
/* 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("http://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;
}
參考
http://codeguide.bootcss.com/
https://google.github.io/styleguide/htmlcssguide.html
垂直居中
居中vs不居中
代碼:上下padding相等{padding:40px 0;}
http://js.jirengu.com/feki/1/edit?html,css,output
絕對定位實現(xiàn)居中
代碼
http://js.jirengu.com/govucapiko/1/edit?html,css,output
vertical-align實現(xiàn)居中
代碼
vertical-align:middle作用在行類元素和表格中
http://js.jirengu.com/pinasosewi/1/edit?html,css,output
table-cell實現(xiàn)居中
代碼
http://js.jirengu.com/nape/1/edit?html,css,output
偽類與偽元素
偽類
偽類用于當已有元素處于的某個狀態(tài)時,為其添加對應(yīng)的樣式困檩,這個狀態(tài)是根據(jù)用戶行為而動態(tài)變化
偽類列舉
偽類
:checked:比如CheckBox祠挫,得到被勾選的這個狀態(tài)
:disabled得到被禁用的這個狀態(tài)
:valid有效的
:invalid無效的
:first-child第一個孩子
:last-child最后一個孩子
:first-of-type某種類型的第一個孩子
link visited hover active 順序
a:link{
color: blue;
}
a:visited{
color: yellow;
}
a:hover{
color: red;
}
a:active{
color: pink;
}
http://js.jirengu.com/kuco/1/edit?html,css,output
first-child vs first-of-type
h1:first-child: 選擇是h1并且它是長子的元素
h1:first-of-type: 選擇是h1并且它是它父親里h1類型中的長子的元素
<div class="wrap">
<h1>我是大標題1</h1>
<p>我是段落2</p>
<h1>我是大標題3</h1>
</div>
<style>
.wrap h1:first-of-type{
background: yellow;
}
.wrap p:first-of-type{
background: pink;
}
.wrap h1:first-child{
color: red;
}
.wrap p:first-child{
color: blue;
}
</style>
http://js.jirengu.com/baqo/1/edit?html,css,output
偽元素
偽元素用于創(chuàng)建一些不在文檔樹中的元素,并為其添加樣式悼沿。
單雙冒號:
::before/:before
::after/:after
::first-letter/:first-letter
::first-line/:first-line
僅雙冒號:
::selection
::placeholder
::backdrop
http://js.jirengu.com/xato/1/embed?html,css,output
:before :after
<div class="box">
<p>這是第一段</p>
</div>
<style>
.box:before{
content: 'start'
}
.box:after{
content: 'end'
}
</style>
<div class="box">
::before
<p>這是第一段</p>
::after
</div>
:before :after
element:before 在element內(nèi)部創(chuàng)建一個行內(nèi)元素等舔,作為element的第一個孩子
element:after 在element內(nèi)部創(chuàng)建一個行內(nèi)元素,作為element的最后一個個孩子
用:before :after 的目的是為了省標簽
其中content 是必不可少
應(yīng)用-清除浮動
.clearfix:after {
content:"";
display:block;
clear:both;
}
http://js.jirengu.com/kiwo/1/edit?html,css,output
應(yīng)用-替代標簽
hi, 這里是饑人谷 hi, 這里是饑人谷
代碼
<div class="tooltip">
<span class="caret"></span>
hi, 這里是饑人谷
</div>
<div class="bubble">hi, 這里是饑人谷</div>
<style>
.tooltip,
.bubble{
position: relative;
padding: 10px;
border-radius: 3px;
background: #fff;
border: 1px solid #000;
display: inline-block;
}
.tooltip .caret,
.bubble:before{
width: 10px;
height: 10px;
border-left: 1px solid #000;
border-top: 1px solid #000;
background: #fff;
display: inline-block;
transform: rotateZ(45deg);
position: absolute;
top: -6px;
}
.bubble:before{
content:''
}
</style>
http://js.jirengu.com/buqo/1/edit?html,output
應(yīng)用-自定義checkbox
點擊笑臉切換: 代碼
今天的心情: <input type="checkbox">
<style>
input[type=checkbox]{
-webkit-appearance: none;
appearance: none;
background: url(http://7xpvnv.com2.z0.glb.qiniucdn.com/b6dcd011-23cc-4d95-9e51-9f10100103bd.png) 0 0 no-repeat;
display: inline-block;
width: 20px;
height: 20px;
background-size: contain;
vertical-align: middle;
outline: none;
}
input[type=checkbox]:checked{
-webkit-appearance: none;
appearance: none;
background: url(http://7xpvnv.com2.z0.glb.qiniucdn.com/538f26f0-6f3e-48d5-91e6-5b5bb730dd19.png) 0 0 no-repeat;
display: inline-block;
width: 20px;
height: 20px;
background-size: contain;
vertical-align: middle;
}
</style>
http://js.jirengu.com/bazi/1/edit?html,output
字體圖標
<link rel="stylesheet" type="text/css" href="http//at.alicdn.com/t/font_nyta5x5h650cnmi.css">
<span class="iconfont icon-jirengulogojichu2"></span> 代碼
代碼
參考文章-前端學習指南-字體圖標
https://zhuanlan.zhihu.com/p/22724856?refer=study-fe
參考
Alloy Team - 總結(jié)偽類與偽元素
http://www.alloyteam.com/2016/05/summary-of-pseudo-classes-and-pseudo-elements/
W3C - Pseudo-elements and pseudo-classes
W3C - Pseudo-elements and pseudo-classes 翻譯版
http://www.ayqy.net/doc/css2-1/selector.html#pseudo-elements