前端开发高效率的工具插件:Emmet语法说明 使用示例 (结合Sublime Text)
分类:软件开发| 发布:佚名| 查看:473 | 发表时间:2015/3/25
HTML代码写起来很费事,因为它的标签多。
一种解决方法是采用模板, 在别人写好的骨架内,填入自己的内容。还有一种就是我今天想要介绍的方法----简写法。
Emmet (前身为 Zen Coding) 是一个能大幅度提高前端开发效率的一个工具:

基本上,大多数的文本编辑器都会允许你存储和重用一些代码块,我们称之为“片段”。虽然片段能很好地推动你得生产力,但大多数的实现都有这样一个缺点:你必须先定义你得代码片段,并且不能再运行时进行拓展。
Emmet把片段这个概念提高到了一个新的层次:你可以设置CSS形式的能够动态被解析的表达式,然后根据你所输入的缩写来得到相应的内容。Emmet是很成熟的并且非常适用于编写HTML/XML 和 CSS 代码的前端开发人员,但也可以用于编程语言。
先安装到一个编辑器上
Emmet是一个编辑器插件,官方网站提供多编辑器支持。我一般使用Sublime,下面就以Sublime插件举例。
假设你已经按照了Sublime Text 3,如图查看是否有Emmet? 菜单:Preferences>Package Settings>Emmet

