search
HomeWeb Front-endHTML Tutorialemmet 教程 emmet快捷键大全_html/css_WEB-ITnose

Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生。它使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度,比如下面的演示:

Zen coding下的编码演示

去年年底,该插件已经改名为Emmet。 但Emmet不只改名,还带来了一些新特性。本文就来直观地演示给你。

一、快速编写HTML代码

1.  初始化

HTML文档需要包含一些固定的标签,比如、

、等,现在你只需要1秒钟就可以输入这些标签。比如输入“!”或“html:5”,然后按Tab键: 

  • html:5 或!:用于HTML5文档类型
  • html:xt:用于XHTML过渡文档类型
  • html:4s:用于HTML4严格文档类型

2.  轻松添加类、id、文本和属性

连续输入元素名称和ID,Emmet会自动为你补全,比如输入p#foo: 

连续输入类和id,比如p.bar#foo,会自动生成: 

<p class="bar" id="foo"></p>  

下面来看看如何定义HTML元素的内容和属性。你可以通过输入h1{foo}和a[href=#],就可以自动生成如下代码:

<h1 id="foo">foo</h1>  <a href="#"></a>  

3.  嵌套

现在你只需要1行代码就可以实现标签的嵌套。 

  • >:子元素符号,表示嵌套的元素
  • +:同级标签符号
  • ^:可以使该符号前的标签提升一行

效果如下图所示:

4.  分组

你可以通过嵌套和括号来快速生成一些代码块,比如输入(.foo>h1)+(.bar>h2),会自动生成如下代码: 

<div class="foo">    <h1></h1>  </div>  <div class="bar">    <h2></h2>  </div>  

5.  隐式标签

声明一个带类的标签,只需输入div.item,就会生成

。 

在过去版本中,可以省略掉div,即输入.item即可生成

。现在如果只输入.item,则Emmet会根据父标签进行判定。比如在
    中输入.item,就会生成
  • 。 

    下面是所有的隐式标签名称: 

    • li:用于ul和ol中
    • tr:用于table、tbody、thead和tfoot中
    • td:用于tr中
    • option:用于select和optgroup中

    6.  定义多个元素

    要定义多个元素,可以使用*符号。比如,ul>li*3可以生成如下代码: 

    <ul>    <li></li>    <li></li>    <li></li>  </ul>  

    7.  定义多个带属性的元素

    如果输入 ul>li.item$*3,将会生成如下代码: 

    <ul>    <li class="item1"></li>    <li class="item2"></li>    <li class="item3"></li>  </ul>  

    二、CSS缩写

    1.  值

    比如要定义元素的宽度,只需输入w100,即可生成 

    width: 100px;  

    除了px,也可以生成其他单位,比如输入h10p+m5e,结果如下: 

    height: 10%;  margin: 5em;  

    单位别名列表: 

    • p 表示%
    • e 表示 em
    • x 表示 ex

    2.  附加属性

    可能你之前已经了解了一些缩写,比如 @f,可以生成: 

    @font-face {    font-family:;    src:url();  }  

    一些其他的属性,比如background-image、border-radius、font、@font-face,text-outline、text-shadow等额外的选项,可以通过“+”符号来生成,比如输入@f+,将生成: 

    @font-face {    font-family: 'FontName';    src: url('FileName.eot');    src: url('FileName.eot?#iefix') format('embedded-opentype'),       url('FileName.woff') format('woff'),       url('FileName.ttf') format('truetype'),       url('FileName.svg#FontName') format('svg');    font-style: normal;    font-weight: normal;  }  

    3.  模糊匹配

    如果有些缩写你拿不准,Emmet会根据你的输入内容匹配最接近的语法,比如输入ov:h、ov-h、ovh和oh,生成的代码是相同的: 

    overflow: hidden;  

    4.  供应商前缀

    如果输入非W3C标准的CSS属性,Emmet会自动加上供应商前缀,比如输入trs,则会生成: 

    -webkit-transform: ;  -moz-transform: ;  -ms-transform: ;  -o-transform: ;  transform: ;  

    你也可以在任意属性前加上“-”符号,也可以为该属性加上前缀。比如输入-super-foo: 

    -webkit-super-foo: ;  -moz-super-foo: ;  -ms-super-foo: ;  -o-super-foo: ;  super-foo: ;  

    如果不希望加上所有前缀,可以使用缩写来指定,比如-wm-trf表示只加上-webkit和-moz前缀: 

    -webkit-transform: ;  -moz-transform: ;  transform: ;  

    前缀缩写如下: 

    • w 表示 -webkit-
    • m 表示 -moz-
    • s 表示 -ms-
    • o 表示 -o-

    5.  渐变

    输入lg(left, #fff 50%, #000),会生成如下代码: 

    background-image: -webkit-gradient(linear, 0 0, 100% 0, color-stop(0.5, #fff), to(#000));  background-image: -webkit-linear-gradient(left, #fff 50%, #000);  background-image: -moz-linear-gradient(left, #fff 50%, #000);  background-image: -o-linear-gradient(left, #fff 50%, #000);  background-image: linear-gradient(left, #fff 50%, #000);  

    三、附加功能

    生成Lorem ipsum文本

    Lorem ipsum指一篇常用于排版设计领域的拉丁文文章,主要目的是测试文章或文字在不同字型、版型下看起来的效果。通过Emmet,你只需输入lorem 或 lipsum即可生成这些文字。还可以指定文字的个数,比如lorem10,将生成: 

    引用

    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Libero delectus.

    四、定制

    你还可以定制Emmet插件: 

    • 添加新缩写或更新现有缩写,可修改 snippets.json 文件
    • 更改Emmet过滤器和操作的行为,可修改 preferences.json 文件
    • 定义如何生成HTML或XML代码,可修改 syntaxProfiles.json 文件

    五、针对不同编辑器的插件

    Emmet支持的编辑器如下(链接为针对该编辑器的Emmet插件): 

    • Sublime Text 2
    • TextMate 1.x
    • Eclipse/Aptana
    • Coda 1.6 and 2.x
    • Espresso
    • Chocolat  (通过“Install Mixin”对话框添加)
    • Komodo Edit/IDE  (通过Tools → Add-ons菜单添加)
    • Notepad++
    • PSPad
    • CodeMirror2/3
    • Brackets

    相关文档: http://docs.emmet.io/ (其中包含了一个Demo,你可以试验文中所提到的这些缩写) 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Mar 04, 2025 pm 12:32 PM

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

How do I use HTML5 form validation attributes to validate user input?How do I use HTML5 form validation attributes to validate user input?Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

What are the best practices for cross-browser compatibility in HTML5?What are the best practices for cross-browser compatibility in HTML5?Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

How to efficiently add stroke effects to PNG images on web pages?How to efficiently add stroke effects to PNG images on web pages?Mar 04, 2025 pm 02:39 PM

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

What is the purpose of the <datalist> element?What is the purpose of the <datalist> element?Mar 21, 2025 pm 12:33 PM

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <progress> element?What is the purpose of the <progress> element?Mar 21, 2025 pm 12:34 PM

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

How do I use the HTML5 <time> element to represent dates and times semantically?How do I use the HTML5 <time> element to represent dates and times semantically?Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

What is the purpose of the <meter> element?What is the purpose of the <meter> element?Mar 21, 2025 pm 12:35 PM

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.