blog
2020-08-11
2 阅读
emmet使用方法
Emmet uses syntax similar to CSS selectors for describing elements’ positions inside generated tree and elements’ attributes.
Child:
You can use Sibling:
Use Climb-up:
With Multiplication:
With Grouping:
Parenthesises are used by Emmets’ power users for grouping subtrees in complex abbreviations:
Elements
You can use elements’ names likediv or p to generate HTML tags. Emmet doesn’t have a predefined set of available tag names, you can write any word and transform it into a tag: div → , foo → and so on.
Nesting operators
Nesting operators are used to position abbreviation elements inside generated tree: whether it should be placed inside or near the context element.Child: >
You can use > operator to nest elements inside each other:
div>ul>li
...will produce
<div>
<ul>
<li>li>
ul>
div>
Sibling: +
Use + operator to place elements near each other, on the same level:
div+p+bq
...will output
<div>div>
<p>p>
<blockquote>blockquote>
Climb-up: ^
With > operator you’re descending down the generated tree and positions of all sibling elements will be resolved against the most deepest element:
div+div>p>span+em
...will be expanded to
<div>div>
<div>
<p><span>span><em>em>p>
div>
With ^ operator, you can climb one level up the tree and change context where following elements should appear:
div+div>p>span+em^bq
...outputs to
<div>div>
<div>
<p><span>span><em>em>p>
<blockquote>blockquote>
div>
You can use as many ^ operators as you like, each operator will move one level up:
div+div>p>span+em^^^bq
...will output to
<div>div>
<div>
<p><span>span><em>em>p>
div>
<blockquote>blockquote>
Multiplication: *
With * operator you can define how many times element should be outputted:
ul>li*5
...outputs to
<ul>
<li>li>
<li>li>
<li>li>
<li>li>
<li>li>
ul>
Grouping: ()
Parenthesises are used by Emmets’ power users for grouping subtrees in complex abbreviations:
div>(header>ul>li*2>a)+footer>p
...expands to
<div>
<header>
<ul>
<li><a href="">a>li>
<li><a href="">a>li>
ul>
header>
<footer>
<p>p>
footer>
div>
If you’re working with browser’s DOM, you may think of groups as Document Fragments: each group contains abbreviation subtree and all the following elements are inserted at the same level as the first element of group.
You can nest groups inside each other and combine them with multiplication * operator:
(div>dl>(dt+dd)*3)+footer>p
...produces
<div>
<dl>
<dt>dt>
<dd>dd>
<dt>dt>
<dd>dd>
<dt>dt>
<dd>dd>
dl>
div>
<footer>
<p>p>
footer>
With groups, you can literally write full page mark-up with a single abbreviation, but please don’t do that.
Attribute operators
Attribute operators are used to modify attributes of outputted elements. For example, in HTML and XML you can quickly addclass attribute to generated element.
ID and CLASS
In CSS, you useelem#id and elem.class notation to reach the elements with specified id or class attributes. In Emmet, you can use the very same syntax to add these attributes to specified element:
div#header+div.page+div#footer.class1.class2.class3
...will output
<div id="header">div>
<div class="page">div>
<div id="footer" class="class1 class2 class3">div>
Custom attributes
You can use[attr] notation (as in CSS) to add custom attributes to your element:
td[title="Hello world!" colspan=3]
...outputs
<td title="Hello world!" colspan="3">td>
- You can place as many attributes as you like inside square brackets.
- You don’t have to specify attribute values:
td[colspan title]will producewith tabstops inside each empty attribute (if your editor supports them). - You can use single or double quotes for quoting attribute values.
- You don’t need to quote values if they don’t contain spaces:
td[title=hello colspan=3]will work.Item numbering:
With multiplication$*operator you can repeat elements, but with$you can number them. Place$operator inside element’s name, attribute’s name or attribute’s value to output current number of repeated element:
...outputs toul>li.item$*5
You can use multiple<ul> <li class="item1">li> <li class="item2">li> <li class="item3">li> <li class="item4">li> <li class="item5">li> ul>$in a row to pad number with zeroes:
...outputs toul>li.item$$$*5<ul> <li class="item001">li> <li class="item002">li> <li class="item003">li> <li class="item004">li> <li class="item005">li> ul>Changing numbering base and direction
With@modifier, you can change numbering direction (ascending or descending) and base (e.g. start value). For example, to change direction, add@-after$:
…outputs toul>li.item$@-*5
To change counter base value, add<ul> <li class="item5">li> <li class="item4">li> <li class="item3">li> <li class="item2">li> <li class="item1">li> ul>@Nmodifier to$:
…transforms toul>li.item$@3*5
You can use these modifiers together:<ul> <li class="item3">li> <li class="item4">li> <li class="item5">li> <li class="item6">li> <li class="item7">li> ul>
…is transformed toul>li.item$@-3*5<ul> <li class="item7">li> <li class="item6">li> <li class="item5">li> <li class="item4">li> <li class="item3">li> ul>Text:
You can use curly braces to add text to element:{}
...will producea{Click me}
Note that<a href="">Click mea>{text}is used and parsed as a separate element (like,div,petc.) but has a special meaning when written right after element. For example,a{click}anda>{click}will produce the same output, buta{click}+b{here}anda>{click}+b{here}won’t:
In second example the<a href="">clicka><b>hereb> <a href="">click<b>hereb>a>element is placed insideelement. And that’s the difference: when{text}is written right after element, it doesn’t change parent context. Here’s more complex example showing why it is important:
...producesp>{Click }+a{here}+{ to continue}
In this example, to write<p>Click <a href="">herea> to continuep>Click here to continueinsideelement we have explicitly move down the tree with>operator afterp, but in case ofaelement we don’t have to, since we needelement withhereword only, without changing parent context. For comparison, here’s the same abbreviation written without child>operator:
...producesp{Click }+a{here}+{ to continue}<p>Click p> <a href="">herea> to continueNotes on abbreviation formatting
When you get familiar with Emmet’s abbreviations syntax, you may want to use some formatting to make your abbreviations more readable. For example, use spaces between elements and operators, like this:
But it won’t work, because space is a stop symbol where Emmet stops abbreviation parsing. Many users mistakenly think that each abbreviation should be written in a new line, but they are wrong: you can type and expand abbreviation anywhere in the text: https://docs.emmet.io/abbreviations/syntax/(header > ul.nav > li*5) + footer