Introduction
In this article, we will talk 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 in React
By default, Emmet is not enabled for JSX in React in VS Code.
But you can enable it by following the below steps:
- In Visual Studio Code, press
Control + Shift + P
orCommand + Shift + P (Mac)
to open command palette and type setting and then select "Preferences: Open User Settings" option
- On the left side, expand the extension menu and click on Emmet
- Under
Include Languages
section, click onAdd Item
button and addjavascript
as a key andjavascriptreact
as a value and click onOk
Now open any component file in React 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.
Get my Mastering Modern JavaScript book at 40% off for the next 2 days only as a part of the Christmas sale.
Don't forget to subscribe to get my weekly newsletter with amazing tips, tricks, and articles directly in your inbox here.
Top comments (6)
When I first heard of vscode shortcuts, I was like, that's too much.
Now I can't leave w/o it.
Emmet looks like "too much" initially but got the same vscode vibe!
Looks worth investing time in~
Thank you for sharing, Yogesh!
That's true.. Installing a new extension to get something done every time does not sound good but there are some tools like emmet which we can't afford to miss considering the amount of time saved by it. Thanks for your feedback about it 🙂
This is great - Emmet is an amazing tool. An absolute must-have for anyone working on front-end development
Thanks, Kyle and You're right. It's a must-have tool for every developer making developers life so much easy and improves productivity a lot
Glad to see its working in React.
Awesome 👍