1 of 67

CS193X:�Web Programming

Fundamentals

Spring 2017

Victoria Kirst

(vrk@stanford.edu)

2 of 67

Today's schedule

Announcements:

  • Office Hours now posted

Schedule:

  • HTML and CSS
  • Inline vs block
  • Classes and Ids

3 of 67

HW0 Reminders

HW0 still due this Friday!

A few tips:

  • Please don't make your repository public
    • If you do, I will just make it private again
  • Don't forget to submit your homework via the Google Form linked at the bottom of the HW0 spec
  • You can update your HW0 GitHub repository/published page without submitting the Google Form again ☺️ (but multiple submissions are OK)

4 of 67

Waitlist??

  • If you have an access code and have not enrolled: Please do so ASAP�
  • If you do not have an access code yet:�Please email me!

5 of 67

Suggestion: Bring your laptop!

  • Bring your laptop to lecture so you can follow along with the lecture slides and check out the live examples
  • I will be using CodePen in lecture, which lets you livestream the code I write, which might be hard to see on the projector screen�

(But, y'know, don't look ahead for the answers to lecture questions and then pretend like you knew them all along.)

6 of 67

HTML and CSS

7 of 67

Quick review

8 of 67

Recall: HTML

HTML (Hypertext Markup Language)

  • Describes the content and structure of a web page; not a programming language.
  • Made up of building blocks called elements.

<p>

HTML is <em>awesome!!!</em>

<img src="puppy.png" />

</p>

9 of 67

Some HTML elements

Top-level heading: h1, h2, ... h6

Paragraph: p

Line break: br

10 of 67

Some HTML elements

Image: img

Link: a (note: not link)

