What HTML and CSS are and how they work
HTML is a markup language that describes the structure and meaning of a page’s content: headings, paragraphs, links, forms. CSS is a language of rules that sets how these elements look and where they sit on the screen. The browser reads both and turns them into a picture of the page.
An HTML document is an ordinary text file with the .html extension; a site’s home page is usually called index.html. To see the result, save the file and open it in a browser tab. Even without any CSS of your own the page is readable: the browser has its own default styles (the user agent stylesheet) — headings are larger than body text, links are underlined. What the browser actually received and applied is shown by the developer tools — see the section “Common errors and troubleshooting”.
A minimal document
<!doctype html><html lang="ru"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>Class schedule</title> <link rel="stylesheet" href="styles.css"> </head> <body> <h1>Class schedule</h1> <p>Classes run on Tuesdays and Thursdays.</p> </body></html><!doctype html>— the mandatory first line: a historical relic without which the rest may not work correctly.<html>— the root element: everything else sits inside it.<head>— information about the page that the reader does not see: the encoding, the title, the styles.<meta charset="utf-8">— the UTF-8 encoding: text in most of the world’s languages displays without distortion.<meta name="viewport">— the width of the page on a phone; more detail in the section “Responsive design”.<title>— the name shown in the browser tab and in bookmarks.<body>— everything visible on the page.
How CSS is written
A stylesheet is a list of rules. A rule starts with a selector that picks out elements; the declaration block follows in curly braces. A declaration is a “property: value” pair, and declarations are separated by semicolons.
h1 { color: #1d3a8a; font-size: 2.5em;}Three ways to attach CSS
| Method | How to write it | When it fits |
|---|---|---|
| External file | <link rel="stylesheet" href="styles.css"> in <head> | the default: one file styles every page of the site |
| Internal stylesheet | <style>…</style> in <head> | a single page, or no access to the CSS files |
| Inline style | the style="color: purple" attribute on an element | a last resort: the style of a single element is mixed into the markup |
Choose an external file: it is attached to every page, and the style changes in one place. Rules from <style> would have to be repeated on each page, and the style attribute on every element.
How the browser builds the page
The browser parses the HTML into a DOM tree and the CSS into a separate CSSOM tree. From them it builds the render tree: only the visible nodes with their computed styles end up in it. The browser then works out the size and position of every box (layout), paints the pixels (paint) and stacks the page’s layers in the right order (composite). When sizes change — for example, an image without set width and height finishes loading — layout and paint run again.
display: none and the contents of <head> do not get into the render tree, while an element with visibility: hidden does: it takes up space.How Flexbox differs from Grid
| Feature | Flexbox | Grid |
|---|---|---|
| Dimensions | one: a row or a column | two: rows and columns at once |
| Where sizes come from | from the content: elements divide the space in their row | from the layout: first the grid, then elements placed into its cells |
| Wrapping to a new row | each row distributes the space on its own, columns do not line up | elements stay in strict rows and columns |
| Typical task | a row of items that need to be spaced out evenly inside a container | a layout into which items are placed by rows and columns |
Choose Flexbox when the layout needs controlling along a row or along a column only, and Grid when it needs controlling along rows and columns at the same time. If in Flexbox you set widths on items so that they line up exactly beneath the items of the row above, what you need is a grid. Both align items with the same properties from the CSS Box Alignment module.
What to know beforehand
- Working with files and folders: create a file, save it, open it in a browser
- Page behaviour, the DOM and events — the JavaScript reference
- How people use a page with a screen reader and from the keyboard — the “Accessibility (a11y)” reference
Section sources
the section is written from these pages- Populating the page: how browsers workdeveloper.mozilla.org/en-US/docs/Web/Performance/Guides/How_browsers_work(opens in a new tab)
- What is CSS?developer.mozilla.org/en-US/docs/Learn_web_development/Core/Styling_basics/What_is_CSS(opens in a new tab)
- Basic HTML syntaxdeveloper.mozilla.org/en-US/docs/Learn_web_development/Core/Structuring_content/Basic_HTML_syntax(opens in a new tab)
- Getting started with CSSdeveloper.mozilla.org/en-US/docs/Learn_web_development/Core/Styling_basics/Getting_started(opens in a new tab)
- Relationship of grid layout to other layout methodsdeveloper.mozilla.org/en-US/docs/Web/CSS/Guides/Grid_layout/Relationship_with_other_layout_methods(opens in a new tab)