1 of 63

Course Overview

CS 396: �Introduction to Web Development

Winter, 2022

2 of 63

Announcements & Roadmap

  1. Lab 2 – will be posted tonight. Due Friday at midnight
  2. Homework 1 posted. Due Tuesday, 1/18, at midnight
  3. Today’s readings / videos – please do them!

3 of 63

Outline

  1. Intro to the Web Browser
  2. Intro to HTML
  3. Intro to the Document Object Model (DOM)
  4. Intro to CSS
  5. CSS Exercises

4 of 63

Outline

  • Intro to the Web Browser
  • Intro to HTML
  • Intro to the Document Object Model (DOM)
  • Intro to CSS
  • CSS Exercises

5 of 63

What Can a Browser Do?

In web dev, browser’s are also referred to as “the client.” They’re the consumer of web resources. They have a few jobs:

  1. Creating (POST), reading (GET), updating (PUT), and deleting (DELETE) resources on servers on the user’s behalf. Examples of web resources include:
    1. text files (HTML, CSS, and JavaScript files)
    2. images
    3. data files
    4. video & audio files
    5. custom fonts

5

6 of 63

What Can a Browser Do?

Browsers have several different jobs...

  • Interpreting instructions (HTML, CSS, and JavaScript files) and rendering (i.e. “drawing”) text, images, and graphics to the screen.
  • Responding to user events via default behaviors or via custom behaviors that are controlled by JavaScript
  • Writing local data (cookies, local storage, password storage, history to your hard drive)

6

7 of 63

What is a Website?

A website as a program – written using HTML, CSS, and JavaScript – that your browser downloads from a server that corresponds to a web address (URL)

Your browser then interprets the program it just downloaded “on-the-fly”

Web Server

Sends / receives static or generated files (HTML, CSS, JavaScript, JSON, Images).

Web Browser�(a) requests instructions �(b) interprets instructions

Browser makes a request to a url

Server sends data and files

8 of 63

Languages your web browser understands

HTML

  • controls the content & structure

CSS (Cascading Style Sheets)

  • controls the style, colors, layout, fonts, etc.

JavaScript

  • controls movement and interactivity
  • can transmit data to and from servers without refreshing the page
  • can interact with local data stores

Also understands select data formats including:

  • PNG, JPG, GIF, JSON, SVG, XML, and some select video, audio, and font formats

8

9 of 63

Let’s take a look at these three types �of files working together�

lecture03/01-simple-carousel

10 of 63

Note in the carousel example:

Links ~ References�Links to scripts & stylesheets are analogous to importing references / dependencies in a ‘traditional’ programming language

HTML ~ Content�Defines # of slides and their content

CSS ~ Style�Styles the slides (yellow, centered, etc.)

JavaScript ~ Interactivity: Sets up timer; responds to rendering dynamic elements on-the-fly)

lecture03/01-simple-carousel

11 of 63

Activity: Tinker with each technology

  1. Modify the HTML content by:
    1. Modifying the text in a slide
    2. Adding an image to a slide
    3. Adding a fifth slide
  2. Modify the CSS by:
    • Changing the color
    • Deleting all of the CSS and seeing what happens
  3. Modify the JavaScript by:
    • Changing the interval time (how long it takes before the slide switches)
    • We’ll be going over DOM manipulation functions later in this lecture.

11

lecture03/01-simple-carousel

12 of 63

Reminder: Languages your web browser does NOT understand

Languages other than HTML, CSS, and JavaScript. This includes:

  • Python, Ruby, Java, C#, etc.
  • TypeScript, CoffeeScript, JSX, and Dart
    • While designed to make JavaScript easier to program, they have to be ‘transpiled’ into JavaScript so that a browser can understand them.
  • SCSS and SASS
    • Ditto: they need to be compiled into CSS.
  • Server-side JavaScript libraries that were designed, specifically to work with Node.js

12

13 of 63

How a browser interprets files

Here are the steps that a browser follows to render an HTML page to the screen:

  1. Pulls down the HTML file
  2. Reads it, scans it for links (“src” and some “href” attributes), and then pulls down linked files
  3. As it pulls down resources, it redraws the screen with the information. The addition of new image, CSS, and JavaScript files usually triggers a screen redraw

13

14 of 63

Using the Browser Inspector

