The Basics of HTML and CSS: Build Your First Web Page

Code Playground is only enabled on larger screen sizes.

HTML and CSS are the foundations of a webpage, and they are also our first step towards becoming a web developer.

HTML defines the structure and content of the webpage, while CSS enhances the style and appearance, and together, they create the webpages we see today.

In this chapter, we’ll explore the fundamentals of HTML and CSS, giving you the essential skills to start building and styling your own webpages.

What is HTML?

HTML stands for HyperText Markup Language. It defines the structure and content of webpages using HTML elements.

To start writing HTML code, try the code demo below:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>

    <link rel="stylesheet" href="/styles.css" />
  </head>

  <body>
    <p>Hello, World!</p>
  </body>
</html>

Modify the source code directly to see how it affects the rendered webpage. For example, change the content between <p>...</p>:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>

    <link rel="stylesheet" href="/styles.css" />
  </head>

  <body>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
  </body>
</html>

Save the changes, and the rendered webpage should be updated as well.

Set up your computer (Optional)

For our first chapter, you can test all the code snippets directly in the code playground. However, if you prefer setting up a local dev environment, here is how you can do it:

First, you'll need a code editor to write and edit your code.

Visual Studio Code is a great option for beginners (and professionals, for that matter). Download the appropriate installer for your operating system from their official website.

download vscode

After you've installed VSCode, make sure to install the Live Server extension as well.

Navigate to the Extensions tab on the left sidebar, and type in Live Server in the search box.

From there, you'll be able to download and install the extension.

install live server

Live Server will create a local development server with the auto-reload feature. For example, create a new work directory and open it using VSCode.

empty directory

Create a new file named index.html under this directory. The .html extension indicates that this is an HTML document.

Type in ! in VSCode, and you will see suggestions like this:

new html doc

This is a shortcut for creating HTML documents quickly. You can navigate with the ↑ or ↓ keys.

Select the first option, and the following code should be generated.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body></body>
</html>

Notice that at the bottom right corner of the VSCode window, there is a Go Live button.

go live button

Clicking this button will activate the Live Server extension. A local development server will be started, hosting the index.html file you just created.

empty html

Of course, the file is still empty right now, so you can't see anything. Add something between the <body> and </body> tags.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <p>Hello, world!</p>
  </body>
</html>

Save the changes, and the webpage will be automatically refreshed with the new content.

html with hello world

The structure of an HTML document

A typical HTML document always has the following structure:

<!DOCTYPE html>
<html lang="en">
  <head>
    . . .
  </head>
  <body>
    . . .
  </body>
</html>

The <!DOCTYPE html> tag defines the document type.

When the web browser encounters <!DOCTYPE html>, it understands that the page should be parsed and displayed according to the specifications of HTML5, the latest version of HTML standard.

This ensures that modern browsers interpret the webpage's content and layout correctly.

Everything else in the file should be enclosed inside an <html> element, defined by an opening tag (<html>) and a closing tag (</html>).

<html lang="en">
  . . .
</html>

lang is called an attribute, and it has the value "en" in our example. This tells the browser as well as the search engine that English is the primary language used for this webpage.

Inside the <html> element, there are two child elements, <head> and <body>.

<html lang="en">
  <head>
    . . .
  </head>
  <body>
    . . .
  </body>
</html>

<head> contains metadata and other information about the HTML document. This information will not be displayed in the browser but is often used by search engines for SEO (Search Engine Optimization) purposes.

<body>, on the other hand, contains the main content of the webpage that is visible to the users, and for that reason, it is also the part of the HTML file we are going to focus on in this chapter.

Elements and attributes

The HTML document is made of different HTML elements in a nested structure.

Most elements have both an opening tag and a closing tag:

<tag>. . .</tag>

In this case, <tag> is the opening tag, and </tag> is the closing tag, and together, they define an HTML element.

An element could hold textual content between the opening and closing tags.

<tag>Hello, world!</tag>

Or wrap around other elements, forming a nested structure.

<tag>
  <tag>. . .</tag>
  <tag>
    <tag>. . .</tag>
  </tag>
</tag>

Inside the opening tag, you can define attributes, which are used to specify additional information about the element, such as its class, id, and so on.

<tag attribute="value">. . .</tag>

The attribute is usually in a key/value pair, and the value must always be enclosed inside matching quotes (double or single).

There are some exceptions to these general formats.

For example, the <br /> element, which creates a line break, does not need an opening tag.

Some attributes, such as multiple, do not require a value.

However, you should remember that if an element does require a pair of tags, then it should never be overlooked.

In most cases, the webpage could still render correctly, but as the structure of your HTML document grows more complex, unexpected errors may occur.