After HTML 4.01 was released, focus shifted to XHTML and its stricter standards.
XHTML 2.0 had even stricter standards than 1.0, rejecting web pages that did not comply. It fell out of favor gradually and was abandoned completely in 2009.
HTML5 is much more tolerant and can handle markup from all the prior versions.
Though HTML5 was published officially in 2012, it has been in development since 2004.
These are just some of the new elements introduced in HTML5.
<!DOCTYPE html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible"/>
<meta name="description" content="goes here..."/>
<meta name="keywords" content="keyword 1, keyword 2"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="robots"/>
<link rel="stylesheet" href="your-single-deployment-stylesheet.css">
<link rel="apple-touch-icon-precomposed" href="apple-touch-icon-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="apple-touch-icon-72x72-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="apple-touch-icon-114x114-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="apple-touch-icon-144x144-precomposed.png">
<!-- JS Links should sit on the bottom of the page! -->
</head>
<body>
... content
<script src="yourJavascript.js"></script>
</body>
</html>
Doctype declaration prior to HTML5 version
In HTML5, there is just one possible DOCTYPE declaration and it is simpler:
The DOCTYPE tells the browser which type and version of document to expect. This should be the last time the DOCTYPE is ever changed. From now on, all future versions of HTML will use this same simplified declaration.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!DOCTYPE html>
Semantic Markup
HTML provides a number of semantic constructs that allow automated tools like search engines and screen readers to make sense of the document and to understand relationships between pieces of content.
HTML Standards and Browser Support
All markup will be written using the latest HTML5 markup specifications from the W3C, as implemented by browsers and devices that meet project requirements.
Doctype
Always include a proper doctype to trigger standards mode. Omitting the doctype triggers quirks mode and should always be avoided.
Optional and Self-closing Tags
While current standards designate certain closing elements and even document level elements as optional, use all open and closing elements nested in the correct ways to ensure maximum compatibility and clarity of document structure
Validation
Valid markup is a goal but not a mandate. However, be aware validation can be an excellent starting place while debugging a Web page — especially if the problems are unusual.
IDs vs. Classes
HTML elements can be identified by using the id and class attributes. An ID is a unique identifier for that particular element; no other element on the page should use the same ID.
Classes are not unique. The same class can be used on multiple elements within a page, and a single element can have more than one class, in a space delimited list.
When your browser renders a web page, it constructs a Document Object Model, a collection of objects that represent the HTML elements on the page. Every element is represented in the DOM by a different object.
In browsers that support HTML5 features, certain objects will have unique properties. A quick peek at the DOM will tell you which features are supported.
[Modernizr] is an open source, MIT-licensed JavaScript library that detects support for many HTML5 & CSS3 features. To use it, include the following <script> element at the top of your page...
Modernizr runs automatically. When it runs, it creates a global object called Modernizr, that contains a set of Boolean properties for each feature it can detect.
if (Modernizr.canvas) {
// let's draw some shapes!
} else {
// no native canvas support available :(
}
Write HTML structure for below layout
The separation of document content from document presentation including aspects such as the layout, colors, and fonts.
<h1 style="font-size: 22px; colour: red; padding: 10px 15px;">
Welcome to the Lancashire Digital website
</h1>
<html>
<head>
<style>
h1 {
font-size: 22px;
color: red;
padding: 10px;
}
</style>
</head>
<body>
<h1>Welcome to the Lancashire Digital website</h1>
</body>
</html>
var pageHeading = document.getElementsByTagName('h1')[0];
pageHeading.style.fontSize = '22px';
pageHeading.style.color = 'red';
pageHeading.style.padding = '10px';
<html>
<head>
<link rel="stylesheet" href="css/main.css">
</head>
<body>
<!-- you style elements in here via CSS -->
</body>
</html>
/* External main.css file */
.main-title {
font-size: 22px;
color: red;
padding: 10px;
}
<!-- HTML body tag -->
<h1 class=“main-title”>Welcome to the Lancashire Digital website</h1>
.my-class-name
Used for multiple elements within the page with shared characteristics.
prefix with " . "
#myID
Use for individual elements within the page.
IDs are unique, you should only have one per page.
prefix with "#"
The general recommendation is to only use classes as these are more flexible.
If you're absolutely sure an element will only ever be a single instance, IDs are fine.
Use lowercase letters and dashes instead of spaces.
Try to use generic names where possible.
Use CamelCase.
Very useful when working as a team. There are differing opinions, the main thing is choose a convention and stick to it.
<!-- HTML example -->
<h2 class="section-title">What we do</h2>
/* CSS example */
.section-title {
color: blue;
padding: 20px 0;
}
<!-- HTML example -->
<h2 id="WhatWeDoTitle">What we do</h2>
/* CSS example */
#WhatWeDoTitle {
color: red;
margin: 15px 0 15px 0;
}
/* inherited from a framework e.g. Bootstrap */
.modal{
position:fixed;
top:0;
right:0;
bottom:0;left:0;
z-index:1050;
display:none;
overflow:hidden;
outline:0;
}
/* LDL specific CSS example */
.modal {
/* would affect all Bootstrap modals */
}
.ldl-modal {
/* we can set our own css properties here
and be sure we do not mess with anything major */
}
Chaining can be thought of as inheritance.
We chain by adding a space between each class name.
<!-- HTML chaining example -->
<div class="box"></div>
<div class="box green-box"></div>
<div class="box red-box round-corners"></div>
<div class="box blue-box round-corners"></div>
HTML | CSS | JS |
---|---|---|
It's a structural building block of my page/app | It's purely style related | It's accessing data or manipulating the DOM in some way |
Use shorthand to quickly set properties
/* CSS long winded example */
.box {
width: 200px;
height: 200px;
background-color: red;
padding-top: 10px;
padding-right: 5px;
padding-bottom: 10px;
padding-left: 0px;
margin-top: 0;
margin-right: auto;
margin-bottom: 0;
margin-left: auto;
border-width: 10px;
border-style: dotted;
border-color: black;
}
/* CSS concise example */
.box {
width: 200px;
height: 200px;
background-color: red;
margin: 0 auto;
padding: 10px 5px 10px 0;
border: 10px solid black;
}
Some properties have a designated order
/* CSS margin/padding order examples */
.box {
/* top/bottom 0, left/right auto */
margin: 0 auto;
/* top 10px, right 5px, bottom 10px, right 0 */
padding: 10px 5px 10px 0;
}
Browsers come with a default stylesheet.
/* CSS to reset body tags default margin/padding */
body {
margin:0;
padding: 0;
}
ID
Class
Attribute
Universal
Pseudo
Selector | Example | Example description |
---|---|---|
. class | .intro | Selects all elements with class="intro" |
# id | #firstname | Selects the element with id="firstname" |
* | * | Selects all elements |
element | p | Selects all <p> elements |
element,element | div, p | Selects all <div> elements and all <p> elements |
element element | div p | Selects all <p> elements inside <div> elements |
element> element | div > p | Selects all <p> elements where the parent is a <div> element |
element+ element | div + p | Selects all <p> elements that are placed immediately after <div> elements |
element1~ element2 | p ~ ul | Selects every <ul> element that are preceded by a <p> element |
[ attribute] | [target] | Selects all elements with a target attribute |
[ attribute= value] | [target=_blank] | Selects all elements with target="_blank" |
[ attribute~= value] | [title~=flower] | Selects all elements with a title attribute containing the word "flower" |
[ attribute|= value] | [lang|=en] | Selects all elements with a lang attribute value starting with "en" |
[ attribute^= value] | a[href^="https"] | Selects every <a> element whose href attribute value begins with "https" |
:active | a:active | Selects the active link |
::after | p::after | Insert something after the content of each <p> element |
::before | p::before | Insert something before the content of each <p> element |
Selector | Example | Example description |
---|---|---|
:hover | a:hover | Selects links on mouse over |
:in-range | input:in-range | Selects input elements with a value within a specified range |
:invalid | input:invalid | Selects all input elements with an invalid value |
:lang(language) | p:lang(it) | Selects every <p> element with a lang attribute equal to "it" (Italian) |
:last-child | p:last-child | Selects every <p> element that is the last child of its parent |
:last-of-type | p:last-of-type | Selects every <p> element that is the last <p> element of its parent |
:link | a:link | Selects all unvisited links |
:not(selector) | :not(p) | Selects every element that is not a <p> element |
:nth-child(n) | p:nth-child(2) | Selects every <p> element that is the second child of its parent |
:nth-last-child(n) | p:nth-last-child(2) | Selects every <p> element that is the second child of its parent, counting from the last child |
:nth-last-of-type(n) | p:nth-last-of-type(2) | Selects every <p> element that is the second <p> element of its parent, counting from the last child |
:nth-of-type(n) | p:nth-of-type(2) | Selects every <p> element that is the second <p> element of its parent |
:only-of-type | p:only-of-type | Selects every <p> element that is the only <p> element of its parent |
:only-child | p:only-child | Selects every <p> element that is the only child of its parent |
:optional | input:optional | Selects input elements with no "required" attribute |
:out-of-range | input:out-of-range | Selects input elements with a value outside a specified range |
:read-only | input:read-only | Selects input elements with the "readonly" attribute specified |
@media queries allow us to adapt the CSS rules dependant
on the device viewing the page
/* from 0 zero up to 500px wide */
@media (min-width: 0px) and (max-width: 500px) {
.block {
width:100%;
font-size: 30px;
height: auto;
}
}
/* from 501px up to 700px do this */
@media (min-width:501px) and (max-width: 700px) {
.block {
width:50%;
font-size: 20px;
}
}
@media can detect a wide-range of features
CSS3 has given us the ability to transform elements
/* CSS 2D transforms */
.block {
transform: rotate(145deg);
transform: skew(145deg);
transform: scale(0.5,0.5);
}
With CSS3 came web fonts
/* bold version of font */
@font-face {
font-family: 'ubuntu-web';
src: url('/assets/fonts/ubuntu-bold-webfont.eot');
src: url('/assets/fonts/ubuntu-bold-webfont.eot?#iefix') format('embedded-opentype'),
url('/assets/fonts/ubuntu-bold-webfont.woff2') format('woff2'),
url('/assets/fonts/ubuntu-bold-webfont.woff') format('woff'),
url('/assets/fonts/ubuntu-bold-webfont.ttf') format('truetype'),
url('/assets/fonts/ubuntu-bold-webfont.svg#ubuntubold') format('svg');
font-weight: 700;
font-style: normal;
}
/* normal version of font */
@font-face {
font-family: 'ubuntu-web';
src: url('/assets/fonts/ubuntu-light-webfont.eot');
src: url('/assets/fonts/ubuntu-light-webfont.eot?#iefix') format('embedded-opentype'),
url('/assets/fonts/ubuntu-light-webfont.woff2') format('woff2'),
url('/assets/fonts/ubuntu-light-webfont.woff') format('woff'),
url('/assets/fonts/ubuntu-light-webfont.ttf') format('truetype'),
url('/assets/fonts/ubuntu-light-webfont.svg#ubuntulight') format('svg');
font-weight: 300;
font-style: normal;
}
/* using a array of fonts we can set our preference, should one font fail to believe */
p, h2 {
font-family: 'ubuntu-web', 'arial', sans-serif;
}
p {
font-weight: 300;
}
Exercise