How to Use Emmet to Speed Up Development
Writing HTML can be a bit tedious a times. The free toolkit, Emmet, aims to take the monotony out of your coding and let you get back to being creative.
Join the DZone community and get the full member experience.Join For Free
Writing code, especially HTML and CSS, can be a very tedious process — writing closing element tags or duplicate HTML elements that serve as the page's structure are repetitive, monotonous tasks that take up a lot of time. Fortunately, there is a freely available plugin that can speed up this process immeasurably. Emmet is a web development toolkit for writing HTML and CSS that can drastically change your workflow for the better. It exists as a plugin, available for all of the most popular text editors (Atom, Brackets, Sublime Text, and even vim). While a small book would be required to explain the full breadth of Emmet's features, this post will aim to teach the very basics of this extremely useful plugin. After you get used to its syntax, you will wonder how you ever coded/lived without Emmet.
How Emmet Works
While many of the most popular text editors have some basic form of auto-completion pre-installed; Emmet takes this to a whole new level. After inputting a value within Emmet's syntax, hitting the launch key will transform this input into HTML and CSS far beyond what you initially typed (this launch key is the TAB key by default, but can be customized to your liking). For example, Emmet can perform all of the standard features of auto-completion, such as typing
p, followed by the
TAB key, will result in:
Classes and IDs
Writing classes and IDs is simplified. A class is denoted by prepending a
. before the class name, and an ID by prepending a
# before the ID. For example, typing
<p class="myClass" id="one"></p>
By default, any class or ID input that is not attached to a specified element will be assumed to be a
div. For example,
You can chain classes together, almost akin to chaining methods together in jQuery. Typing
<div class="class-one class-two class three"> </div>
You can nest multiple elements together. Typing
div>ul>li>a will produce children elements:
<div> <ul> <li> <a href="" > </li> </ul> </div>
Or you can create multiple adjacent siblings. For example,
<div></div> <p></p> <img src=""></img>
One of the most useful features of Emmet (and the one that arguably cuts down on repetitive typing the most) is multiplication. You can multiply elements by a certain number, only having to type a single element once. Typing
ul>li*7 will become:
<ul> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> </ul>
The full span of what Emmet is capable of is long. Luckily, there is fairly good documentation on the official site — this cheat sheet is particularly handy for the most common use-cases. While attempting to learn all of these shortcuts at the same time would be a tall order, incorporating even a small portion of these into your workflow can be a game-changer for your development efficiency.
Opinions expressed by DZone contributors are their own.