Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to run HTML code in visual studio code

Understanding Visual Studio Code

Visual Studio Code, often abbreviated as VS Code, is an open-source code editor developed by Microsoft. It supports a variety of programming languages, including HTML (HyperText Markup Language), which is the standard markup language for documents designed to be displayed in a web browser.

In this context, 'markup language' means a system for annotating a document in a way that is visually distinguishable from the content. Think of it like marking up a script for a play where you highlight different characters' lines in different colors. HTML helps determine the structure and layout of a web page, much like how an architect's blueprint determines the layout of a building.

Setting Up VS Code

Before we start running HTML code, we need to ensure that VS Code is correctly set up on your computer. Follow these steps to install it:

  1. Visit the Visual Studio Code website (https://code.visualstudio.com/) and download the version appropriate for your operating system.
  2. Once downloaded, run the installer and follow the prompts. These steps will be familiar if you've ever installed software before.
  3. After installation, open VS Code. The welcome screen will guide you to the interface.

Creating a New HTML File

To create a new HTML file in VS Code, follow these steps:

  1. Click on File in the menu bar, then select New File (or use the shortcut Ctrl+N). This opens a new, blank file.
  2. To save this file as an HTML file, click on File, then Save As. Name your file with a .html extension, like example.html.

Writing HTML Code in VS Code

Now, for the fun part! Let's start by writing a simple HTML code. Copy the following code into your new HTML file:

<!DOCTYPE html>
<html>
    <head>
        <title>My First HTML Page</title>
    </head>
    <body>
        <h1>Hello, World!</h1>
    </body>
</html>

This is a basic HTML document structure. It includes a <!DOCTYPE html> declaration (which tells the browser that this is an HTML5 document), an <html> element (which contains the whole HTML document), a <head> element (which contains meta-information about the document), a <title> element (which specifies a title for the document), and a <body> element (which contains the document's content).

Running HTML Code in VS Code

You don't "run" HTML code in the same way you run programs written in languages like Python or JavaScript. HTML is a static language, meaning the browser reads the file and renders the content as described by the HTML tags.

To view your HTML file in a browser, follow these steps:

  1. Save your HTML file by clicking on File, then Save, or use the shortcut Ctrl+S.
  2. Right-click anywhere in your HTML file.
  3. Select Open with Live Server. This will open your default web browser and display your HTML file.

If you don't see the 'Open with Live Server' option, it means you need to install the Live Server extension. Go to the Extensions view (the square icon on the left sidebar), search for 'Live Server' and click Install. This handy extension allows you to run a local development server and live reload feature for static and dynamic pages.

Making Changes and Seeing Them in Real Time

One of the great features of using VS Code and Live Server is the ability to see your changes in real time. When you make changes to your HTML file in VS Code, those changes are immediately reflected in your browser.

Try it now by changing the text inside the <h1> tags from 'Hello, World!' to something else. Save your file (Ctrl+S), and watch your browser update automatically!

Conclusion

Congratulations! You've now learned how to write and run HTML code in Visual Studio Code. Remember, programming is a skill that improves with practice. Don't be afraid to experiment with different HTML tags and attributes to see how they affect the look of your webpage. Happy coding!

Remember, understanding HTML and being comfortable with VS Code is the first step towards web development. As you become more confident, you can start exploring other technologies such as CSS (Cascading Style Sheets) and JavaScript to add style and interactivity to your web pages.