如果没有这个Emmet,没关系,不要着急,先下载一个:Emmet(Emmet为大部分流行的编辑器都提供了安装插件 这里选择Sublime Text),把下载好的解压缩后,复制emmet-sublime-master文件夹,找到Sublime Text的安装目录,比如:D:\Camnpr\FEEditor\Sublime Text 3\Data\Packages 粘贴到此处,就可以了。再去看看上边的图,有Emmet了吗?
如何使用Emmet的功能?
先在Sublime里新建一个html页面,里边是空空的,接下来输入:html:5
按下tab键或者按ctrl+e,是不是发生了神奇的事情?
结果如下:
04 | < meta charset = "UTF-8" > |
那么Emmet的快捷键都有哪些?
展开缩写–Tab或Ctrl + E
互动“展开缩写“Ctrl + Alt + Enter
匹配标签对外向–?D(MAC)/ Ctrl +,(PC)
匹配标签对内向–?J / Shift + Ctrl + 0
去匹配对–??T /按Ctrl + Alt + J
用缩写-?W / Shift + Ctrl + G包
去编辑点按Ctrl + Alt + Ctrl + Alt +←→或
–??选择项目。或??,/ Shift + Ctrl +。或Shift + Ctrl +,
切换评论??/ Shift + Ctrl + /
分/合标签??/ Ctrl +转移+ `
移除标签–?/ Shift + Ctrl +;
更新图像尺寸??我/ Ctrl + U
评估的数学表达式??Y / Shift + Ctrl + Y
反映CSS值–??R / Shift + Ctrl + R
编码/解码图像数据:URL–??D / Ctrl + '
重命名标签–??K / Shift + Ctrl + '
Emmet支持的简写规则,类似于CSS选择器(大写的E代表一个HTML标签)
1. E 代表HTML标签。
2. E#id 代表id属性。
3. E.class 代表class属性。
4. E[attr=foo] 代表某一个特定属性。
5. E{foo} 代表标签包含的内容是foo。
6. E>N 代表N是E的子元素。
7. E+N 代表N是E的同级元素。
8. E^N 代表N是E的上级元素。
1. !!! 5 代表 <!DOCTYPE html>
2. %E 代表HTML标签。
3. %E#id 代表id属性。
4. %E.class 代表class属性。
5. %E(attr="xxx") 代表某一个特定属性。
6. %E XXX 代表插入标签的内容。
7. %E %N 代表N是E的子元素。N如果写在第二行,需要缩进。
那么上边的这些规则怎么用呢?看下边的示例:
Emmet的语法及使用示例:
后代:>
缩写:nav>ul>li
兄弟:+
缩写:div+p+bq
3 | < blockquote ></ blockquote > |
上级:^
缩写:div+div>p>span+em^bq
3 | < p >< span ></ span >< em ></ em ></ p > |
4 | < blockquote ></ blockquote > |
缩写:div+div>p>span+em^^bq
3 | < p >< span ></ span >< em ></ em ></ p > |
5 | < blockquote ></ blockquote > |
缩写:div>(header>ul>li*2>a)+footer>p
04 | < li >< a href = "" ></ a ></ li > |
05 | < li >< a href = "" ></ a ></ li > |
缩写:(div>dl>(dt+dd)*3)+footer>p
缩写:ul>li*5
缩写:ul>li.item$*5
2 | < li class = "item1" ></ li > |
3 | < li class = "item2" ></ li > |
4 | < li class = "item3" ></ li > |
5 | < li class = "item4" ></ li > |
6 | < li class = "item5" ></ li > |
缩写:h$[title=item$]{Header $}*3
1 | < h1 title = "item1" >Header 1</ h1 > |
2 | < h2 title = "item2" >Header 2</ h2 > |
3 | < h3 title = "item3" >Header 3</ h3 > |
缩写:ul>li.item$$$*5
2 | < li class = "item001" ></ li > |
3 | < li class = "item002" ></ li > |
4 | < li class = "item003" ></ li > |
5 | < li class = "item004" ></ li > |
6 | < li class = "item005" ></ li > |
缩写:ul>li.item$@-*5
2 | < li class = "item5" ></ li > |
3 | < li class = "item4" ></ li > |
4 | < li class = "item3" ></ li > |
5 | < li class = "item2" ></ li > |
6 | < li class = "item1" ></ li > |
缩写:ul>li.item$@3*5
2 | < li class = "item3" ></ li > |
3 | < li class = "item4" ></ li > |
4 | < li class = "item5" ></ li > |
5 | < li class = "item6" ></ li > |
6 | < li class = "item7" ></ li > |
缩写:#header
缩写:.title
1 | < div class = "title" ></ div > |
缩写:form#search.wide
1 | < form id = "search" class = "wide" ></ form > |
缩写:p.class1.class2.class3
1 | < p class = "class1 class2 class3" ></ p > |
缩写:p[title="Hello world"]
1 | < p title = "Hello world" ></ p > |
缩写:td[rowspan=2 colspan=3 title]
1 | < td rowspan = "2" colspan = "3" title = "" ></ td > |
缩写:[a='value1' b="value2"]
1 | < div a = "value1" b = "value2" ></ div > |
缩写:a{Click me}
缩写:p>{Click }+a{here}+{ to continue}
1 | < p >Click < a href = "" >here</ a > to continue</ p > |
缩写:.class
1 | < div class = "class" ></ div > |
缩写:em>.class
1 | < em >< span class = "class" ></ span ></ em > |
缩写:ul>.class
2 | < li class = "class" ></ li > |
缩写:table>.row>.col
所有未知的缩写都会转换成标签,例如,foo → <foo></foo>
缩写:!
5 | < title >Document</ title > |
缩写:a
缩写:a:link
缩写:a:mail
缩写:abbr
缩写:acronym
1 | < acronym title = "" ></ acronym > |
缩写:base
缩写:basefont
缩写:br
缩写:frame
缩写:hr
缩写:bdo
缩写:bdo:r
缩写:bdo:l
缩写:col
缩写:link
1 | < link rel = "stylesheet" href = "" /> |
缩写:link:css
1 | < link rel = "stylesheet" href = "style.css" /> |
缩写:link:print
1 | < link rel = "stylesheet" href = "print.css" media = "print" /> |
缩写:link:favicon
1 | < link rel = "shortcut icon" type = "image/x-icon" href = "favicon.ico" /> |
缩写:link:touch
1 | < link rel = "apple-touch-icon" href = "favicon.png" /> |
缩写:link:rss
1 | < link rel = "alternate" type = "application/rss+xml" title = "RSS" href = "rss.xml" /> |
缩写:link:atom
1 | < link rel = "alternate" type = "application/atom+xml" title = "Atom" href = "atom.xml" /> |
缩写:meta
缩写:meta:utf
1 | < meta http-equiv = "Content-Type" content = "text/html;charset=UTF-8" /> |
缩写:meta:win
1 | < meta http-equiv = "Content-Type" content = "text/html;charset=windows-1251" /> |
缩写:meta:vp
1 | < meta name = "viewport" content = "width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" /> |
缩写:meta:compat
1 | < meta http-equiv = "X-UA-Compatible" content = "IE=7" /> |
缩写:style
缩写:script
缩写:script:src
1 | < script src = "" ></ script > |
缩写:img
缩写:iframe
1 | < iframe src = "" frameborder = "0" ></ iframe > |
缩写:embed
1 | < embed src = "" type = "" /> |
缩写:object
1 | < object data = "" type = "" ></ object > |
缩写:param
1 | < param name = "" value = "" /> |
缩写:map
缩写:area
1 | < area shape = "" coords = "" href = "" alt = "" /> |
缩写:area:d
1 | < area shape = "default" href = "" alt = "" /> |
缩写:area:c
1 | < area shape = "circle" coords = "" href = "" alt = "" /> |
缩写:area:r
1 | < area shape = "rect" coords = "" href = "" alt = "" /> |
缩写:area:p
1 | < area shape = "poly" coords = "" href = "" alt = "" /> |
缩写:form
缩写:form:get
1 | < form action = "" method = "get" ></ form > |
缩写:form:post
1 | < form action = "" method = "post" ></ form > |
缩写:label
缩写:input
缩写:inp
1 | < input type = "text" name = "" id = "" /> |
缩写:input:hidden
别名:input[type=hidden name]
1 | < input type = "hidden" name = "" /> |
缩写:input:h
别名:input:hidden
1 | < input type = "hidden" name = "" /> |
缩写:input:text, input:t
别名:inp
1 | < input type = "text" name = "" id = "" /> |
缩写:input:search
别名:inp[type=search]
1 | < input type = "search" name = "" id = "" /> |
缩写:input:email
别名:inp[type=email]
1 | < input type = "email" name = "" id = "" /> |
缩写:input:url
别名:inp[type=url]
1 | < input type = "url" name = "" id = "" /> |
缩写:input:password
别名:inp[type=password]
1 | < input type = "password" name = "" id = "" /> |
缩写:input:p
别名:input:password
1 | < input type = "password" name = "" id = "" /> |
缩写:input:datetime
别名:inp[type=datetime]
1 | < input type = "datetime" name = "" id = "" /> |
缩写:input:date
别名:inp[type=date]
1 | < input type = "date" name = "" id = "" /> |
缩写:input:datetime-local
别名:inp[type=datetime-local]
1 | < input type = "datetime-local" name = "" id = "" /> |
缩写:input:month
别名:inp[type=month]
1 | < input type = "month" name = "" id = "" /> |
缩写:input:week
别名:inp[type=week]
1 | < input type = "week" name = "" id = "" /> |
缩写:input:time
别名:inp[type=time]
1 | < input type = "time" name = "" id = "" /> |
缩写:input:number
别名:inp[type=number]
1 | < input type = "number" name = "" id = "" /> |
缩写:input:color
别名:inp[type=color]
1 | < input type = "color" name = "" id = "" /> |
缩写:input:checkbox
别名:inp[type=checkbox]
1 | < input type = "checkbox" name = "" id = "" /> |
缩写:input:c
别名:input:checkbox
1 | < input type = "checkbox" name = "" id = "" /> |
缩写:input:radio
别名:inp[type=radio]
1 | < input type = "radio" name = "" id = "" /> |
缩写:input:r
别名:input:radio
1 | < input type = "radio" name = "" id = "" /> |
缩写:input:range
别名:inp[type=range]
1 | < input type = "range" name = "" id = "" /> |
缩写:input:file
别名:inp[type=file]
1 | < input type = "file" name = "" id = "" /> |
缩写:input:f
别名:input:file
1 | < input type = "file" name = "" id = "" /> |
缩写:input:submit
1 | < input type = "submit" value = "" /> |
缩写:input:s
别名:input:submit
1 | < input type = "submit" value = "" /> |
缩写:input:image
1 | < input type = "image" src = "" alt = "" /> |
缩写:input:i
别名:input:image
1 | < input type = "image" src = "" alt = "" /> |
缩写:input:button
1 | < input type = "button" value = "" /> |
缩写:input:b
别名:input:button
1 | < input type = "button" value = "" /> |
缩写:isindex
缩写:input:reset
别名:input:button[type=reset]
1 | < input type = "reset" value = "" /> |
缩写:select
1 | < select name = "" id = "" ></ select > |
缩写:option
1 | < option value = "" ></ option > |
缩写:textarea
1 | < textarea name = "" id = "" cols = "30" rows = "10" ></ textarea > |
缩写:menu:context
别名:menu[type=context]>
1 | < menu type = "context" ></ menu > |
缩写:menu:c
别名:menu:context
1 | < menu type = "context" ></ menu > |
缩写:menu:toolbar
别名:menu[type=toolbar]>
1 | < menu type = "toolbar" ></ menu > |
缩写:menu:t
别名:menu:toolbar
1 | < menu type = "toolbar" ></ menu > |
缩写:video
缩写:audio
缩写:html:xml
1 | < html xmlns = "1999/xhtml" ></ html > |
缩写:keygen
缩写:command
缩写:bq
别名:blockquote
1 | < blockquote ></ blockquote > |
缩写:acr
别名:acronym
1 | < acronym title = "" ></ acronym > |
缩写:fig
别名:figure
缩写:figc
别名:figcaption
1 | < figcaption ></ figcaption > |
缩写:ifr
别名:iframe
1 | < iframe src = "" frameborder = "0" ></ iframe > |
缩写:emb
别名:embed
1 | < embed src = "" type = "" /> |
缩写:obj
别名:object
1 | < object data = "" type = "" ></ object > |
缩写:src
别名:source
缩写:cap
别名:caption
缩写:colg
别名:colgroup
缩写:fst, fset
别名:fieldset
缩写:btn
别名:button
缩写:btn:b
别名:button[type=button]
1 | < button type = "button" ></ button > |
缩写:btn:r
别名:button[type=reset]
1 | < button type = "reset" ></ button > |
缩写:btn:s
别名:button[type=submit]
<button type="submit"></button>
关于更多的HTML以及CSS的缩写请查看:官网文档