Like with all programming, you will encounter errors as you develop your websites. The Browser Inspector is the very best resource that you have to help you resolve issues. It can help you...

  • Inspect and change elements and CSS properties
  • Examining the files that your browser retrieves
  • Examining requests and responses (communications)
  • Help you identify JavaScript errors

14

15 of 63

Chrome Browser Inspector

  1. Open the 01-simple-carousel/index.html page in your browser
  2. Right-click anywhere on your web page and then select the “Inspect” option
  3. Take a look at the following panels:
    • Elements
    • Console
    • Network

16 of 63

Outline

  • Intro to the Web Browser
  • Intro to HTML
  • Intro to the Document Object Model (DOM)
  • Intro to CSS
  • CSS Exercises

17 of 63

Intro to HTML (Hypertext Markup Language)

HTML is a way of creating web documents using “markup tags”

  • Each HTML tag has a set of rules that you have to follow to correctly use the tag.
  • Sometimes, tags need to be nested in a particular way to be understood by your browser.

18 of 63

How the Browser Interprets HTML

HTML File

<html lang="en"><head><title>DOM Example</title><meta name="author" content="EECS 130"><link rel="stylesheet" href="main.css"></head>��<body><main><h1>Hello!</h1>� <p>Welcome to this webpage.</p>� </main></body></html>

<html>

<title>

<head>

<body>

<link>

<main>

<h1>

<p>

Hello!

Welcome to this webpage.

<meta>

Invisible section

(for metadata)

Visible section

(for document elements)

19 of 63

Lots of elements can go inside of the body element

19

Body

<body>

</body>

Image�<img src="??" />

YouTube Video

<iframe src="??"></iframe>

Hyperlink

<a href=”??”>my link</a>

Containers

<div></div>

<span></span>

<nav></nav>

<article></article>

<header></header>

<section></section>

<footer></footer>

Table

<table>

<tr>

<td>Cell 1</td>

<td>Cell 2</td>

</tr>

<tr>

<td>Cell 3</td>

<td>Cell 4</td>

</tr>

</table>

20 of 63

Important Rules of Thumb

21 of 63

1. Avoid spaces, capital letters, and special characters when naming files

