1 of 41

Intro to CSS & the DOM

CSCI 185�Tools and Technologies of the World Wide Web

Fall, 2022

1

2 of 41

Announcements: Lab this Week: GitHub

Using GitHub, you’re going to publish your files to the cloud (someone else’s computer) so that other people can see your work!

2

3 of 41

3

GitHub�csci185-coursework

Your Computer

Your csci185

.git repository

4 of 41

Outline

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

4

5 of 41

Outline

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

5

6 of 41

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.

7 of 41

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>

8 of 41

HTML Tree Diagram

<html>

<title>

<head>

<body>

<link>

<main>

<h1>

<p>

Hello!

Welcome to this webpage.

<meta>

9 of 41

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>

10 of 41

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

11 of 41

Outline

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

11

12 of 41

But how do you make stuff look nice?

12

13 of 41

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!

14 of 41

How to include a stylesheet in your HTML

External stylesheets are stored in a separate *.css

15 of 41

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

15

16 of 41

Anatomy of a Ruleset

body {

color: black;

padding: 1em;

}

16

selector

property

value

declaration

17 of 41

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>

18 of 41

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>

19 of 41

Outline

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

19

20 of 41

Selectors: Most Important Ones

20

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

21 of 41

Selectors: Also Useful

21

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

22 of 41

...

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;

}

23 of 41

...

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 41

25 of 41

1. Which are the selectors?

h1 {

color: blue;

margin-top: 1em;

}�

.title-bar {

padding: 5px;

font-weight: bold;

}�

#profile {

background-color: #ddd;

}

26 of 41

3. Name three properties

h1 {

color: blue;

margin-top: 1em;

}�

.title-bar {

padding: 5px;

font-weight: bold;

}�

#profile {

background-color: #ddd;

}

27 of 41

4. What will this rule set 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>

...

img {

width: 200px;

}

28 of 41

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;

}

29 of 41

6. What will this rule set 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>

15 <button>Click me</button>

...

* {

color: red;

text-transform: uppercase;

}

30 of 41

7. What will this rule set 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>

15 <button>Click me</button>

...

h1, span {

color: red;

text-transform: uppercase;

}

31 of 41

Outline

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

31

32 of 41

The Box Model

32

References:

  • W3 Schools

33 of 41

The Box Model

  • margin
  • padding
  • border
  • width
  • box-sizing (border-box v. content box)
  • display (block, inline-block, inline, grid, none, etc.)

34 of 41

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>

35 of 41

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>

36 of 41

Outline

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

36

37 of 41

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.

38 of 41

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.

39 of 41

Outline

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

39

40 of 41

Common CSS Properties and Techniques

41 of 41

Activity

41