React JS: HTML basics
HTML for React (1/5)
1 Overview
📘 HTML HyperText Markup Language is the standard markup language for creating web pages. It describes the structure of a page as a tree of elements: “this is a heading”, “this is a paragraph”, “this is a link”, and so on.
React does not replace HTML: every React component ends up rendering HTML elements into the DOM. JSX is just HTML-like syntax inside JavaScript, so knowing HTML well is the first step to writing good JSX.
2 Why HTML before React?
When we write a component like this:
the browser never sees JSX. React translates it and finally the DOM contains a plain <h1 class="title">. So:
- the tags we use in JSX are HTML tags (
div,p,ul,form,button…), - the attributes are (almost) HTML attributes (
src,alt,href,type…), - the events we handle in React (
onClick,onChange,onSubmit) are the browser’s HTML DOM events (see Events and the DOM).
If you know which HTML you want in the browser, writing the JSX is easy. If you don’t, React only makes it harder.
3 HTML document structure
A minimal HTML5 document looks like this:
index.html
<!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>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>My first heading</h1>
<p>My first paragraph.</p>
<script src="app.js"></script>
</body>
</html><!DOCTYPE html>tells the browser this is an HTML5 document.<html>is the root element of the page.<head>holds meta information, not shown on the page:<meta charset>: character encoding (useUTF-8).<meta name="viewport">: makes the page responsive on mobile.<title>: the text in the browser tab (and in search results).<link>: links external resources, mostly CSS stylesheets.<script>: JavaScript, inline or from a file (src). It can also go at the end of<body>.
<body>contains everything visible: headings, paragraphs, images, links, tables, lists…
5 Attributes
Attributes give extra information about an element. They always go in the start tag as name="value".
| Attribute | Used on | What it does |
|---|---|---|
id |
any element | A unique identifier in the page (for CSS, JS, labels) |
class |
any element | One or more class names, shared by many elements (CSS) |
style |
any element | Inline CSS: style="color:red; font-size:20px" |
href |
<a>, <link> |
URL of the link or resource |
src |
<img>, <script> |
Path of the image or script |
alt |
<img> |
Alternative text for the image |
type |
<input>, <button> |
Kind of input or button |
name |
form fields | Key of the field when the form is sent |
value |
form fields | Current or initial value of the field |
lang |
<html> |
Language of the page |
6 Summary
| Group | Tags |
|---|---|
| Structure | <!DOCTYPE html>, <html>, <head>, <body>, <meta>, <title>, <link>, <script> |
| Text | <h1>–<h6>, <p>, <strong>, <em>, <br>, <hr> |
| Links/media | <a>, <img> |
| Lists | <ul>, <ol>, <li> |
| Tables | <table>, <thead>, <tbody>, <tr>, <th>, <td> |
| Containers | <div> (block), <span> (inline) |
| Semantic | <header>, <nav>, <main>, <section>, <article>, <aside>, <footer> |
Forms have their own article: HTML forms.
1. HTML basics · 2. Forms · 3. Events and the DOM · 4. From HTML to JSX · 5. CSS basics
Next ➡️ HTML forms
7 References
- W3Schools: HTML Introduction
- W3Schools: HTML Basic Examples
- W3Schools: HTML Elements
- W3Schools: HTML Attributes
- W3Schools: HTML Headings and Paragraphs
- W3Schools: HTML Links and Images
- W3Schools: HTML Lists and Tables
- W3Schools: HTML Block and Inline Elements
- W3Schools: HTML Layout and Semantic Elements
- W3Schools: HTML Head
- React JS: JSX and React JS: render virtual DOM