Say hello to Emmet and write HTML with less pain ( more fun )

Pankaj Ladhar
3 min readApr 9, 2018

--

Sample Layout

If you have to create above mentioned Layout, you have to

  1. write loads of HTML tags
  2. add classes or ids for styling
  3. add content.

HTML code for sample layout

HTML code for sample layout

If you miss closing tag of one/some of the tag then finding them become so irritating. Time taken to type entire code character by character will be more time consuming.

Every developer wants to add some tools/automation to their workflow, so they can add speed and efficiency to their regular work.

In this case Emmet (formerly known as Zen Coding) becomes savior. It is a plugin for many popular text editors which greatly improves HTML & CSS workflow.

Emmet Wiki Link

How to configure Emmet in editor

  1. Visual Studio Code : https://code.visualstudio.com/docs/editor/emmet
  2. Sublime Text : https://github.com/sergeche/emmet-sublime#readme
  3. Atom : https://github.com/emmetio/emmet-atom#readme
  4. Notepad ++ : https://github.com/emmetio/npp#readme
  5. Dreamweaver : https://github.com/emmetio/dreamweaver#readme

I have listed some of majorly used editors. You can use this link to configure other editors.

In this story I will be explaining most commonly used feature of Emmet for HTML generation, using Visual Studio Code editor for sample.

How to use

Abbreviations are the heart of the Emmet toolkit: these special expressions are parsed in runtime and transformed into structured code block, HTML for example.

Child Elements:

Use > operator to nest elements inside each other.

Sample : div>p

Sibling Elements:

Use + operator to place elements near each other, on the same level.

Sample : div+p

Multiplication Elements:

Use * operator with how many times element should be outputted.

Sample : ul>li*5>a

ID and CLASS:

Use elem#id or elem.class to generate HTML with class or id.

Sample : div.someclass or div#someid

Use elem#id.class to generate HTML with class and id.

Sample : div#someid.someclass

Add Text to element:

Use {some text} add text to element.

Grouping Elements:

Use () grouping subtrees in complex abbreviation.

Sample : (div.root>div.child+p)+footer

Custom attributes

Use elem[attrname="value"] add attribute to element.

Notes 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:

(header > ul.nav > li*5) + footer

But it won’t work, because space is a stop symbol where Emmet stops abbreviation parsing.

Emmet code for above mentioned layout

div.wrapper#wrapper>(header>img.brand[src="http://somedomain.com/images/logo" alt="logo"]+nav.main_nav>ul>li*4>a[href="http://somedomain.com/item$"]{item$})+(div.container>aside+section)+(footer>nav.footer_nav>ul>li*4>a[href="http://somedomain.com/item$"]{item$})

Follow below mentioned link if you want to use more feature from Emmet :

If you like this post and it was helpful, please click the clap 👏 button below, thank you.

--

--