Your preferred web browser (e.g. Mozilla Firefox)
Your preferred code editor (e.g. VS Code)
est. 1990
Open simple-page.html
in your browser.
Open simple-page.html
in your code editor.
<!DOCTYPE html>
<html>
<head></head>
<body>Hackerschool 2020… The start of Reccess Week!</body>
</html>
<!DOCTYPE html>
<html>
<head></head>
<body>Hackerschool 2020… The start of Reccess Week!</body>
</html>
Declares that this document is a HTML document.
<!DOCTYPE html>
<html>
<head></head>
<body>Hackerschool 2020… The start of Reccess Week!</body>
</html>
<!DOCTYPE html>
<html>
<head></head>
<body>Hackerschool 2020… The start of Reccess Week!</body>
</html>
<!DOCTYPE html>
<html>
<head></head>
<body>Hackerschool 2020… The start of Reccess Week!</body>
</html>
<!DOCTYPE html>
<html>
<head></head>
<body>Hackerschool 2020… The start of Reccess Week!</body>
</html>
<!DOCTYPE html>
<html>
<head></head>
<body>Hackerschool 2020… The start of Reccess Week!</body>
</html>
<head>
<!DOCTYPE html>
<html>
<head></head>
<body>Hackerschool 2020… The start of Reccess Week!</body>
</html>
Contains page metadata, like title, links to stylesheets, etc.
<body>
<!DOCTYPE html>
<html>
<head></head>
<body>Hackerschool 2020… The start of Reccess Week!</body>
</html>
Contains page contents.
<!DOCTYPE html>
<html>
<head></head>
<body>Hackerschool 2020… The start of Reccess Week!</mark></body>
</html>
<!DOCTYPE html>
<html>
<head></head>
<body>Hackerschool 2020… The start of Reccess Week!</mark> Preparing to lift off!</body>
</html>
String that begins with & and ends with ; to display special characters.
Let's try out more HTML elements.
Follow by modifying simple-page.html, or by adding the elements via the document inspector.
Open by F12 or Ctrl+Shift+C (Cmd+Shift+C on Mac).
Creates new node in the selected element.
Does not exist in Chrome; right-click or press F2 to edit-as-HTML instead.
New node created.
Double click anything to edit it. Enter to commit.
<div>
element<div>Some content</div>
Along with <span>
, two of the most generic tags for content.
Simply helps to divide up your content.
Many elements have some special meaning to them, even though they have no special default styling. E.g.
<p>
is a paragraph<main>
contains the "main" content of the page<article>
contains an "article" e.g. forum post, news article, etc<header>
, <footer>
should contain the header and footerSee MDN's guide.
Why use Semantic Elements?
<title>
element<title>Page title</title>
Goes into <head>
.
It is the title shown in the tab title, and in search engines, etc.
<h1>
to <h6>
; MDN
<h1>This is a H1.</h1>
<h2>This is a H2.</h2>
<h3>This is a H3.</h3>
<h4>This is a H4.</h4>
<h5>This is a H5.</h5>
<h6>This is a H6.</h6>
<p>
; MDN
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
(Never use <br>
to break paragraphs!)
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
<ol>
<li>Item 1</li>
<li>Item 2</li>
</ol>
<a>
; MDN
<a href="https://google.com">Google</a>
Results in: Google
<a href="https://google.com">Google</a>
<a href="https://google.com">Google</a>
<a href="https://google.com">Google</a>
<img>
; MDN
<img src="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png">
Results in:
Note: <img>
is a void element.
It does not need to be closed with </img>
(this is invalid), or self-closed like <img ... />
(this is redundant but acceptable).
Many elements can contain other elements, and combining them usually leads to what you'd expect.
How to make a clickable image?
<a href="https://google.com"><img src="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png"></a>
Recreate this page. Note: Your font may be different. That's alright! We'll fix it soon™
est. 1994
style
<p style="color: blue;">Blue text!</p>
Results in:
Blue text!
<p style="color: blue;">Blue text!</p>
<p style="color: blue;">Blue text!</p>
There are many properties. See the MDN list.
Some common properties are:
Try them out!
What if we have many elements?
<p style="color: blue;">Blue text!</p>
<p style="color: blue;">Blue text!</p>
<p style="color: blue;">Blue text!</p>
<p style="color: blue;">Blue text!</p>
<p style="color: blue;">Blue text!</p>
<p style="color: blue;">Blue text!</p>
<p style="color: blue;">Blue text!</p>
<p style="color: blue;">Blue text!</p>
<style>
p {
color: blue;
}
</style>
<p>Blue text!</p>
<p>Blue text!</p>
<p>Blue text!</p>
<p>Blue text!</p>
<p>Blue text!</p>
<p>Blue text!</p>
<p>Blue text!</p>
<p>Blue text!</p>
<style>
p {
color: blue;
}
</style>
<style>
p {
color: blue;
}
</style>
<style>
p {
color: blue;
}
</style>
id
, class
<style>
#my-p {
/* some declarations */
}
.red {
color: red;
}
.em {
font-weight: bold;
}
</style>
<p id="my-p" class="red em">Red bold paragraph</p>
tag
: selects elements of tag tag
#element-id
: selects element with ID element-id
.class
: selects elements of class class
*
: selects all elements[attr]
: selects elements based on attr
(MDN)tag.class
: selects tag
elements of class class
.class1.class2
: selects elements of class class1
AND class2
.class1, .class2
: selects elements of class class1
OR class2
CSS is often put in a separate file from the HTML, and it is simply linked in.
In <head>
:
<link rel="stylesheet" type="text/css" href="theme.css">
Style exercise 1 like so, using an external stylesheet (no inline styles!).
The font is Montserrat from Google Fonts. Feel free to explore around!
X Y
: match Y that is a descendant of XX > Y
: match Y that is a direct child of XX ~ Y
: match Y that is after XX + Y
: match Y that is directly after X<div>
and <span>
<div>
is a generic block-level (by default) element.
<span>
is a generic inline (by default) element.
They have no separate semantic meaning; they're just used to apply CSS on. ID and classes come in useful here.
Special classes that allow you to select elements in a certain state. Commonly used:
:hover
: elements being hovered over:focus
: focussed element:nth(-last)-of-type()
: nth (last) element of the same type among siblings (1-based):nth(-last)-child()
: nth (last) element among siblings (1-based):first/last/only-of-type
: first/last/only of the same type among siblings:first/last/only-child
: first/last/only among siblings:not(selector)
: elements which do not match selectornth
syntaxThe nth/nth-last-child/of-type
selectors accept an argument in parentheses, which can be:
odd
: odd numbered elementseven
: even numbered elementsAn+B
: where A, B are integers, elements whose order matches the pattern. You can just specify either An
or B
only as well.The nth-of-type
selector is commonly used to give tables alternating colours.
<style>
table tr:nth-of-type(odd):not(:first-of-type) {
background-color: #aaa;
}
</style>
Modify the table demo so the colours invert when you hover over the table.
No JavaScript needed!
Hint: :not
, :hover
When a property is not specified, the element either inherits it from its parent, or it gets a default value.
E.g. inherited properties: color
, font-*
, etc
E.g. non-inherited properties: border-*
, background-*
, etc
When specifying lengths in CSS (width
, height
, margin
, padding
), you will have to specify them with units.
Common units:
There are many ways to lay out a page with CSS.
<style>
#grid {
display: grid;
grid-template: 1fr 1fr 1fr / 1fr 1fr 1fr;
}
</style>
<div id="grid">
<div>1</div>
...
<div>9</div>
</div>
<style>
#grid {
display: grid;
grid-template: 1fr 1fr 1fr / 1fr 1fr 1fr;
}
</style>
<div id="grid">
<div>1</div>
...
<div>9</div>
</div>
<style>
#grid {
display: grid;
grid-template: 1fr 1fr 1fr / 1fr 1fr 1fr;
}
</style>
<div id="grid">
<div>1</div>
...
<div>9</div>
</div>
<style>
.box1 {
grid-column: 1;
grid-row: 1 / 4;
}
.box2 {
grid-column: 3;
grid-row: 1 / 3;
}
...
</style>
<div id="grid">
<div class="box1">1</div>
<div class="box2">2</div>
...
</div>
<style>
.box1 {
grid-column: 1;
grid-row: 1 / 4;
}
.box2 {
grid-column: 3;
grid-row: 1 / 3;
}
...
</style>
<div id="grid">
<div class="box1">1</div>
<div class="box2">2</div>
...
</div>
<style>
.box1 {
grid-column: 1;
grid-row: 1 / 4;
}
.box2 {
grid-column: 3;
grid-row: 1 / 3;
}
...
</style>
<div id="grid">
<div class="box1">1</div>
<div class="box2">2</div>
...
</div>
Recreate this.
Check out the React workshop to learn React!