When creating new HTML files, it is important to follow the naming conventions listed below:

  1. No whitespace�Rename page 1.htmlpage_1.html or page1.html
  2. No capitalization; all lowercase�Rename Page1.htmlpage1.html
  3. No special characters (‘,*!^%#). Dashes & underscores are OK�Rename Jenny's Page!.htmljennys_page.html In addition, all HTML files end with either the .htm or .html file extension.

21

22 of 63

2. Most tags have an opening tag and a closing tag

<h1>My Title</h1>

But some don’t:

  • Images: <img src="dog.png" />
  • Line Breaks: <br />
  • Horizontal Rules: <hr />
  • Stylesheet Links: <link rel="stylesheet" href="my_style.css" />

You’ll eventually figure out the rules as you continue building web pages. You can also consult the HTML Reference to learn more about the rules of each individual tag.

22

23 of 63

3. The browser ignores whitespace

The browser ignores whitespace:��<h1>My Title</h1>

...is interpreted the same way as...�

<h1> My

Title

</h1>

23

24 of 63

4. Make your code readable by indenting and using line breaks

Make your code readable by indenting and using line breaks. Please don’t do this:��<main><p>Welcome, <strong>Leonard</strong></p><ol><li>item 1</li><li>item2</li><li>item 3</li></ol></main>

25 of 63

4. Make your code readable by indenting and using line breaks

Instead, do this:��<main>� <p>� Welcome, <strong>Leonard</strong></p>� <ol>� <li>item 1</li><li>item 2</li><li>item 3</li></ol></main>

26 of 63

5. Attribute syntax

Attributes are always followed by an equals sign and values are surrounded by quotation marks.��<img src=“my_image.jpg”>

26

No space between attribute, equals sign, and quotations

27 of 63

6. Last in, first out (LIFO)

Correct��<p>Welcome, � <strong>Leonard</strong></p>�

Incorrect��<p>Welcome, � <strong>Leonard</p></strong>

27

28 of 63

7. Use comments to help you understand your code

<!-- Welcome Section --><section><p>� Welcome, <strong>Leonard</strong></p>� <ol><li>item 1</li>� <li>item 2</li>� </ol>�</section>

29 of 63

8. Know the difference between relative and absolute links

  • Linking is perhaps the biggest idea of the web: documents link together creating a “web” of networked resources.
  • Many different HTML tags use the concept of linking:
    • Stylesheet references
    • Multimedia embedding (e.g. images, videos, audio files)
    • Hyperlinks
  • Links can be absolute: https://i.pinimg.com/originals/ac/f4/9b/acf49bd0f42b441160a9363dce88b243.jpg
  • Or they can be relative (in relation to the location of your html file):�images/my_puppy.jpg �

29

30 of 63

A note on learning HTML

  • If you are new to HTML, please invest the time to learn some of the core HTML ideas and tags (see assigned readings and videos):
  • I’ve created some resources for some core HTML elements you’ll need to know here.
  • Without an understanding of some of the fundamental tags and language rules, UI development will be more difficult for you.

31 of 63

Outline

  • Intro to the Web Browser
  • Intro to HTML
  • Intro to the Document Object Model (DOM)
  • Intro to CSS
  • CSS Exercises

32 of 63

Document Object Model (DOM)

(n.) a way of representing a document, like a web page, in a way that can be understood by a human and by a computer. Typically, a DOM is created using HTML. Declarative languages like CSS are used to style it, and programming languages like Javascript allow us to change it.

Allows you to programmatically manipulate elements and styles on a web page using CSS and JavaScript

Open your console and type: �> document

33 of 63

DOM Representation

HTML

<html lang="en"><head><title>DOM Example</title><meta name="author" content="EECS 130"><link rel="stylesheet" href="main.css"></head>��<body><main><h1>Hello!</h1>� <p>Welcome to this webpage.</p>� </main></body></html>

<html>

<title>

<head>

<body>

<link>

<main>

<h1>

<p>

Hello!

Welcome to this webpage.

<meta>

34 of 63

Outline

  • Intro to the Web Browser
  • Intro to HTML
  • Intro to the Document Object Model (DOM)
  • Intro to CSS
  • CSS Exercises

35 of 63

CSS: How you make your websites look nice!

35

36 of 63

CSS Files: Style & Layout

  • Controls the page style & layout
  • Stylesheets can either be internal (within style tags) or external (in a separate *.css file)
  • You can include as many stylesheets as you want!

37 of 63

How to include a stylesheet in your HTML

External stylesheets are stored in a separate *.css

38 of 63

Anatomy of a Stylesheet

body {

color: black;

padding: 1em;

}

.title-bar {

padding: 5px;

font-weight: bold;

}

#profile {

background-color: #ddd;

}

Stylesheets are comprised �of rulesets

38

39 of 63

Anatomy of a Ruleset

body {

color: black;

padding: 1em;

}

39

selector

property

value

declaration

40 of 63

Specificity and the Cascade

  • The “cascade” refers to the way that CSS styles are applied to HTML elements. Styles applied to elements cascade down to their descendants, unless they are overridden.
  • Specificity is the means by which browsers decide which CSS property values are the most relevant to an element and, therefore, will be applied.
  • You can think of the cascade as a layering of styling rules, in order of specificity.

41 of 63

Specificity and the Cascade

  • More specific style declarations take precedence over more “distant” ones:
  • If one rule is more specific than another one, the more specific rule wins.
  • If two rules share the same specificity, then then the more “recent” rule takes precedence.
  • Note: Some properties are not inherited (because it wouldn’t make sense if they did). A table of properties that are / are not inherited can be found at the W3C specification site.

42 of 63

Selectors allow you to target elements in the DOM

CSS and JavaScript can both target elements within the DOM using different kinds of selectors.

  1. The selector indicates what part of the DOM tree you want to access
  2. Once you have accessed one or more nodes (via a selector), you can modify their style properties using CSS

43 of 63

Types of Selectors: Most Important Ones

43

Selector

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

MEMORIZE THIS SLIDE

44 of 63

Types of Selectors: Also Useful

44

Selector

Example

Description

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

element~element

p ~ ul

Selects every <ul> element that is preceded by a <p> element

45 of 63

...

1 <body>

2 <div class=”title-bar”>

3 <h1>Welcome, Malik</h1>

4 <img id=”profilesrc=images/pic.png” />

5 <hr>

6 </div>

7 <div>Right

8 <ul>

9 <li>list item 1</li>

10 <li>list item 2</li>

