Lecture 0 Notes
Lecture 0 Notes
Lecture 0
Introduction
Web Programming
HTML (Hypertext Markup Language)
o Document Object Model (DOM)
o More HTML Elements
o Forms
CSS (Cascading Style Sheets)
Responsive Design
Bootstrap
Sass (Syntactically Awesome Style Sheets)
Introduction
In this course, we’re picking up where CS50 left off and diving into the
design and creation of web applications. We’ll build our web-design
skills by working on a number of projects throughout the course,
including an open-ended final project where you’ll have the chance to
create a website of your own!
In this course, you’ll need a text editor where you can write code
locally on your computer. Some popular ones include Visual Studios
Code, Sublime Text, Atom, and Vim, but there are many more to
choose from!
Web Programming
Course Topics: We’ll go into more detail later, but here’s a brief
overview of what we’ll be working on during this course:
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML Elements</title>
</head>
<body>
<!-- We can create headings using h1 through h6 as
tags. -->
<h1>A Large Heading</h1>
<h2>A Smaller Heading</h2>
<h6>The Smallest Heading</h6>
<!-- The strong and i tags give us bold and italics
respectively. -->
A <strong>bold</strong> word and an <i>italicized</i>
word!
</div>
<input type="submit">
</form>
</body>
</html>
CSS (Cascading Style Sheets)
CSS is used to customize the appearance of a website.
While we’re just getting, started, we can add a style attribute to
any HTML element in order to apply some CSS to it.
We change style by altering the CSS properties of an element,
writing something like color: blue or text-align: center
In this example below, we make a slight change to our very first
file to give it a colorful heading:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello!</title>
</head>
<body>
<h1 style="color: blue; text-align: center;">A Colorful
Heading!</h1>
Hello, world!
</body>
<html>
If we style an outer element, all of the inner elements
automatically take on that style. We can see this if we move the styling
we just applied from the header tag to the body tag:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello!</title>
</head>
<body style="color: blue; text-align: center;">
<h1 >A Colorful Heading!</h1>
Hello, world!
</body>
<html>
While we can style our web page as we’ve done above, to
achieve better design, we should be able to move our styling away
from the individual lines.
o One way of doing this is to add your styling
between <style> tags in the head. Inside these tags, we write which
types of elements we want to be style, and the styling we wish to apply
to them. For example:
<html lang="en">
<!DOCTYPE html>
<head>
<title>Hello!</title>
<style>
h1 {
color: blue;
text-align: center;
}
</style>
</head>
<body>
<h1 >A Colorful Heading!</h1>
Hello, world!
</body>
</html>
o Another way is to include in a <link> element in
your head with a link to a styles.css file that contains some styling. This
means the HTML file would look like:
<html lang="en">
<!DOCTYPE html>
<head>
<title>Hello!</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1 >A Colorful Heading!</h1>
Hello, world!
</body>
</html>
And our file called styles.css would look like:
h1 {
color: blue;
text-align: center;
}
There are far too many CSS properties to go over here, but just
like HTML elements, it’s typically easy to Google something along the
lines of “change font to blue CSS” to get the result. Some of the most
common ones though are:
o color: the color of text
o text-align: where elements are placed on the page
o background-color: can be set to any color
o width: in pixels or percent of a page
o height: in pixels or percent of a page
o padding: how much space should be left inside an element
o margin: how much space should be left outside an element
o font-family: type of font for text on page
o font-size: in pixels
o border: size type (solid, dashed, etc) color
Let’s use some of what we just learned to improve upon our
oceans table from above. Here’s some HTML to start us off:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Nicer Table</title>
</head>
<body>
<table>
<thead>
<th>Ocean</th>
<th>Average Depth</th>
<th>Maximum Depth</th>
</thead>
<tbody>
<tr>
<td>Pacific</td>
<td>4280 m</td>
<td>10911 m</td>
</tr>
<tr>
<td>Atlantic</td>
<td>3646 m</td>
<td>8486 m</td>
</tr>
</tbody>
</table>
</body>
<html>
The above looks a lot like what we had before, but now, either by
including a style tag or a link to a stylesheet in the head element, we
add the following css:
table {
border: 1px solid black;
border-collapse: collapse;
}
td {
border: 1px solid black;
padding: 2px;
}
th {
border: 1px solid black;
padding: 2px;
}
Which leaves us with this nicer-looking table:
You may already be thinking that there’s some needless
repetition in our CSS at the moment, as td and th have the same
styling. We can (and should) condense this down to the following code,
using a comma to show the styling should apply to more than one
element type.
table {
border: 1px solid black;
border-collapse: collapse;
}
td, th {
border: 1px solid black;
padding: 2px;
}
This is a good introduction into what are known as CSS selectors.
There are many ways to determine which HTML elements you are
styling, some of which we’ll mention here:
o element type: this is what we’ve been doing so far:
styling all elements of the same type.
o id: Another option is to give our HTML elements an id like
so: <h1 id="first-header">Hello!</h1> and then applying styling
using #first-header{...} using the hashtag to show that we’re
searching by id. Importantly, no two elements can have the same id,
and no element can have more than one id.
o class: This is similar to id, but a class can be shared by
more than one element, and a single element can have more than one
class. We add classes to an HTML element like this: <h1 class="page-
text muted">Hello!</h1> (note that we just added two classes to the
element: page-text and muted). We then style based on class using a
period instead of a hashtag: .muted {...}
Now, we also have to deal with the problem of potentially
conflicting CSS. What happens when a header should be red based on
its class but blue based on its id? CSS has a specificity order that goes:
o In-line styling
o id
o class
o element type
In addition to the comma for multiple selectors, there are several
other ways to specify which elements you would like to style. This table
from lecture provides a few, and we’ll go through a few examples
below:
</body>
<html>
</body>
<html>
Not only can we use CSS to change what an element looks like
permanently, but also what it looks like under certain conditions. For
example, what if we wanted a button to change color when we hover
over it? We can acheive this using a CSS pseudoclass, which provides
additional styling during special circumstances. We write this by adding
a colon after our selector, and then adding the circumstance after that
colon.
In the case of the button, we would add :hover to the button
selector to specify the design only when hovering:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Pseudoclasses</title>
<style>
button {
background-color: red;
width: 200px;
height: 50px;
font-size: 24px;
}
button:hover {
background-color: green;
}
</style>
</head>
<body>
<button>Button 1</button>
<button>Button 2</button>
<button>Button 3</button>
</body>
<html>
Responsive Design
Today, many people view websites on devices other than
computers, such as smartphones and tablets. It’s important to make
sure your website is readable to people on all devices.
One way we can achieve this is through knowledge of
the viewport. The viewport is the part of the screen that is actually
visible to the user at any given time. By default, many webpages
assume that the viewport is the same on any device, which is what
leads to many sites (especially older ones) being difficult to interact
with on mobile devices.
One simple way to improve the appearance of a site on a mobile
device is to add the following line in the head of our HTML files. This
line tells the mobile device to use a viewport that is the same width as
that of the device you’re using rather than a much larger one.
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Another way we can deal with different devices is through media
queries. Media queries are ways of changing the style of a page based
on how the page is being viewed.
For an example of a media query, let’s try to simply change the
color of the screen when it shrinks down to a certain size. We signal a
media query by typing @media followed by the type of query in
parentheses:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Screen Size</title>
<style>
@media (min-width: 600px) {
body {
background-color: red;
}
}
Another way to deal with differing screen size is using a new CSS
attribute known as a flexbox. This allows us to easily have elements
wrap around to the next line if they don’t fit horizontally. We do this by
putting all of our elements in a div that we’ll call our container. We
then add some styling to that div specifying that we want to use a
flexbox display for the elements inside of it. We’ve also added some
additional styling to the inner divs to better illustrate the wrapping
that’s occuring here.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Screen Size</title>
<style>
#container {
display: flex;
flex-wrap: wrap;
}
.grid-item {
background-color: white;
font-size: 20px;
padding: 20px;
text-align: center;
}
</style>
</head>
<body>
<div class="grid">
<div class="grid-item">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
<div class="grid-item">4</div>
<div class="grid-item">5</div>
<div class="grid-item">6</div>
<div class="grid-item">7</div>
<div class="grid-item">8</div>
<div class="grid-item">9</div>
<div class="grid-item">10</div>
<div class="grid-item">11</div>
<div class="grid-item">12</div>
</div>
</body>
</html>
Bootstrap
It turns out that there are many libraries that other people have
already written that can make the styling of a webpage even simpler.
One popular library that we’ll use throughout the course is known
as bootstrap.
We can include bootstrap in our code by adding a single line to
the head of our HTML file:
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.
min.css" integrity="sha384-
9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk"
crossorigin="anonymous">
Next, we can look at some of bootstrap’s features by navigating
to the documentation portion of their website. On this page, you’ll find
many examples of classes you can add to elements that allow them to
be styled with bootstrap.
One popular bootstrap feature is their grid system. Bootstrap
automatically splits a page into 12 columns, and we can decide how
many columns an element takes up by adding the class col-
x where x is a number between 1 and 12. For example, in the following
page, we have a row of columns of equal width, and then a row where
the center column is larger:
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Web Page!</title>
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.
min.css"
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9Mu
hOf23Q9Ifjh" crossorigin="anonymous">
<style>
.row > div {
padding: 20px;
background-color: teal;
border: 2px solid black;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-4">
This is a section.
</div>
<div class="col-4">
This is another section.
</div>
<div class="col-4">
This is a third section.
</div>
</div>
</div>
<br/>
<div class="container">
<div class="row">
<div class="col-3">
This is a section.
</div>
<div class="col-6">
This is another section.
</div>
<div class="col-3">
This is a third section.
</div>
</div>
</div>
</body>
</html>
ul {
font-size: 14px;
color: $color;
}
ol {
font-size: 18px;
color: $color;
}
Now, in order to link this styling to our HTML file, we can’t just
link to the .scss file because most web browsers only
recognize .css files. To deal with this problem, we have to download a
program called Sass onto our computers. Then, in our terminal, we
write sass variables.scss:variables.css This command will compile
a .scss file named variables.scss into a .css file named variables.css,
to which you can add a link in your HTML page.
To speed up this process, we can use the command sass --watch
variables.scss:variables.css which automatically changes the .css file
every time a change is detected in the .scss file.
While using Sass, we can also physically nest our styling rather
than use the CSS selectors we talked about earlier. For example, if we
want to apply some styling only to paragraphs and unordered lists
within a div, we can write the following:
div {
font-size: 18px;
p {
color: blue;
}
ul {
color: green;
}
}
Once compiled into CSS, we would get a file that looks like:
div {
font-size: 18px;
}
div p {
color: blue;
}
div ul {
color: green;
}
One more feature that Sass gives us is known as inheritance.
This allows us to create a basic set of styling that can be shared by
several different elements. We do this by adding a % before a name of a
class, adding some styling, and then later adding the line @extend
%classname to the beginning of some styling. For example, the following
code applies the styling within the message class to each of the different
classes below, resulting in a webpage that looks like the one below.
%message {
font-family: sans-serif;
font-size: 18px;
font-weight: bold;
border: 1px solid black;
padding: 20px;
margin: 20px;
}
.success {
@extend %message;
background-color: green;
}
.warning {
@extend %message;
background-color: orange;
}
.error {
@extend %message;
background-color: red;
}