Handout HTML Css Hamburg Coding School 2022
Handout HTML Css Hamburg Coding School 2022
Handout HTML Css Hamburg Coding School 2022
Outline
● Introduction
○ Three Languages for Web Development
○ Coding Principles
○ IDE - Integrated Development Environment
○ Chrome Developer Tools
● HTML
○ All Websites are Documents
○ What is HTML?
○ HTML Syntax
○ Text Blocks and Headlines
○ Empty Elements
○ Links
○ Nested Blocks
○ Comments
○ HTML Document Structure
○ Head
○ Metadata
○ Viewport (optional knowledge)
○ Text Formatting
○ HTML Entities
○ Images
○ Lists
○ Tables
○ Forms
○ Form Input Groups
○ Other Input Elements
○ Action
○ HTML5 Forms (optional knowledge)
○ Block and Inline Elements
● CSS
○ CSS Syntax
○ Comments
○ Colors
2
○ CSS Selectors
○ The BEM Naming Convention (optional knowledge)
○ Units
○ Including CSS
○ Specificity Rules
○ The CSS Box Model
○ Responsiveness
○ CSS Flexbox (optional knowledge)
○ Bootstrap
● Glossary
● Useful links
3
Introduction
Three Languages for Web Development
Almost all webpages are built using 3 languages:
● HTML for the structure and content of the website,
● CSS for the layout and design,
● JavaScript to animate, work with data and overall behavior of the site.
Coding Principles
● You don’t need to remember every little rule (you can look that up). But you need
to understand how it works.
● When learning to code, make heavy use of search engines like google.
● RTFM - ‘read the frigging manual’ 😉 - Most popular frameworks have excellent
documentation. Use it!
HTML
All Websites are Documents
A website is a document, just like any text file or Word document.
It has the file ending *.html
You can create a new file on your laptop and call it mywebsite.html. If you open it, it
will open in your web browser.
Websites on the internet are not much more. They are HTML documents that are saved
on a server, and can be opened in your browser from there.
What is HTML?
HTML stands for: HyperText Markup Language.
It is a structural language, that means it is used for declaring how the document is
structured and which text is displayed.
It is not a programming language, like for example JavaScript. You cannot write logic
with it, like with if and else.
💡 History: HTML was originally created for displaying academic papers and linking
them to each other.
HTML Syntax
HTML describes the structure of a Web page, i.e. what is a text block, what is a headline,
what is a link, and so on. For that, it uses HTML elements. HTML elements tell the
browser how to display the content.
An HTML element always consists of an opening and a closing tag. A tag is written in
pointy brackets. It looks like this:
<p>This is a paragraph</p>
The <p> tag tells the browser: here starts a text block.
Then there is the text, which the browser will show.
Then comes the closing tag: </p> It tells the browser: the text block ends here.
The enclosed text is called the text content (“This is a paragraph” in this example).
5
In addition to the text content, a tag can also have one or multiple attributes. This will
not be shown in the browser, but is used for additional information like styles.
An attribute has a key and a value, of the form: key=”value”.
We will see more about that later.
Image: http://desarrolloweb.dlsi.ua.es/cursos/2011/html5-css3/html-basics
💡 Good to know: W3Schools has a list of all HTML tags. It is a great site to look up
everything about HTML and CSS: https://www.w3schools.com/tags/default.asp
<p>This is a paragraph</p>
The difference to a <p> block is that it doesn’t have any space before or after.
A headline looks like this:
<h1>This Is A Title</h1>
6
<h1>Largest Headline</h1>
<h2>Second Largest</h2>
<h3>Third Largest</h3>
<h4>Fourth Largest</h4>
<h5>Fifth Largest</h5>
<h6>Smallest Headline</h6>
Empty Elements
There are elements that are empty, meaning they don’t have a closing tag.
An example is the <br> tag, the line break.
The browser will only display line breaks if you put a <br> tag into the text. The tag itself
will not be shown.
💡 Optional Knowledge: In the strict version of HTML we need to close every tag,
including empty ones. This means, a line break should actually look like: <br/>
Links
Some long time ago, links on websites were called hyperlinks. Nowadays we don’t call
them like this anymore, but this is what gave HTML its name: HyperText Markup
Language. Links are probably the most important feature of HTML.
With links, you can create a clickable link in your document that leads to another
document.
7
If you have mywebsite.html, and another page called about.html, you can create a
link in mywebsite.html that leads to about.html like this:
<a href=”about.html”>About</a>
The href attribute specifies the target document where the link should lead to. In this
example, it leads to about.html.
Alternatively, you can also link to an external, public website by specifying the address.
The text in between the <a> tags is the text that the browser will show, the text that will
be clickable. By default, links are displayed blue and underlined.
Nested Blocks
In HTML, tags are usually nested into each other. You might have a div, in which you
have a headline and a text block, in which you have links.
<div>
<h1>My First Website</h1>
<p>
Welcome to my first website. I’m happy that I learned how to
use HTML to write my own websites. Btw, this is
<a href=”https://hamburgcodingschool.com”>where I learned
HTML</a>.
</p>
</div>
Note that we indent each line according to how it is nested, meaning we add whitespace
at the beginning of the line: the deeper the nesting level the more.
💡 Good to know: In HTML, everything is a block. You have blocks of text, blocks of
images, and blocks of blocks nested into each other. Every block is rectangular, just
the sizes and their arrangement is different. You can imagine tags as boxes: you can
put boxes into boxes into boxes...
8
Comments
In HTML, you can write comments. Comments will not be displayed in the browser. They
are only visible in the source code.
Comments are often used by developers to put extra information into the HTML code,
like hints about a block of HTML code. This makes it easier to quickly look over a
document and understand what a code block is for.
Comments are also great for debugging HTML, because you can comment out lines of
code, one at a time, to search for errors:
</div>
</div>
<!-- maybe one closing tag too many?
</div>
-->
💡 Good to know: In most editors you can use a shortcode for that: select the text
you wish to comment out and press ⌘ + / on a Mac or Ctrl + / on a PC.
9
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
...
</body>
</html>
It starts with the document type declaration: <!DOCTYPE html> that explains to the
browser that the document is written in the newest version of HTML: HTML5.
The HTML document itself begins with the opening <html> tag and ends with the
closing </html> tag.
Inside the html tags are the <head> and <body> tags.
The <head> element is a container for metadata about the HTML document. It typically
defines the title of the document, styles, scripts, and other meta information. Metadata
is not displayed.
💡 Good to know: Browsers are very patient, and try to display anything that you
write in the code. Even if you leave out the <html>, <head> and <body> tags, it will
still display your content. But it might not have the desired results.
In general, the rule applies: Be kind to your browser, and your browser will be kind
to you. Meaning: try to write well-formed HTML and stick to the document
structure. Then you minimize the errors or unexpected results that you get from
your browser.
Head
The <head> part of the HTML document contains metadata and other data that is not
part of the document structure, but has some other function.
A typical head looks like this:
10
<head>
<title>Hamburg Coding School</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="last-modified" content='Mon, 06 Jan 2020 16:23:39 CET'>
<meta name="description" content="Programmier-Kurse, die Spaß machen.">
<meta property="og:title" content="Hamburg Coding School"/>
<meta property="og:description" content="Programmier-Kurse, die Spaß machen."/>
<meta property="og:image" content="https://hamburgcodingschool.com/class-201811.jpg"/>
<meta name="twitter:card" content="summary_large_image">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
<link rel="stylesheet" href="https://hamburgcodingschool.com/css/codingschool.css">
<link rel="shortcut icon" href="/favicon.png">
<style>
h1 { color:red; }
p { color:blue; }
</style>
<script>
window.onload = function() {
if (window.matchMedia("(max-width: 48em)").matches) {
location.hash = "#content-start";
}
}
</script>
</head>
<title> Contains the title of the website = the name that is displayed in the
browser tab.
<meta> Includes meta information that is used for e.g. displaying snippets in
search results or link previews in social media.
<link> Includes CSS into the document, or loads the favicon (shortcut icon).
<style> Defines local CSS styles.
<script> Defines local JavaScript.
Metadata
The <meta> tags contain metadata: data that is not displayed on the page, but is
machine parsable. Meta elements typically contain information about
● page description,
● keywords,
● author of the document,
● last modified or
● thumbnail image.
Metadata is mostly used by search engines or by social media to display snippets with
title, description and a thumbnail image.
11
HTML5 introduced a method to let web designers take control over the viewport,
through the <meta> tag. The viewport is the user's visible area of a web page and varies
with the device (mobile phone, tablet, computer screen).
You should include the <meta> viewport element in all your web pages.
The viewport element gives the browser instructions on how to control the page's
dimensions and scaling:
● The width=device-width part sets the width of the page to follow the
screen-width of the device (which will vary depending on the device).
● The initial-scale=1.0 part sets the initial zoom level when the page is first
loaded.
Here is an example of a web page without the viewport meta tag, and the same web
page with the viewport meta tag:
Without the viewport meta tag With the viewport meta tag
Source: https://www.w3schools.com/css/css_rwd_viewport.asp
12
Text Formatting
HTML brings some tags for structuring and formatting your text.
<p> A paragraph. Adds space before and after the text.
<br/> A line break. Single tag without closing tag.
<div> A box. Unlike <p>, <div> doesn’t add space before or after the text.
<h1> A headline. You can choose from <h1> (largest) to <h6> (smallest).
<b> Makes the text bold. Inline tag: does not break the text.
<i> Makes the text italic. Inline tag: does not break the text.
<u> Underlines the text. Inline tag: does not break the text.
<small> Makes the text smaller. Inline tag: does not break the text.
🤓 Want to know more? There are a lot more tags available. A good cheat sheet
for your overview is this one: https://www.simplehtmlguide.com/cheatsheet.php
HTML Entities
Some characters will not render in HTML. A problem is, for example, if you want to use a
> or a < in your text. Since HTML uses these characters for tags, how can we make it see
it as text content instead? For this, we use HTML entities.
The cryptic text > will be rendered as a > character in the text.
Similarly, we have a bunch of other HTML entities. Here is a selection of common ones:
> >
< <
& &
" “
' ‘
€ €
© ©
non-breaking space
13
Images
To include an image in your HTML, use the <img> tag:
The path to the picture is the path relative to your HTML file.
To define height and width, use the height and width attributes:
You can also define a value for the alt attribute. This is a text that is displayed if the
image is broken. It is also useful for screen readers (if blind people read your website),
and for google image search to list your image.
SVGs
SVG images are a little different. They are not a pixel-by-pixel image, like JPG and PNG,
but they describe vectors and attributes of the image – they are a vector image.
Due to this characteristic, they are scalable in size. That means that they will always be
focused, no matter how large you make them.
Lists
You can use HTML to make lists. There are two kinds of lists: unordered lists (with bullet
points), and ordered lists (numbered).
Unordered List
<ul>
<li>HTML</li> ○ HTML
<li>CSS</li> ○ CSS
<li>JavaScript</li> ○ JavaScript
</ul>
Ordered List
<ol>
<li>HTML</li> 1. HTML
<li>CSS</li> 2. CSS
<li>JavaScript</li> 3. JavaScript
</ol>
<ul style="list-style-type:circle;">
<li>HTML</li> ○ HTML
<li>CSS</li> ○ CSS
<li>JavaScript</li> ○ JavaScript
</ul>
This actually uses CSS, this means we are adding a style to the list.
15
circle ○ item
square ■ item
none item
<ol type="A">
<li>HTML</li> A. HTML
<li>CSS</li> B. CSS
<li>JavaScript</li> C. JavaScript
</ol>
type="A" A. First
B. Second
C. Third
type="a" a. First
b. Second
c. Third
type="I" I. First
II. Second
III. Third
type="i" i. First
ii. Second
iii. Third
16
Tables
Tables are an important feature in HTML. You can create a table like this:
<table>
<tr>
<th>Course</th>
<th>Hours</th>
<th>Teacher</th>
</tr>
<tr>
<td>HTML and CSS</td>
<td>27h</td>
<td>Alex Löhn</td>
</tr>
<tr>
<td>Learn to Code</td>
<td>24h</td>
<td>Helder Pereira</td>
</tr>
<tr>
<td>JavaScript for Web</td>
<td>24h</td>
<td>Teresa Holfeld</td>
</tr>
</table>
Forms
If you want to create a website where the user should type in some data, e.g. login
credentials, you need forms.
<form action="/sign-in.php">
First name:<br>
<input type="text" name="firstname" value="Max"><br>
Last name:<br>
<input type="text" name="lastname" value="Mustermann"><br><br>
<input type="submit" value="Submit">
</form>
Radio Buttons
Radio buttons are round bullet buttons where you can select one of many options.
<form>
<input type="radio" name="role" value="student" checked> Student<br>
<input type="radio" name="role" value="teacher"> Teacher<br>
<input type="radio" name="role" value="administrative"> Administrative
</form>
18
Checkboxes
Checkboxes are form inputs that the user can select or deselect independently from
each other. They are useful for multiple-choice options.
<form>
<input type="checkbox" name="course" value="html"> HTML & CSS<br>
<input type="checkbox" name="course" value="l2c"> Learn to Code<br>
<input type="checkbox" name="course" value="js4w"> JavaScript for Web<br>
</form>
Drop-down Selection
Many sign-up forms use a drop-down selection for the salutation, to know if they should
address someone as Mr. or Mrs.
<form>
<select name="salutation">
<option value="mr">Mr.</option>
<option value="mrs">Mrs.</option>
</select>
</form>
19
<form>
<textarea name="post" rows="10" cols="80">
Your blog post
</textarea>
</form>
Button
A button is an alternative to the <input type=”submit”> field.
<form>
...
<button type=”reset”>Reset fields</button>
</form>
The main difference to the <input type=”submit”> field is that you have more type
options:
<button type=”submit”> Submits the form.
Submit is the default option for a <button>, so you
can leave that attribute off.
<button type=”reset”> Resets all form fields.
<button type=”button”> A click button for a different action that you define
yourself (e.g. with JavaScript).
🤓 Want to know more? You can learn about more input types here:
https://www.w3schools.com/html/html_form_input_types.asp
20
Action
Every form should define a form action with the action=”...” attribute. The action
attribute specifies where to send the form-data when a form is submitted.
<form action="/sign-in.php">
In this example, the data that the user typed into the form fields is sent to the
sign-in.php program. It is assumed that the file containing the PHP code exists and is
able to handle the data.
In the course JavaScript for Web you will learn to build your own programs in
JavaScript, which can be used as receiver of form input.
🤓 Optional knowledge: <img> elements are actually not inline, but inline-block.
This is a combination of both. The element behaves like an inline element, but it is
also a block, meaning that you can set both height and width.
From: https://www.w3schools.com/css/tryit.asp?filename=trycss_inline-block_span1
22
CSS
CSS Syntax
CSS stands for Cascading Style Sheets and describes how HTML elements are to be
displayed in the browser.
Image: http://desarrolloweb.dlsi.ua.es/cursos/2011/html5-css3/css-basics
Comments
Just as in HTML comments are used to explain the code and may help when you edit the
source code at a later date and are ignored by browsers but the syntax is a bit different.
A CSS comment starts with /* and ends with */. Comments can span multiple lines:
.logo {
height: 32px;
width: 32px;
/* margin: 20px;
Padding: 5px; */
cursor: pointer;
}
23
Colors
Colors in CSS are defined like this:
.logo {
color: red; /* text color */
background-color: #44F25A; /* background color */
}
Color values can be specified in four ways: as color name, hex color, rgb color, or hsl
color.
Color Names
CSS has some predefined names for colors, e.g. red, blue, orange or tomato.
📚 You can look up the complete list here:
https://www.w3schools.com/colors/colors_names.asp
Hex Colors
Hex colors follow the pattern: #rrggbb (red, green, blue)
The values are hexadecimal values between 00 and FF.
📚 Read more: https://www.w3schools.com/colors/colors_hexadecimal.asp
RGB Colors
Here, colors are defined as: rgb(r, g, b) (red, green, blue)
The values are numbers between 0 and 255.
📚 Read more: https://www.w3schools.com/colors/colors_rgb.asp
HSL Colors
This is a different color space where colors are defined by hue, saturation and lightness.
They follow the pattern: hsl(h, s%, l%) (hue, saturation, lightness)
Hue is a number between 0 and 360 (degrees on the color wheel).
Saturation and lightness are a percentage (between 0 and 100).
📚 Read more: https://www.w3schools.com/colors/colors_hsl.asp
💡 Note: RGB and HSL colors are not used very often, but you will see hex colors all
the time. It is a good idea to study hex codes and know the most common ones.
24
CSS Selectors
In CSS, a selector is the element before the opening bracket { that defines to which
element(s) the style will be applied.
Tags
Tag selectors look like this:
Classes
To select a class, you use the dot (.):
IDs
To select HTML tags with a certain id, use the hash (#):
All Elements
The selector for all elements is the asterisk (*):
Combining Selectors
Instead of defining the same style for multiple selectors, you can separate them by
comma (,).
block__element--modifier
The block is the larger section that you want to display, e.g. a form.
The element is an element of it, e.g. a button.
The modifier specifies a variant of this element, e.g. a highlighted button. It is optional.
In the HTML, this would look like this:
<button class=”form__button--highlighted”>Submit</button>
.form__button--highlighted {
...
}
Units
For specifying a length, CSS provides a selection of units you can choose from.
You usually use them for width, height, margin, padding or font-size.
Length values are written as a number followed by the unit:
27
.wrapper {
font-size: 1em;
width: 200px;
}
Absolute units:
● px pixels
● pt points
● pc picas (1pc = 12 pt)
● cm centimeters
● mm millimeters
● in inches
In web development, you usually use px for height and width, and pt for font-size.
💡 Note: Pixel values (px) are heavily used, but their actual size on the screen
depends on the screen of the device. It is good practice to test your website on
multiple devices and to make the website responsive (see section
“Responsiveness”).
Relative units:
● em Relative to the element’s font size (2em = 2 times the font size)
● rem Relative to the font size of the root element
● vw Relative to 1% of the viewport width
● vh Relative to 1% of the height of the viewport
● vmin Relative to 1% of viewport's smaller dimension
● vmax Relative to 1% of viewport's larger dimension
● % Relative to the parent element
● ex Relative to the x-height of the element’s font (rarely used)
● ch Relative to width of the 0 character (rarely used)
In web development, you will use em, rem, vw and % a lot. The other values you will
rarely encounter.
The viewport is the size of the window in the browser.
💡 Tip: It is good practice to use em and rem a lot. It helps create good responsive
websites. vw is also a useful unit, but has to be tested especially on small devices.
28
Including CSS
There are three ways of inserting a style sheet:
● Inline CSS
● Internal CSS
● External CSS
Inline CSS
CSS is defined in the style attribute of an HTML tag.
Internal CSS
The CSS is defined in the <head> part of the HTML document.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>HTML and CSS Course</title>
<style>
body {
background-color: gold;
}
h1 {
color: #3c3c3c;
}
</style>
</head>
<body>
<h1>HTML and CSS Course</h1>
<p>In this course, we are going to learn ...</p>
...
</body>
</html>
29
External CSS
In external CSS, the styles are defined in its own file and then included via a <link>
element in the <head> of the HTML file.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>HTML and CSS Course</title>
<link rel="stylesheet" type="text/css" href="courses.css">
</head>
<body>
<h1>HTML and CSS Course</h1>
<p>In this course, we are going to learn ...</p>
...
</body>
</html>
Specificity Rules
In CSS, some styles can overwrite others. For this, CSS applies its specificity rules.
.highlighted {
background-color: yellow !important;
}
All HTML elements can be considered as boxes. In CSS, the term box model is used
when talking about design and layout.
The CSS box model is essentially a box that wraps around every HTML element. It
consists of margin, border, padding, and the actual content.
(https://www.w3schools.com/css/css_boxmodel.asp)
31
The box model allows us to add a border around elements, and to define space
between elements.
Responsiveness
A website is responsive if it is suitable for all screen sizes, even for small mobile screens.
For making a website responsive, we can use media queries.
Media queries are styles wrapped in a @media block that defines for which screen size
this style applies.
The style without the media query is the smallest and the default style. The style inside
the media query is the one for the larger screens and will overwrite the default style if
the screen where the website is displayed is larger than specified.
.container {
display: flex;
}
The flex direction specifies if the items will be aligned in a row or as a column (one
below the other). This is specified on the container element as well:
.container {
display: flex;
flex-direction: row;
}
📚 Learn more: You can specify much more for the container and items. A good
cheat sheet with visual aids is this:
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Bootstrap
Bootstrap is a library for CSS, HTML and JavaScript. It provides a lot of functionality, and
predefined CSS styles that make a website look nice.
https://getbootstrap.com/
33
Glossary
HTML HyperText Markup Language
Developer Tools Feature of the Chrome browser to look at HTML, CSS and
other things.
radio button A group of items with round buttons where you can select
only one.
selector In CSS: the item that specifies the tag, class or id to which
the style should be applied. In this example, the .blue class:
.blue {
color: blue;
}
CSS box model The box model is a model that shows where margin,
border, padding and content of an HTML element are
located.
35
media query A media query is a special CSS element that defines for
which screen size a style should be applied.
Of the format:
@media (min-width: 576px) {
...
}
Useful Links
HTML
W3Schools: https://www.w3schools.com/html/
Many of this handout’s examples stem from here. Thank you, w3schools! 🙏
MDN: https://developer.mozilla.org/de/docs/Web/HTML
SelfHTML: https://www.selfhtml.org/
Boilerplates: https://amp.dev/boilerplate/
Overview of the most used HTML elements with short explanations:
https://www.advancedwebranking.com/html/
Placeholder Bilder: http://placekitten.com/, https://www.fillmurray.com
CSS
W3Schools: https://www.w3schools.com/css/
To exercise and test your CSS Knowledge: https://flukeout.github.io/
BEM Naming Convention: http://getbem.com/naming/
Flexbox: http://flexbox.help/
Flexbox Generator: https://loading.io/flexbox/
Learn flexbox by playing: http://flexboxfroggy.com/
The complete guide to flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Bootstrap: https://getbootstrap.com
Browser support: https://caniuse.com/