编码规范、垂直居中、伪类

命名技巧

语义化
语义化标签优先
基于功能命名、基于内容命名、基于表现命名
简略、明了、无后患
tips:

大声叫出它的名字
翻译成英文

<!-- 不好  -->
<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>
<!-- 不好  --> :取名容易带来误解
<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.命名体现功能,不涉及表现样式(颜色、字体、边框、背景等)

推荐:`<div class="text-lesser"></div>`

不推荐: `<div class="light-grey"></div>`

常见命名1

.wrap或.wrapper -- 用于外侧包裹
.container或 .ct -- 包裹容器
.header -- 用于头部
.body -- 页面 body
.footer -- 页面尾部
aside、sidebar -- 用于侧边栏
.content -- 和header footer 对应,用于主要内容
.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 -- 版权
.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规范

书写规范

tab 用两个空格表示
css的 :后加个空格, {前加个空格
每条声明后都加上分号
换行,而不是放到一行
颜色用小写,用缩写, #fff
小数不用写前缀, 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("//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

绝对定位实现居中

代码

http://js.jirengu.com/govucapiko/1/edit?html,css,output

vertical-align实现居中

代码
vertical-align:middle作用在行类元素和表格中
http://js.jirengu.com/pinasosewi/1/edit?html,css,output

table-cell实现居中

代码
http://js.jirengu.com/nape/1/edit?html,css,output

伪类与伪元素

伪类

伪类用于当已有元素处于的某个状态时,为其添加对应的样式,这个状态是根据用户行为而动态变化

伪类列举

伪类

:checked:比如CheckBox,得到被勾选的这个状态
:disabled得到被禁用的这个状态
: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

伪元素

伪元素用于创建一些不在文档树中的元素,并为其添加样式。

单双冒号:
::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内部创建一个行内元素,作为element的第一个孩子
element:after 在element内部创建一个行内元素,作为element的最后一个个孩子
用:before :after 的目的是为了省标签
其中content 是必不可少

应用-清除浮动

.clearfix:after {
    content:"";
    display:block;
    clear:both;
}

http://js.jirengu.com/kiwo/1/edit?html,css,output

应用-替代标签

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

应用-自定义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 - 总结伪类与伪元素
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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容