HTML Basics
HTML basics cover the minimum structure every web page needs and the handful of elements you will use on almost every page: headings, paragraphs, links, images and lists. HTML describes what each piece of content is; the browser reads that description and builds the page. Styling comes later with CSS, and behaviour with JavaScript.
The skeleton of every page
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My First Page</title>
</head>
<body>
<h1>Hello, web!</h1>
<p>This is my first paragraph.</p>
<a href="https://developer.mozilla.org">Read the HTML docs</a>
<img src="images/cat.jpg" alt="A grey cat asleep on a sofa" width="400" height="300">
<ul>
<li>HTML for structure</li>
<li>CSS for style</li>
<li>JavaScript for behaviour</li>
</ul>
</body>
</html>
Line by line:
<!DOCTYPE html>tells the browser to use standards mode. Leaving it out triggers older quirks mode, where layouts behave unpredictably.<html lang="en">wraps everything and declares the language, which helps screen readers and translation tools.<head>holds information about the page that is not shown directly: character set, viewport settings, the title shown in the browser tab, and later links to CSS.<body>holds everything visitors actually see.
Tags, elements and attributes
A tag is the text in angle brackets. Most elements have an opening tag and a closing tag with a slash, and content in between: <p>Text</p>. Some elements, such as <img>, <br> and <meta>, are void elements: they have no content and no closing tag.
Attributes add extra information inside the opening tag as name and value pairs. href tells a link where to go, src tells an image which file to load, and alt provides a text description. Put attribute values in double quotes.
The elements you will use daily
| Element | Used for |
|---|---|
<h1> to <h6> | Headings, from the main title down to small subheadings. |
<p> | A paragraph of text. |
<a> | A link to another page, file or section. |
<img> | An image. |
<ul>, <ol>, <li> | Bulleted and numbered lists and their items. |
Use headings in order, one <h1> for the page title, then <h2> for sections and <h3> inside those. Pick headings for meaning, not size; CSS controls size.
Nesting correctly
Elements sit inside one another like boxes. Whatever opens last must close first. <p><strong>Bold</strong></p> is correct; <p><strong>Bold</p></strong> is not. Browsers try to repair broken nesting, but they guess, and the guess may differ from what you meant.
Beginner slips to avoid
- Missing or empty
alttext on meaningful images leaves screen reader users with nothing. Describe what the image shows. Usealt=""only for purely decorative images. - Wrong file paths.
src="images/cat.jpg"is relative to the HTML file. If the image is not showing, check the folder name and letter case; many servers treatCat.JPGandcat.jpgas different files. - Spaces and line breaks in your code collapse into a single space on screen. Use separate paragraphs rather than pressing Enter many times.
- Saving with the wrong extension. The file must end in
.html, and the home page is usually namedindex.html.
Try it yourself
Save the skeleton above as index.html, open it in a browser, change the heading and add a second list item. Then right-click the page and choose View Page Source to compare what you wrote with what the browser shows. These few elements and the doctype, head and body structure are the foundation every later HTML topic builds on.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.