1 of 43

Intro to CSS & the DOM

CSCI 344: Advanced Web Technologies

Spring 2026

1

2 of 43

Announcements

Upcoming Deadlines:

  • Homework 1 due tonight at midnight; presentations during class on Friday

Readings & Quizzes

  • Please keep up with the readings and quizzes. CSS requires that you study the rules (esp. selector rules and flex / grid rules)

Roadmap

  • Today, Friday, and Monday: CSS
  • Wednesday: Design with Accessibility in mind
  • Tutorial 3: Design Systems with Tailwind

2

3 of 43

Before Lecture Today, Let’s Make a Homepage!

  1. Open your entire csci344 folder in VS Code
  2. At the root of your csci344 folder:
    • Make an empty file called index.html
    • Make an empty file called styles.css
  3. Create an HTML skeleton by using the html:5 shortcut (demo)
    • Create a link to your tutorial 1’s index.html file (demo)
    • Create a link to your styles.css stylesheet (demo)
  4. Make the background of your body tag hotpink (demo)
  5. Preview your file using Live Server – it should be hotpink!

3

4 of 43

About the special name “index.html”

  • The filename index.html is the default file that a web server will serve when a user requests a directory (if a file is not explicitly specified
  • For example, if you navigate to https://example.com/, the web server will look for a file named index.html in the root directory of the website. If it finds this file, it will serve it to the browser. If there is no index.html, the server might list the directory contents or return an error, depending on the server's configuration.
  • A convenient way to access the main page of a website without needing to type the full path to a specific file.

4

5 of 43

Outline

  1. The DOM
  2. Intro to CSS
  3. Selectors
  4. Specificity and the cascade
  5. The Box Model
  6. Common CSS Properties

5

6 of 43

Outline

  1. The DOM
  2. Intro to CSS
  3. Selectors
  4. Specificity and the cascade
  5. The Box Model
  6. Common CSS Properties

6

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

Open your console and type:

> document

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

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>

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

11 of 43

Outline

  1. The DOM
  2. Intro to CSS
  3. Selectors
  4. Specificity and the cascade
  5. The Box Model
  6. Common CSS Properties

11

12 of 43

But how do you make stuff look nice?

12

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

14 of 43

How to include a stylesheet in your HTML

External stylesheets are stored in a separate *.css

15 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

15

16 of 43

Anatomy of a Ruleset

body {

color: black;

padding: 1em;

}

16

selector

property

value

declaration

17 of 43

Outline

  1. The DOM
  2. Intro to CSS
  3. Selectors
  4. Specificity and the cascade
  5. The Box Model
  6. Common CSS Properties

17

18 of 43

Selectors: Most Important Ones

18

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

element, element

div, p

Selects all <div> elements and all <p> elements

MEMORIZE THIS SLIDE

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

}

20 of 43

Selectors: Some Additional Ones to Know…

20

Selector

Example

Description

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

}

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

25 of 43

1. Which are the selectors?

h1 {

color: blue;

margin-top: 1em;

}�

.title-bar {

padding: 5px;

font-weight: bold;

}�

#profile {

background-color: #ddd;

}

26 of 43

2. Name three properties

h1 {

color: blue;

margin-top: 1em;

}�

.title-bar {

padding: 5px;

font-weight: bold;

}�

#profile {

background-color: #ddd;

}

27 of 43

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

...

28 of 43

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

...

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

}

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

...

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

...

32 of 43

Outline

  1. The DOM
  2. Intro to CSS
  3. Selectors
  4. Specificity and the cascade
  5. The Box Model
  6. Common CSS Properties

32

33 of 43

Specificity and the Cascade

  1. 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.
  2. Specificity is the means by which browsers decide which CSS property values are the most relevant to an element and, therefore, will be applied.
  3. You can think of the cascade as a layering of styling rules, in order of specificity.

34 of 43

Specificity and the Cascade

  1. More specific style declarations take precedence over more “distant” ones:
  2. If one rule is more specific than another one, the more specific rule wins.
  3. If two rules share the same specificity, then then the more “recent” rule takes precedence.
  4. 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.

35 of 43

Outline

  1. The DOM
  2. Intro to CSS
  3. Selectors
  4. Specificity and the cascade
  5. The Box Model
  6. Common CSS Properties

35

36 of 43

The Box Model

36

References:

  • W3 Schools

37 of 43

The Box Model

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

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

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

40 of 43

Outline

  1. The DOM
  2. Intro to CSS
  3. Selectors
  4. Specificity and the cascade
  5. The Box Model
  6. Common CSS Properties

40

41 of 43

Common CSS Properties and Techniques

  1. Box Model (border, margin, padding, width, height)
  2. Fonts
  3. Color
  4. pseudo-classes (:hover, :active, :visited, etc.)

42 of 43

Activity: Design a Photo Gallery

42

43 of 43

Friday: �CSS Layouts + Presentations

43