1 of 43

Intro to CSS & the DOM

CSCI 185, Fall 2023�Intro to Computer Programming for the Web

1

2 of 43

Announcements

  • Academic indicators system
  • Please review the assigned readings for this week.
  • Tutorial 3 – deadline extended to Wednesday evening (leaves enough time to resolve any GitHub issues).
  • HW2 – deadline moved to this Friday (9/15) at 11:59PM
    • Don’t leave it ‘til the last minute!
  • Quiz 1 on Monday, 9/25 (2 weeks from today)�Study guide and practice quiz have been posted

2

3 of 43

Outline

  • The DOM
  • Intro to CSS
  • Selectors
  • The box model
  • Specificity and the cascade
  • Some common properties

3

4 of 43

Outline

  • The DOM
  • Intro to CSS
  • Selectors
  • The box model
  • Specificity and the cascade
  • Some common properties

4

5 of 43

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.

6 of 43

Convert this Simple HTML Page to a DOM Diagram

<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>

7 of 43

HTML Tree Diagram

<html>

<title>

<head>

<body>

<link>

<main>

<h1>

<p>

Hello!

Welcome to this webpage.

<meta>

8 of 43

DOM Schema

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>

9 of 43

The DOM (Document Object Model)

A way to manipulate elements and styles on a web page using CSS and JavaScript

Open your console and type:

> document

10 of 43

Outline

  • The DOM
  • Intro to CSS
  • Selectors
  • The box model
  • Specificity and the cascade
  • Some common properties

10

11 of 43

But how do you make stuff look nice?

11

12 of 43

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!

13 of 43

How to include a stylesheet in your HTML

External stylesheets are stored in a separate *.css

14 of 43

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

14

15 of 43

Anatomy of a Ruleset

body {

color: black;

padding: 1em;

}

15

selector

property

value

declaration

16 of 43

Relationships

Parent & Child

Siblings

1 <html>

2 <head><title>Hello!</title></head>

3 <body>

4 <div>

5 <h1>Left</h1>

6 <p>hi!</p>

7 <p><em>hello</em></p>

8 <hr

9 </div>

10 <div>Right

11 <ul>

12 <li>list item 1</li>

13 <li>list item 2</li>

14 <li>list item 3</li>

15 </ul>

16 </div>

17 </body>�18 </html>

17 of 43

Relationships

Descendants

Ancestors

1 <html>

2 <head><title>Hello!</title></head>

3 <body>

4 <div>

5 <h1>Left</h1>

6 <p>hi!</p>

7 <p><em>hello</em></p>

8 <hr

9 </div>

10 <div>Right

11 <ul>

12 <li>list item 1</li>

13 <li>list item 2</li>

14 <li>list item 3</li>

15 </ul>

16 </div>

17 </body>�18 </html>

18 of 43

Outline

  • The DOM
  • Intro to CSS
  • Selectors
  • The box model
  • Specificity and the cascade
  • Some common properties

18

19 of 43

Selectors: Most Important Ones

19

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

20 of 43

Selectors: Also Useful

20

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

21 of 43

...

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;

}

22 of 43

...

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;

}

23 of 43

...

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;

}

24 of 43

...

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;

}

25 of 43

26 of 43

1. Which are the selectors?

h1 {

color: blue;

margin-top: 1em;

}�

.title-bar {

padding: 5px;

font-weight: bold;

}�

#profile {

background-color: #ddd;

}

27 of 43

3. Name three properties

h1 {

color: blue;

margin-top: 1em;

}�

.title-bar {

padding: 5px;

font-weight: bold;

}�

#profile {

background-color: #ddd;

}

28 of 43

4. What will this rule set do to the HTML?

img {

width: 200px;

}

...

1 <body>

2 <div class=”nav-bar”>

3 <h1>Welcome, Malik</h1>

4 <section>

5 <p>text text text</p>

6 </section>

7 <img src=pic1.png” />

8 <span>123 456 789</span>

9 </div>

10 <img src=pic2.png” />

11 <p>blah blah blah</p>

12 <div>

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

14 </div>

...

29 of 43

5. What will these rule sets do to the HTML?

body {

text-transform: uppercase;

}

div img {

border: solid 2px #F00;

}

...

1 <body>

2 <div class=”nav-bar”>

3 <h1>Welcome, Malik</h1>

4 <section>

5 <p>text text text</p>

6 </section>

7 <img src=pic1.png” />

8 <span>123 456 789</span>

9 </div>

10 <img src=pic2.png” />

11 <p>blah blah blah</p>

12 <div>

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

14 </div>

...

30 of 43

5. What will these rule sets do to the HTML?

...

1 <body>

2 <div class=”nav-bar”>

3 <h1>Welcome, Malik</h1>

4 <section>

5 <p>text text text</p>

6 </section>

7 <img src=pic1.png” />

8 <span>123 456 789</span>

9 </div>

10 <img src=pic2.png” />

11 <p>blah blah blah</p>

12 <div>

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

14 </div>

...

body {

text-transform: uppercase;

}

div img {

border: solid 2px #F00;

}

31 of 43

6. What will this rule set do to the HTML?

* {

color: red;

text-transform: uppercase;

}

...

1 <body>

2 <div class=”nav-bar”>

3 <h1>Welcome, Malik</h1>

4 <section>

5 <p>text text text</p>

6 </section>

7 <img src=pic1.png” />

8 <span>123 456 789</span>

9 </div>

10 <img src=pic2.png” />

11 <p>blah blah blah</p>

12 <div>

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

14 </div>

...

32 of 43

7. What will this rule set do to the HTML?

h1, span {

color: red;

text-transform: uppercase;

}

...

1 <body>

2 <div class=”nav-bar”>

3 <h1>Welcome, Malik</h1>

4 <section>

5 <p>text text text</p>

6 </section>

7 <img src=pic1.png” />

8 <span>123 456 789</span>

9 </div>

10 <img src=pic2.png” />

11 <p>blah blah blah</p>

12 <div>

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

14 </div>

...

33 of 43

Outline

  • The DOM
  • Intro to CSS
  • Selectors
  • The box model
  • Specificity and the cascade
  • Some common properties

33

34 of 43

The Box Model

34

References:

  • W3 Schools

35 of 43

The Box Model

  • margin
  • padding
  • border
  • width
  • box-sizing (border-box v. content box)
  • display (block, inline-block, flex, grid, etc.). Anything but “inline”

36 of 43

Block-Level Elements

HTML tags that have a default display of: display: block;

<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>

37 of 43

Inline Elements

HTML tags that have a default display of: display: inline;

<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>

38 of 43

Outline

  • The DOM
  • Intro to CSS
  • Selectors
  • The box model
  • Specificity and the cascade
  • Some common properties

38

39 of 43

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.

40 of 43

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.

41 of 43

Outline

  • The DOM
  • Intro to CSS
  • Selectors
  • The box model
  • Specificity and the cascade
  • Some common properties

41

42 of 43

Common CSS Properties and Techniques

43 of 43

Activity

43