1 of 37

12. Intro to CSS

MSER 521�Fall 2025

1

2 of 37

Guiding Question: How to you style your work?

2

3 of 37

Outline

  1. The DOM
  2. Intro to CSS
  3. Selectors
  4. The box model
  5. Specificity and the cascade
  6. Some common properties

3

4 of 37

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. That said:

  • CSS is used to style the DOM
  • Javascript is used to manipulate the DOM on-the-fly after the page has already loaded.

5 of 37

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>

6 of 37

Outline

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

6

7 of 37

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!

8 of 37

How to include a stylesheet in your HTML

External stylesheets are stored in a separate *.css

9 of 37

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

9

10 of 37

Anatomy of a Ruleset

body {

color: black;

padding: 1em;

}

10

selector

property

value

declaration

11 of 37

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>

12 of 37

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>

13 of 37

Outline

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

13

14 of 37

Selectors: Most Important Ones

14

Selector

Example

Description

element

p

Selects all <p> elements

.class

.card

Selects all elements with class of ”card”

#id

#firstname

Selects the element with id of ”firstname”

selector, selector

div, .card

Grouping selector: Select all <div> elements and all elements with class of ”card”’

selector selector

div p

Descendant selector: “Select all <p> elements that are descendants of the <div> element.”

selector > selector

div > p

Direct children selector: “Select all <p> elems that are direct children of <div> elems”

MEMORIZE

15 of 37

...

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;

}

16 of 37

...

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;

}

17 of 37

...

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;

}

18 of 37

...

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;

}

19 of 37

20 of 37

1. Which are the selectors?

h1 {

color: blue;

margin-top: 1em;

}�

.title-bar {

padding: 5px;

font-weight: bold;

}�

#profile {

background-color: #ddd;

}

21 of 37

2. Name three properties

h1 {

color: blue;

margin-top: 1em;

}�

.title-bar {

padding: 5px;

font-weight: bold;

}�

#profile {

background-color: #ddd;

}

22 of 37

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>

...

23 of 37

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>

...

24 of 37

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;

}

25 of 37

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>

...

26 of 37

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>

...

27 of 37

Outline

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

27

28 of 37

The Box Model

28

References:

  • W3 Schools

29 of 37

The Box Model

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

30 of 37

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>

31 of 37

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>

32 of 37

Outline

  • Paper share-outs
  • Homework 2 overview
  • The DOM
  • Intro to CSS
  • Selectors
  • The box model
  • Specificity and the cascade
  • Some common properties

32

33 of 37

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 37

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.

35 of 37

Outline

  • Paper share-outs
  • Homework 2 overview
  • The DOM
  • Intro to CSS
  • Selectors
  • The box model
  • Specificity and the cascade
  • Some common properties

35

36 of 37

Common CSS Properties and Techniques

37 of 37

Activity

37