Buffer Yard
Language setup

Emacs HTML and CSS Setup: web-mode, Emmet and Live Preview

Updated

A folder with index.html, style.css and a few images: that is still how many landing pages, documentation sites and prototypes begin, and it is the case this page is built around. Emacs already opens both files in usable modes. What it lacks out of the box is fast markup entry, completion of CSS properties, and some way to see the page change without pressing reload by hand.

Which mode for which file

The choice of major mode depends on what the file contains, not on its extension alone.

File Mode Comes with
Static .html mhtml-mode Emacs
Templates (.blade.php, .twig, .erb, .j2) web-mode MELPA
.css css-ts-mode or css-mode Emacs
.scss scss-mode Emacs (in css-mode.el)
.json json-ts-mode Emacs 29+

mhtml-mode is what Emacs picks for .html files by default. It is html-mode with a twist: inside <style> and <script> blocks it switches indentation and highlighting to CSS and JavaScript. For static pages it is the right tool and needs nothing installed.

web-mode, by François-Xavier Bois, is a different animal. It parses markup with embedded code from more than a dozen template engines, highlights each part on its own terms, auto-closes tags, and lets you jump between matching tags with C-c C-n. If your HTML lives inside a framework’s views, use it. If you only write static pages, it adds options you will never touch.

For CSS, css-ts-mode (Emacs 29.1 and later, tree-sitter grammar css) is the more precise of the two built-in modes. css-mode remains a fine fallback without the grammar, and it has one small trick worth knowing: with css-fontify-colors on (the default), hex codes and colour names in the buffer are drawn on a swatch of their own colour, so a wrong shade of grey is visible before you ever open a browser.

Indentation in both CSS modes follows css-indent-offset, which defaults to four spaces. If your project uses two, set it once in your config or per project in .dir-locals.el, and the language server and Prettier will agree with what you type.

Emmet: type less markup

Emmet expands short abbreviations into markup. ul>li.item*3>a becomes a list with three items, each holding a link. section.hero>h1+p writes the skeleton of a hero block. Emmet has CSS abbreviations too: m10 expands to margin: 10px;.

The emmet-mode package from MELPA brings this to Emacs, bound to C-j: type the abbreviation, press C-j, and it expands at point. Once you know a dozen abbreviations it is the single largest speed-up on this page, more than any language server.

(use-package emmet-mode
  :ensure t
  :hook (mhtml-mode web-mode css-mode css-ts-mode)
  :config
  (add-to-list 'emmet-css-major-modes 'css-ts-mode))

The CSS abbreviations depend on emmet-mode recognising the buffer as a stylesheet. Its list of CSS modes, emmet-css-major-modes, predates the tree-sitter ones, which is why the :config line adds css-ts-mode to it. Without that line, m10 in a .css file opened in css-ts-mode expands to an <m10> tag instead of a declaration.

Since C-j normally inserts a newline and indents, some people move Emmet to TAB or another key. Leave it where it is for a week first; the default is easier to learn than to remap twice.

Language servers: one npm package, three servers

The npm package vscode-langservers-extracted holds the HTML, CSS and JSON language servers extracted from VS Code, plus an ESLint server:

npm install -g vscode-langservers-extracted

That puts vscode-html-language-server, vscode-css-language-server and vscode-json-language-server on your path. The CSS server is the most useful of the three: completion of property names and values, validation of unknown properties, colour information and hover documentation drawn from MDN data. The HTML server completes tags and attributes; the JSON server validates files against schemas, which matters for package.json and tsconfig.json.

eglot, built into Emacs since 29.1, needs to be told which mode talks to which binary. Recent eglot versions have defaults for some of these, but writing them out costs three lines and removes the guesswork:

(use-package css-mode
  :mode ("\\.css\\'" . css-ts-mode)
  :hook (css-ts-mode . eglot-ensure))

(use-package eglot
  :config
  (add-to-list 'eglot-server-programs
               '((html-mode mhtml-mode web-mode) "vscode-html-language-server" "--stdio"))
  (add-to-list 'eglot-server-programs
               '((css-mode css-ts-mode scss-mode) "vscode-css-language-server" "--stdio"))
  (add-to-list 'eglot-server-programs
               '((json-mode json-ts-mode) "vscode-json-language-server" "--stdio")))

(add-hook 'mhtml-mode-hook #'eglot-ensure)

Server completions show up through completion-at-point, so the popup you set up on the completion page handles them. If you also want Prettier for HTML and CSS, the apheleia setup from the JavaScript and TypeScript guide covers these modes too.

Seeing the page while you type

There is no single answer here; it depends on how live you need it.

Open it once. In html-mode and mhtml-mode, C-c C-v runs browse-url-of-buffer and opens the file in your default browser. Good enough for a quick look.

impatient-mode. This MELPA package serves buffers over a small built-in web server (from the simple-httpd package). Run M-x httpd-start, turn on impatient-mode in the HTML buffer and in the CSS buffer, and open http://localhost:8080/imp/ in a browser. The page updates as you type, before you even save. It is the most Emacs-native option and works best for single pages.

browser-sync. For a whole site, an external watcher is more dependable:

npx browser-sync start --server --files "*.html, css/*.css"

It serves the folder, injects CSS changes without a reload and reloads the page when HTML changes. Emacs does not need to know about it; saving the file is the trigger. Start it from M-x async-shell-command and it keeps running in its own buffer.

eww is the one to skip for this purpose. It renders HTML inside Emacs but ignores most CSS, so it tells you about structure, not appearance.

Templates and the rest

If your markup lives inside a PHP framework, the PHP setup shows the web-mode engine settings for Blade and Twig. The package guide explains the MELPA side of :ensure t, and the languages overview lists the other guides. One small web-mode setting worth trying right away is web-mode-enable-current-element-highlight, which marks the element around point and makes deep nesting far easier to read.

Questions readers ask

Should I use web-mode or mhtml-mode for plain HTML?

For hand-written static HTML, mhtml-mode is enough: it ships with Emacs, handles embedded CSS and JavaScript, and has no configuration. web-mode earns its place once templates enter the picture (Blade, Twig, ERB, Go templates, Jinja), because it understands those engines inside the markup.

Do the VS Code language servers need VS Code installed?

No. The vscode-langservers-extracted package on npm contains the HTML, CSS, JSON and ESLint servers taken out of VS Code's source and runs them under plain Node.js. Install it once with npm and point eglot at the binaries.

Can Emacs show a live preview of a page?

Inside Emacs, eww renders HTML but ignores most CSS, so it only helps for checking structure. For a real preview, impatient-mode serves the buffer to your browser and updates on each change, and browser-sync reloads the browser whenever a file is saved.