Strong (bold): strong (note: don't use b)

Emphasis (italic): em (note: don't use i)

11 of 67

Recall: Course web page

We wrote some HTML to make the following page:

12 of 67

That was weird

  • We saw that HTML whitespace collapses into one space…

  • Except weirdly the <h1> heading was on a line of its own, and <strong> was not.

Hmmm… strange…

Oh well, it works! Let's move on!!!

<h1>CS 193X: Web Fun</h1>

<strong>Announcements</strong><br/>

4/3: Homework 0 is out!<br/>

13 of 67

CSS

14 of 67

Recall: CSS

CSS: Cascading Style Sheets

  • Describes the appearance and layout of a web page
  • Composed of CSS rules, which define sets of styles

selector {

property: value;

}

15 of 67

Some CSS properties

Font face: font-family

Font color: color

Note that color always refers to font color, and there's no way to make it mean anything other than font color.

Background color: background-color

Assign a background-color to body to make the page a different color.

16 of 67

Some CSS properties

Border: border (border shorthand syntax)

Text alignment: text-align (note: don't use <center>)

17 of 67

140 predefined names (list)color: black;

Hex values�color: #00ff00;�color: #0f0;color: #00ff0080;

rgb() and rgba()�color: rgb(34, 12, 64);�color: rgba(0, 0, 0, 0.5);�

  • The "a" in rgba stands for alpha channel and is a transparency value�
  • Prefer more descriptive:
  • Predefined name
  • rgb / rgba
  • Hex

18 of 67

Exercise: Course web page

Let's write some CSS to style our page:

19 of 67

Exercise: Course web page

Let's write some CSS to style our page:

Font face: Helvetica

Border: hotpink 3px

Background color: lavenderblush

Highlight: yellow

- Box is centered

- Header and link are centered

- Box contents are left-aligned

20 of 67

Solution?!

body {

font-family: Helvetica;

}

h1 {

text-align: center;

}

a {

text-align: center;

}

p {

border: 3px solid hotpink;

background-color: lavenderblush;

}

Produces:

21 of 67

CSS exercise debrief

We used some key techniques:

  • Add invisible containers in HTML to select groups of elements in CSS.
  • Apply styles to parent / ancestor element to style parent and all its children. (Will talk more about this later.)

22 of 67

CSS exercise debrief

But we encountered more weirdness...

  • text-align: center; didn't work on the <a> tag
  • The box was reaaaaaally wide!
  • How to center the box?!
  • How do you highlight?!

How do we get from this…

… to this?

23 of 67

Q: Why is HTML/CSS so bizarre??

24 of 67

A: There is one crucial set of rules we haven't learned yet…

block vs inline display

25 of 67

What is HTML?

HTML (Hypertext Markup Language)

  • Describes the content and structure of a web page
  • Made up of building blocks called elements.

And there are 3 basic types.

<p>

HTML is <em>awesome!!!</em>

<img src="puppy.png" />

</p>

26 of 67

Types of HTML elements

Each HTML element is categorized by the HTML spec into one of three-ish categories:

  1. block: large blocks of content, has height and width�<p><h1>, <blockquote>, <ol>, <ul>, <table>
  2. inline: small amount of content, no height or width�<a>, <em>, <strong>,<br>
    1. inline block: inline content with height and width�<img>
  3. metadata: information about the page, usually not visible�<title>, <meta>

27 of 67

Block elements

Examples: <p>, <h1>, <blockquote>, <ol>, <ul>, <table>�

  • Take up the full width of the page (flows top to bottom)
  • Have a height and width
  • Can have block or inline elements as children

28 of 67

Example: Block

<h1>About vrk</h1>

<p>

She likes <em>puppies</em>

</p>

29 of 67

Q: What does this look like in the browser?

<h1>About vrk</h1>

<p>

She likes <em>puppies</em>

</p>

h1 {

border: 5px solid red;

}

30 of 67

31 of 67

Block-level: �extends the full width of the page

<h1> is block-level, so it extends the full width of the page by default

Note how block-level elements (h1, p) flow top to bottom

See: Codepen

<h1>About vrk</h1>

<p>

She likes <em>puppies</em>

</p>

h1 {

border: 5px solid red;

}

32 of 67

Q: What does this look like in the browser?

<h1>About vrk</h1>

<p>

She likes <em>puppies</em>

</p>

h1 {

border: 5px solid red;

width: 50%;

}

33 of 67

34 of 67

Block-level�width can be modified

<h1> is block-level, so its width can be modified

Block-level elements still flow top to bottom

See: Codepen

<h1>About vrk</h1>

<p>

She likes <em>puppies</em>

</p>

h1 {

border: 5px solid red;

width: 50%;

}

35 of 67

Inline elements

Examples: <a>, <em>, <strong>, <br>

  • Take up only as much width as needed (flows left to right)
  • Cannot have height and width
  • Cannot have a block element child
  • Cannot be positioned (i.e. CSS properties like float and position do not apply to inline elements)
    • Must position its containing block element instead

36 of 67

Example: Inline

<strong>Web programming resources:</strong>

<a href="http://cs193x.stanford.edu">CS 193X</a>

<a href="https://developer.mozilla.org/en-US/">MDN</a>

<a href="http://google.com">Google</a>

37 of 67

Q: What does this look like in the browser?

<strong>Web programming resources:</strong>

<a href="http://cs193x.stanford.edu">CS 193X</a>

<a href="https://developer.mozilla.org/en-US/">MDN</a>

<a href="http://google.com">Google</a>

strong {

border: 5px solid red;

width: 1000px;

}

38 of 67

39 of 67

Inline elements ignore widthwidth cannot be modified

Cannot set width on inline element, so it is ignored (Codepen)

strong {

border: 5px solid red;

width: 1000px;

/* Will not work; strong is

inline! */

}

<strong>Web programming resources:</strong>

<a href="http://cs193x.stanford.edu">CS 193X</a>

<a href="https://developer.mozilla.org/en-US/">MDN</a>

<a href="http://google.com">Google</a>

40 of 67

inline-block

Examples: <img>, any element with display: inline-block;

  • Width is the size of the content, i.e. it takes only as much space as needed (flows left to right)
  • Can have height and width
  • Can have a block element as a child
  • Can be positioned (i.e. CSS properties like float and position apply)

41 of 67

Q: What does this look like in the browser?

Example: Inline-block

img {

width: 50px;

}

<img src="http://i.imgur.com/WJToVGv.jpg" />

<img src="http://i.imgur.com/WJToVGv.jpg" />

<img src="http://i.imgur.com/WJToVGv.jpg" />

<img src="http://i.imgur.com/WJToVGv.jpg" />

<img src="http://i.imgur.com/WJToVGv.jpg" />

42 of 67

43 of 67

Inline-block�Has width and height; flows left to right

Can set width on inline-block element, so image width is set to 50px. (Codepen)

inline-block flows left to right, so images are right next to each other.

img {

width: 50px;

}

<img src="http://i.imgur.com/WJToVGv.jpg" />

<img src="http://i.imgur.com/WJToVGv.jpg" />

<img src="http://i.imgur.com/WJToVGv.jpg" />

<img src="http://i.imgur.com/WJToVGv.jpg" />

<img src="http://i.imgur.com/WJToVGv.jpg" />

44 of 67

The display CSS property

You can change an element's default rendering type by changing the display property. Examples:

Possible values for display:

  • block
  • inline
  • inline-block
  • some others: link

p {

display: inline;

}

a {

display: block;

}

45 of 67

Review

  1. block: flows top-to-bottom; has height and width<p>, <h1>, <blockquote>, <ol>, <ul>, <table>
  2. inline: flows left-to-right; does not have height and width<a>, <em>, <strong>,<br>
    1. inline block: flows left-to-right; has height and width equal to size of the content<img>

Questions?

46 of 67

Moral of the story:

If your CSS isn't working, see if you're trying to apply block-level properties to inline elements

47 of 67

h1 vs strong mystery

Recall: Weirdly the <h1> heading was on a line of its own, and <strong> was not. -- Why?

48 of 67

h1 vs strong demystified!

Recall: Weirdly the <h1> heading was on a line of its own, and <strong> was not. -- Why?

Because h1 is a block-level element,�and strong is an inline-level element

49 of 67

text-align mystery

Recall: We couldn't set text-align: center; on the <a> tag directly, but we could center <h1>. Why?�

Let's try looking at the MDN description of text-align...

50 of 67

text-align mystery

51 of 67

text-align demystified!

Why? From the spec, can't apply text-align to an inline element; must apply text-align to its block container, or set a { display : block; }

52 of 67

Box size mystery

Recall: The pink box we put around the announcements extended the entirety of the page.

Why?

How do we fix this?

53 of 67

Box size mystery

Recall: The pink box we put around the announcements extended the entirety of the page.

Why? Because p is block-level, so width == width of the page

How do we fix this?

54 of 67

Box size mystery: demystified!

Recall: The pink box we put around the announcements extended the entirety of the page.

Why? Because p is block-level, so width == width of the page

How do we fix this? Change display to inline-block (though now the space above the box has increased… will address later!)

55 of 67

Centering the box

We can also center the box by centering the body tag, since p is now inline-block.

56 of 67

Highlight mystery

Recall: We didn't know how to select a random snippet of text to change its background.

How do we fix this?

57 of 67

Highlight: demystified!

We can select a random segment of text by wrapping it in an inline element:

Hmmm… but wouldn't it be better to have a "highlight" element?

58 of 67

Highlight: demystified!

We can select a random segment of text by wrapping it in an inline element:

Hmmm… but wouldn't it be better to have a "highlight" element?

How do we make a generic HTML element?

59 of 67

Have you heard of <div> and <span>?

What are they?

60 of 67

<div> and <span>

Two generic tags with no intended purpose or style:

  • <div>: a generic block element
  • <span>: a generic inline element

61 of 67

<span> in action

We can use <span> as a generic inline HTML container:

62 of 67

Multiple generic containers?

But won't we often want multiple generic containers?

How do we distinguish two generic containers?

In other words, how do we select a subset of elements instead of all elements on the page?

63 of 67

CSS Selectors:

Classes and Ids

64 of 67

Classes and ids

There are 3 basic types of CSS selectors:

Element selector

(this is the one we've been using)

p

All <p> elements

ID selector

#abc

element with id="abc"

Class selector

.abc

elements with class="abc"

<h1 id="title">Homework</h1>

<em class="hw">HW0</em> is due Friday.<br/>

<em class="hw">HW1</em> goes out Monday.<br/>

<em>All homework due at 11:59pm.</em>

65 of 67

Classes and ids

<h1 id="title">Homework</h1>

<em class="hw">HW0</em> is due Friday.<br/>

<em class="hw">HW1</em> goes out Monday.<br/>

<em>All homework due at 11:59pm.</em>

.hw {

color: hotpink;

}

#title {

color: purple;

}

66 of 67

More on class and id

  • class and id are special HTML attributes that can be used on any HTML element
    • class: Used on 1 or more elements; identifies a collection of elements
    • id: Used on exactly 1 element per page; identifies one unique element
  • Can apply multiple classes by space-separating them:�<span class="hw new">HW1</span>
  • Often used with span and div to create generic elements: e.g. <span class="highlight"> is like creating a "highlight" element

67 of 67

Other selectors:

Next time!