Feature Story

Visual Studio Code HTML Preview

Have you ever found yourself testing your HTML code by constantly opening it in a web browser? This can be quite tedious, especially when you're in the midst of a coding session. Visual Studio Code, a popular open-source code editor, offers a faster and more efficient solution: an integrated HTML preview feature. This article walks you through how to use and configure it for a seamless coding experience.

Get started with Visual Studio Code
Get started with Visual Studio Code

Visual Studio Code's Live Server extension enables real-time HTML preview directly from the editor. It updates the preview instantly as you type, eliminating the need to switch between windows and refreshing browsers manually. This innovative feature not only enhances productivity but also provides a better understanding of how your code translates into a web page.

an image of a computer screen with some text on it
an image of a computer screen with some text on it

Installing Live Server Extension

Before diving into how to use the HTML preview feature, let's install the Live Server extension.

SVG - Visual Studio Marketplace
SVG - Visual Studio Marketplace

1. Open Visual Studio Code and click on the Extensions view icon in the Activity Bar on the side of the window (or press Ctrl + Shift + X).

Searching for the Extension

the screenshot of an open source webpage with text and symbols in blue highlighted
the screenshot of an open source webpage with text and symbols in blue highlighted

2. In the search box, type 'Live Server' and press Enter.

3. Look for 'Live Server' by RickyBeyond in the results and click the Install button.

Launching Live Server

User interface
User interface

4. After installation, click on the Live Server icon (or press Ctrl + Alt + W to start the server for the current open folder) in the bottom-right corner of the editor to start the HTML preview.

5. Now, open an HTML file, and you'll see the real-time preview at 'http://127.0.0.1:5500/' in your default web browser.

Configuring Live Server Settings

HTML in Visual Studio Code
HTML in Visual Studio Code

The Live Server extension comes with customizable settings to tailor your HTML preview experience.

1. Open Command Palette (View > Command Palette or press Ctrl + Shift + P) and type 'Live Server: Open Settings (JSON).'

html and css login form
html and css login form
Visual Studio Code 12.5 Update Introduces Portable Mode, Grid Layout
Visual Studio Code 12.5 Update Introduces Portable Mode, Grid Layout
Visual studio extensions
Visual studio extensions
Palenight Theme - Visual Studio Marketplace
Palenight Theme - Visual Studio Marketplace
Visual Studio Shit
Visual Studio Shit
VS Code shortcuts that a beginner programmer needs
VS Code shortcuts that a beginner programmer needs

Adjusting Port Number

2. Inside the settings.json file, you can change the 'liveServer.settings.port' to use a different port for the preview server.

For example, setting 'liveServer.settings.port': 5000 will start the preview server on 'http://127.0.0.1:5000/' instead of the default 'http://127.0.0.1:5500/'

Enabling Auto Open Browser

3. To automatically open your default browser as soon as Live Server starts, set 'liveServer.settings.autoOpenBrowser' to true.

4. After adjusting the settings, save the file, and now the Live Server will open your browser automatically whenever you start previewing your HTML.

In conclusion, using Visual Studio Code's Live Server extension for HTML preview significantly speeds up your workflow and reduces distractions, allowing you to focus more on creating and less on testing. Embrace this tool to enhance your coding journey, and happy building!