11 <li>list item 3</li>

12 </ul>

13 </div>

14 </body>

...

body {

color: grey;

}

h1, li {

text-transform: uppercase;

display: inline-block;

color: #999999;

}

.title-bar {

padding: 5px;

background-color: #EEEEEE;

}

#profile {

width: 100px;

float: left;

margin-right: 20px;

}

46 of 63

...

1 <body>

2 <div class=”title-bar”>

3 <h1>Welcome, Malik</h1>

4 <section>

5 <p>text text text</p>

6 </section>

7 <p>123 456 789</p>

8 </div>

9 <p>blah blah blah</p>

10 <div>Right

11 <p>abc def ghi jkl</p>

12 </div>

13 </body>

...

div p {

color: red;

}

div > p {

color: red;

}

.title-bar p {

color: red;

}

47 of 63

48 of 63

The Box Model

48

References:

  • W3 Schools
  • Lynda.com Video

49 of 63

The Box Model

  1. margin
  2. padding
  3. border
  4. width
  5. box-sizing (border-box v. content box)
  6. display (block, inline-block, inline, grid, none, etc.)

50 of 63

Block-Level Elements

<address> <article> <aside> <blockquote> <canvas> <dd> <div> <dl> <dt> <fieldset> <figcaption> <figure> <footer> <form> <h1>-<h6> <header> <hr> <li> <main> <nav> <noscript> <ol> <p> <pre> <section> <table> <tfoot> <ul> <video>

51 of 63

Inline Elements

<a> <abbr> <acronym> <b> <bdo> <big> <br> <button> <cite> <code> <dfn> <em> <i> <img> <input> <kbd> <label> <map> <object> <output> <q> <samp> <script> <select> <small> <span> <strong> <sub> <sup> <textarea> <time> <tt> <var>

52 of 63

Common CSS Properties and Techniques

53 of 63

CSS & Layouts

Layouts are the hardest thing about CSS for many reasons:

  • The language has many, many different layout ‘paradigms’ for doing the same thing.
  • Specifying the rules for arranging boxes the right way is difficult
  • You have to design for several different browser configurations (what looks good on a desktop doesn’t necessarily look good on mobile).
  • Everything has to be flexible and resizable so that it scales gracefully.

53

54 of 63

Layout Tips

  • Learn Flexbox (HW1) – it’s worth the investment!
  • Also learn CSS Grid – also worth the investment
  • Design your website so that it is responsive (looks good on a mobile, tablet, and desktop). Using Flexbox and CSS Grid will help you with mobile layouts.
  • Avoid older hacks (floats, putting everything inside a table, absolute positioning) – or else use as a last resort.
  • We’ll go through some examples today and on Wednesday.

55 of 63

Flexbox Resources

  • Typically used when you have many child containers that you want to flow within a parent container.
  • Lets you specify a few generic rules that will control how the children of a container are positioned

Some resources:

56 of 63

CSS Grid Resources

CSS Grid allows you to assign containers to specific places on your web page. It is often used to configure the main layout of a page

Some resources:

57 of 63

Outline

  • Intro to the Web Browser
  • Intro to HTML
  • Intro to the Document Object Model (DOM)
  • Intro to CSS
  • CSS Exercises

58 of 63

Lab 1: Box Model

Open 02-box-model and create this card �(from Lab 1):

59 of 63

Using Flexbox: Suggested Strategy

�Parent Container�Put the display into “flex mode” display: flex and then use the following properties to align the child items:

  • align-items: (flex-start, flex-end, space-around, space-between, center)
  • justify-content: (same as align-items)
  • flex-direction: (row, column, row-reverse, column-reverse)
  • flex-wrap: (no-wrap, wrap)

Child Container

Apply sizing to the child container as needed.

60 of 63

Flexbox: Practice 1

Open lecture03/03-flexbox. Can you create the following layouts?

1

2

3

61 of 63

Flexbox: Practice 1 Cont’d – Using Media Queries

Make it so that #1 is for Desktop screens, #2 is for Tablet, and #3 is for Mobile

1

2

3

62 of 63

Flexbox: Practice 2 (Lab 1)

Open 04-flexbox-media-queries and create these screens:

Desktop

Tablet

Mobile

63 of 63

Flexbox: Practice 3: Navigation Bar

Open 05-nav-bar, and try to make the following: