In this article, we will explore all about Emmet. A very popular and highly useful tool that is built into almost every IDE out there like Visual Studio Code, Sublime Text etc.
If you're using Codepen, then you might be happy to know that Emmet is also available in CodePen. So you can type abbreviations to generate code in Codepen.
What is so special about Emmet?
It allows us to just type some abbreviations and generate the output HTML and CSS code.
It increases your productivity to a great extent so you don't have to type the same repetitive code again and again.
Installation
Emmet is already available in almost every IDE so you don't need to install it.
If for some reason, it's not available in your IDE, you can install the extension from this page.
How to use it?
We just have to type the abbreviation and press the Tab
key and Emmet will convert it to the corresponding code.
Inside HTML file
- If you have created a new
.html
file, then instead of manually typing the doctype, head, meta, body, just type ! (exclamation mark) and press tab and Emmet will add the default HTML code
- If you want to create a div with the class
hero-banner
then instead of manually typing<div class="hero-banner">Some content</div>
, just type.hero-banner
and press tab key and the entire code will be generated for you.
As you can see, once the code is generated, Emmet automatically places the cursor inside the div so you don't need to click inside the div to type content inside the div.
- By default Emmet, considers a
div
when you don't specify the tag name.
But you can specify your own tag name also.
Suppose you want to create a section with two classes namely box
and showcase
then you just need to type section.box.showcase
and press the tab key.
- Generate div with id
numbers
Abbreviation: #numbers
- Generate div with 3 paragraphs
Abbreviation: div>p*3
As you can see, once the code is generated, Emmet automatically placed the cursor inside the paragraph so you just need to press the tab key to move to the next paragraph to type the content inside it
- Generate 3 paragraphs with lorem ipsum text inside div
Abbreviation: div>p*3>lorem
- Generate a div with id
btn
and classprimary-btn
Abbreviation: div#btn.primary-btn
- Generate ul with 3 li's and with an anchor tag inside it
Abbreviation: ul>li*3>a
- Generate ul with class
menu-items
and 3 li's with classmenu-item
and with an anchor tag inside it
Abbreviation: ul.menu-items>li*3.menu-item>a
- To generate 4 div's with h2 and ul inside it and 2 li's inside ul
Abbreviation: div*4>h2+ul>li*2
Here, we wanted h2 and ul to be side by side so we have used the + operator
- To generate ul with 4 li's inside it with class item1, item2, item3 and item4
Abbreviation: ul>li.item$*4
Here, $ represents an incrementing number starting with 1.
If for some reason, you want to generate a number starting with 0, use the abbreviation ul>li.item$@0*4
Here, we have specified the number after $ with @ symbol
- To generate a button with text
click here
inside it
Abbreviation: button{click here}
- To generate a paragraph with text
Click here to continue
text wherehere
is a link
Abbreviation: p>{Click }+a{here}+{ to continue}
- To generate input with type checkbox
Abbreviation: input:c or input:checkbox
You actually don't need to remember some abbreviations like input, just type input: and VS Code will suggest you with various Emmet abbreviations
- To generate script tag with the src attribute
Abbreviation: script:src
- To generate tags with some attribute, specify the attribute inside brackets
Abbreviation: a[href='#']
To specify multiple attributes separate them with spaces inside the brackets
Note: If for some reasons, the Emmet suggestion hides and pressing tab does not complete the code, just delete the last character of the abbreviation or press ctrl + space to get the Emmet suggestions and then press tab key.
Inside CSS file
Emmet also works in CSS files.
- To add background to the selector
Abbreviation: bg
- To add an absolute position to the selector
Abbreviation: pos:a
There are tons of abbreviations for CSS but you don't need to remember any of them. VS Code makes it really easy by providing suggestions while typing
- To add any property to the selector just type initial characters and then the next word of that property.
So to add background-color just type backc(back for background and c for color)
- To add background-size property just type backs
So for CSS, you don't need to remember the Emmet abbreviations. VS Code will help you out by providing suggestions.
Enable emmet for React
- In Visual Studio Code, press
Control + Shift + P
orCommand + Shift + P (Mac)
to open command palette and typesetting
and then selectPreferences: Open User Settings
option
Search for emmet in the search box as shown in the below screenshot
On the left side, expand the extension menu and click on
emmet
Then click on the
Add Item
button underEmmet: Include Languages
sectionAnd then enter
javascript
under theItem
textbox and enterjavascriptreact
under theValue
textbox.
- You're done.
Now open any component file from React application and type .albums
and press the tab key and it will be converted to <div className="albums"></div>
As you can see, Emmet automatically converts class
to className
, If we're in a .js
file.
- The great thing about Emmet is that you can generate an entire HTML page structure just using a single line of emmet abbreviation
div.container>h1.title+h2.subtitle+div>div*4>h3+ul>li*4>a
To find out more about such amazing abbreviations check out the Emmet Cheatsheet.
Conclusion
That's it about this article. In this article, we have seen that,
- Using Emmet inside HTML, CSS and even React javascript file, greatly improves productivity.
- We can generate an entire HTML page structure just using a single line of Emmet abbreviation.
- So there is no more need of wasting time typing div, classes, ids manually, let the Emmet do that job for you.
Thanks for reading!
Want to learn all ES6+ features in detail including let and const, promises, various promise methods, array and object destructuring, arrow functions, async/await, import and export and a whole lot more from scratch?
Check out my Mastering Modern JavaScript book. This book covers all the pre-requisites for learning React and helps you to become better at JavaScript and React.
Check out free preview contents of the book here.
Also, you can check out my free Introduction to React Router course to learn React Router from scratch.
Want to stay up to date with regular content regarding JavaScript, React, Node.js? Follow me on LinkedIn.