HTML & CSS

Web pages rely on 3 technologies

  1. 1. HTML
  2. 2. CSS
  3. 3. JavaScript (next course)

Required reading on website bloat

HTML

HyperText Markup Language

HTML is the structure of all web pages

Web page
HTML source

An HTML page is a tree of elements

HTML
<section>
  <header>
    <h2>Header Title</h2>
  </header>
  <p>A paragraph</p>
</section>
Page
Tree

Syntax of elements

Void elements need no closing tag
<img src="cat.jpg">

Browsers are lenient in parsing

HTML
<section>
  <header>
    <h2>Header Title</h3>
  <p>A paragraph<p>
</scetion>
Page

Badly-formed HTML may still render correctly

Use validators (or offline tools)

Elements can have attributes

<img src="moa-moe.jpg"
     title="A description text"
     alt="Picture of a moa bird
          with moe eyes"/>

A link to example.com

<a href="http://example.com">
  A link to example.com
</a>

Syntax:
<tag attr1="val" attr2="val" ...>

Syntax: name="string"

Some attributes apply to all elements

<div id="specialDiv"></div>
id is used for navigation.
The value of `id` is unique in the page.

<span class="literal number">12 </span> class is useful for styling.
Elements can share one or more classes.

Common HTML elements

Reference list on MDN

HTML stands for HyperText

<a href="/pages/about.html"> links to a page on the same domain

href="https://example.com/welcome/" links to another domain

href="#el" links to the element with attribute id="el"

Elements give semantic structure to content

Semantic tags help readability and accessibility (MDN tutorial)

A typical HTML page has three parts

doctype
head (metadata)
body (content)
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Create event</title>
    <script src="script.js"></script>
    <link rel="stylesheet" href="style.css">
  </head>
  <body>
    <h1>Create event</h1>
    <!-- Rest of content here ... -->
  </body>
</html>

Resources to go further

CSS

Cascading Style Sheets

CSS off
CSS on
CSS off
CSS on

CSS can control:

CSS are composed of declarations

Syntax:
property : value;

Examples:
background-color: red;
font-family: sans-serif;
margin: 30px;

Example text

Declarations are grouped in blocks

{
  background-color: red;
  font-family: sans-serif;
  margin: 30px;
}
{
  color: blue;
}

Blocks are applied using selectors

h1 {
  background-color: red;
  font-family: sans-serif;
  margin: 30px;
}

This block styles all h1 elements in the page

selectors + block = rule

Select by tag name
h2 { ... }
p { ... }
div { ... }

Select by class attribute
.check { ... }
span.number { ... }

Select by id
#specialDiv { ... }
section#main { ... }

Select by ancestry
section p { ... }
div p { ... }
div > p { ... }

And many others

CSS box model

Very important for spacing your content properly (more on MDN)

Live example

CSS box model example

First paragraph

Second paragraph

border: 10px solid #fa1;
padding: 10px;
padding-left: 50px;
margin: 15px 5px;

CSS layouts

CSS layouts on MDN

Flexbox froggy

Grid garden

Useful length units

px pixels (for ~96ppi)

em relative to font size of the element

rem relative to font size of the root element

pt points, for fonts

See length on MDN

How to include CSS in a page?

In the <head> part, as a file
<link rel="stylesheet" href="style.css">

Using the <style> tag
<style>h1 { font-weight: bold; }</style>

Inline with the style attribute
<p style="color: yellow;">...</p>

Cascading Style Sheets

Multiple rules can apply for the same element

Priority is given to the more specific,
latest rule (full story)

Elements inherit style from their parents

Cascading example

CSS
html {
  font-size: 20pt;
  color: red;
}
ul {
  font-size: 10pt;
}
li {
  color: green;
}
  
Page
Paste in live example, explain there

Browser rendering differences

The div that looks different in every browser

Why CSS frameworks?

Feasible in CSS, easier with a framework

Bootstrap is the most popular, but others may fit other needs (e.g., Material Design)

Resources on CSS

Design inspiration

FIN

Next lecture: JavaScript & AJAX