L3: CSS

Deepseek prompt: extract everything possible and most useful for writing css for my midterm from this presentation, i rely on you heavily but make it also in a very compact format:

CSS Midterm Study Guide — INF2040B Lecture 03


1. CSS Basics

  • CSS = Cascading Style Sheets — controls presentation/layout of HTML

  • History: Proposed 1994 → CSS1 1996 → CSS2 1998 → CSS3 1999 (ongoing)

  • Rule syntax: selector { property: value; }

  • Separates content (HTML) from design (CSS)


2. Types of CSS

Type

Where

Example

Inline

Inside HTML tag

<p style="">

Internal

<style> in <head>

<style> p { } </style>

External

Separate .css file

<link rel="stylesheet" href="style.css">


3. Selectors (Critical — know all of these)

Selector

Syntax

Targets

Universal

*

All elements

Element

p

All <p> tags

Class

.myClass

Elements with class="myClass"

ID

#myId

Element with id="myId"

Grouping

h1, h2, p

All listed elements

Descendant

div p

All <p> inside any <div> (any depth)

Child

div > p

<p> that is a direct child of <div>

Adjacent Sibling

h1 + p

<p> immediately after <h1>

General Sibling

h1 ~ p

All <p> siblings after <h1>

Key distinction: Descendant (div p) = any depth. Child (div > p) = direct only.
Adjacent sibling (+) = immediately after. General sibling (~) = all after.


4. Pseudo-Classes

a:hover        /* mouse over */
a:link         /* unvisited link */
a:visited      /* visited link */
a:active       /* being clicked */
input:focus    /* element has focus */
p:first-child  /* first child of parent */
p:last-child   /* last child of parent */
p:nth-child(n) /* nth child (n can be number, odd, even, formula) */
p:not(.skip)   /* elements NOT matching selector */
input:checked  /* checked checkbox/radio */
input:disabled /* disabled input */
input:required /* required input */

5. Pseudo-Elements

p::before      /* insert content before element */
p::after       /* insert content after element */
p::first-line  /* style first line of text */
p::first-letter /* style first letter */
::selection    /* user-selected text */

Pseudo-class = : (state of element)
Pseudo-element = :: (part of element)


6. CSS Box Model

┌──────────────────────────────┐
│           MARGIN             │  ← outside (space between elements)
│  ┌────────────────────────┐  │
│  │         BORDER         │  │  ← can have width, style, color
│  │  ┌──────────────────┐  │  │
│  │  │     PADDING      │  │  │  ← inside (space between content & border)
│  │  │  ┌────────────┐  │  │  │
│  │  │  │  CONTENT   │  │  │  │  ← text, images, etc.
│  │  │  └────────────┘  │  │  │
│  │  └──────────────────┘  │  │
│  └────────────────────────┘  │
└──────────────────────────────┘
/* Shorthand: top right bottom left (clockwise) */
margin: 10px 20px 10px 20px;
margin: 10px 20px;          /* top/bottom | left/right */
margin: 10px;               /* all sides */
margin: 10px 20px 5px;      /* top | left/right | bottom */

/* Same pattern for padding */
padding: 10px 20px;

/* Border */
border: 2px solid #000;     /* width style color */
border-style: solid | dashed | dotted | double | none;

box-sizing: border-box — includes padding & border in element's total width/height (recommended!)


7. CSS Units

Unit

Type

Description

px

Absolute

Fixed pixel — precise control

pt

Absolute

1/72 inch — use for print only

%

Relative

% of parent element

em

Relative

Relative to current element's font size (compounds with nesting!)

rem

Relative

Relative to root (html) font size — no compounding

vw

Relative

% of viewport width

vh

Relative

% of viewport height

If base font = 16px: 1.5em = 24px. Watch for compounding with nested em!


8. CSS Colors

Format

Syntax

Notes

Hex

#RRGGBB or #RGB

#FF5733

RGB

rgb(255, 87, 51)

0–255 per channel

RGBA

rgba(255, 87, 51, 0.5)

a = 0 (transparent) → 1 (opaque)

HSL

hsl(14, 100%, 60%)

h=0–360°, s/l=0–100%

HSLA

hsla(14, 100%, 60%, 0.5)

+ alpha channel

Named

red, blue, transparent

Limited set

HSL hue reference: 0/360=red, 120=green, 240=blue
Saturation: 100%=full color, 0%=grayscale
Lightness: 0%=black, 50%=normal, 100%=white


9. CSS Positioning

Value

Behavior

static

Default — normal document flow, top/left etc. have no effect

relative

Offset from its own normal position; still occupies original space

absolute

Positioned relative to nearest positioned ancestor (non-static); removed from flow

fixed

Relative to browser window; stays put on scroll

sticky

Relative until scroll threshold, then fixed

position: absolute;
top: 20px;
left: 50px;
z-index: 10;   /* stacking order; higher = in front */

"Positioned ancestor" = any ancestor with position other than static


10. CSS Variables (Custom Properties)

/* Define in :root for global scope */
:root {
  --primary-
  --
}

/* Use with var() */
p {
  color: var(--primary-color);
  font-size: var(--font-size);
}

/* Fallback value */
color: var(--primary-color, blue);

11. Media Queries

/* Basic syntax */
@media media-type and (condition) { ... }

/* Most common: screen width breakpoints */
@media screen and (max-width: 600px) { ... }   /* mobile */
@media screen and (min-width: 768px) { ... }   /* tablet+ */
@media screen and (min-width: 1024px) { ... }  /* desktop */

/* Combining */
@media screen and (min-width: 600px) and (max-width: 900px) { ... }
@media screen and (orientation: portrait) { ... }
@media print {  }

/* Mobile-first: write base styles for mobile, then override for larger */
/* Start with small, use min-width to go up */

Media Types: screen, print, all (default), speech
Media Features: width, height, min-width, max-width, orientation, resolution


12. CSS Animations & Transitions

Transitions (simple hover effects)

/* transition: property duration timing-function delay */
.btn {
  transition: background-color 0.3s ease;
  transition: all 0.3s ease-in-out;
}

Animations (keyframe-based)

/* Define */
@keyframes slidein {
  from { transform: translateX(-100%); }
  to   { transform: translateX(0); }
}

/* Or with % */
@keyframes bounce {
  0%   { top: 0; }
  50%  { top: 50px; }
  100% { top: 0; }
}

/* Apply */
.box {
  animation: slidein 1s ease-in-out infinite;
  /* name | duration | timing | iteration-count */
}

Timing functions: ease, linear, ease-in, ease-out, ease-in-out


13. CSS Transforms

/* Translate (move) */
transform: translateX(50px);
transform: translateY(-20px);
transform: translate(50px, -20px);

/* Scale */
transform: scale(1.5);       /* 1.5x both axes */
transform: scaleX(2);
transform: scaleY(0.5);

/* Rotate */
transform: rotate(45deg);

/* Skew */
transform: skewX(20deg);
transform: skewY(10deg);
transform: skew(20deg, 10deg);

/* Multiple */
transform: translate(50px, 20px) rotate(45deg) scale(1.2);

14. Common CSS Properties Cheatsheet

/* Text */


font-family: Arial, sans-serif;
font-weight: bold | normal | 100–900;
font-style: italic | normal;
text-align: left | right | center | justify;
text-decoration: underline | none | line-through;
text-transform: uppercase | lowercase | capitalize;
line-height: 1.5;
letter-spacing: 2px;

/* Background */

background-image: url('img.jpg');
background-size: cover | contain | 100%;
background-position: center;
background-repeat: no-repeat;
background: #fff url('img.jpg') no-repeat center/cover; /* shorthand */

/* Display */
display: block | inline | inline-block | flex | grid | none;
visibility: hidden;     /* hides but keeps space */
opacity: 0.5;           /* 0=invisible, 1=fully visible */

/* Sizing */
width: 100px | 50% | auto;
height: 200px;
max-width: 1200px;
min-width: 300px;
overflow: visible | hidden | scroll | auto;

/* Flexbox */
display: flex;
flex-direction: row | column | row-reverse | column-reverse;
justify-content: flex-start | flex-end | center | space-between | space-around;
align-items: stretch | flex-start | flex-end | center | baseline;
flex-wrap: wrap | nowrap;
gap: 10px;
flex: 1;   /* flex-grow shorthand */

/* Cursor */
cursor: pointer | default | not-allowed;

15. Bootstrap (Quick Reference)

Import via CDN:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5/dist/css/bootstrap.min.css">

Grid system (12-column):

<div class="container">
  <div class="row">
    <div class="col-6">Half</div>   <!-- 6/12 = 50% -->
    <div class="col-4">Third</div>  <!-- 4/12 = 33% -->
    <div class="col">Auto</div>     <!-- equal width -->
  </div>
</div>

Responsive grid:

col-sm-*   <!-- ≥576px -->
col-md-*   <!-- ≥768px -->
col-lg-*   <!-- ≥992px -->
col-xl-*   <!-- ≥1200px -->

Common classes: btn btn-primary, btn-secondary, btn-danger, container, container-fluid, row, col, d-flex, justify-content-center, text-center, mt-3, p-2


16. Sass vs Less (CSS Preprocessors)

Feature

Sass

Less

Variable syntax

var:value;</code></p></td><tdcolspan="1"rowspan="1"><p><code>@var:value;</code></p></td></tr><tr><tdcolspan="1"rowspan="1"><p>Nesting</p></td><tdcolspan="1"rowspan="1"><p><spandata−name="checkmarkbutton"data−type="emoji">✅</span></p></td><tdcolspan="1"rowspan="1"><p><spandata−name="checkmarkbutton"data−type="emoji">✅</span></p></td></tr><tr><tdcolspan="1"rowspan="1"><p>Mixins</p></td><tdcolspan="1"rowspan="1"><p><code>@mixin</code>/<code>@include</code></p></td><tdcolspan="1"rowspan="1"><p><code>.mixin()</code></p></td></tr><tr><tdcolspan="1"rowspan="1"><p>Inheritance</p></td><tdcolspan="1"rowspan="1"><p><code>@extend</code></p></td><tdcolspan="1"rowspan="1"><p><code>.extend</code></p></td></tr><tr><tdcolspan="1"rowspan="1"><p>Fileextension</p></td><tdcolspan="1"rowspan="1"><p><code>.scss</code>/<code>.sass</code></p></td><tdcolspan="1"rowspan="1"><p><code>.less</code></p></td></tr><tr><tdcolspan="1"rowspan="1"><p>Compilation</p></td><tdcolspan="1"rowspan="1"><p>Ruby/Dart</p></td><tdcolspan="1"rowspan="1"><p>Node.js</p></td></tr></tbody></table><p><strong>Sassexample:</strong></p><pre><codeclass="language−scss">var: value;</code></p></td><td colspan="1" rowspan="1"><p><code>@var: value;</code></p></td></tr><tr><td colspan="1" rowspan="1"><p>Nesting</p></td><td colspan="1" rowspan="1"><p><span data-name="check_mark_button" data-type="emoji">✅</span></p></td><td colspan="1" rowspan="1"><p><span data-name="check_mark_button" data-type="emoji">✅</span></p></td></tr><tr><td colspan="1" rowspan="1"><p>Mixins</p></td><td colspan="1" rowspan="1"><p><code>@mixin</code> / <code>@include</code></p></td><td colspan="1" rowspan="1"><p><code>.mixin()</code></p></td></tr><tr><td colspan="1" rowspan="1"><p>Inheritance</p></td><td colspan="1" rowspan="1"><p><code>@extend</code></p></td><td colspan="1" rowspan="1"><p><code>.extend</code></p></td></tr><tr><td colspan="1" rowspan="1"><p>File extension</p></td><td colspan="1" rowspan="1"><p><code>.scss</code> / <code>.sass</code></p></td><td colspan="1" rowspan="1"><p><code>.less</code></p></td></tr><tr><td colspan="1" rowspan="1"><p>Compilation</p></td><td colspan="1" rowspan="1"><p>Ruby/Dart</p></td><td colspan="1" rowspan="1"><p>Node.js</p></td></tr></tbody></table><p><strong>Sass example:</strong></p><pre><code class="language-scss">primary: #3498db; .button { background: $primary; &:hover { background: darken($primary, 10%); } } .primary-button { @extend .button; }

Less example:

@primary: #3498db;
.button {
  background: @primary;
}
.primary-button {
  .button();
  
}

17. Responsive Design Principles

/* 1. Viewport meta tag (HTML) — ALWAYS include */
<meta name="viewport" content="width=device-width, initial-scale=1.0">

/* 2. Fluid grids — use % not px for widths */
.container { width: 90%; max-width: 1200px; }

/* 3. Flexible images */
img { max-width: 100%; height: auto; }

/* 4. Mobile-first: base = mobile, then min-width queries */
.nav { flex-direction: column; }
@media (min-width: 768px) {
  .nav { flex-direction: row; }
}

18. Specificity — How CSS Decides Which Rule Wins

Specificity order (highest → lowest):

  1. !important (avoid overusing)

  2. Inline styles (style="")

  3. ID selectors (#id) — 100 points

  4. Class, pseudo-class, attribute selectors — 10 points

  5. Element, pseudo-element selectors — 1 point

  6. Universal selector * — 0 points

When specificity ties, last rule in code wins (cascade).


19. Quick Reference — Common Mistakes

  • em compounds through nesting — prefer rem for consistent sizing

  • position: absolute needs a position: relative ancestor to anchor to

  • visibility: hidden vs display: none — hidden keeps space, none removes it

  • margin: auto on a block element with a set width = horizontal center

  • CSS applies last declared rule when specificity is equal

  • box-sizing: border-box prevents padding from expanding element size

  • Pseudo-element ::before/::after requires content: "" to appear


LAB EXERCISES


Problem1:

 Create a 3 div elements in an HTML page

 In each div, nest a paragraph with some text

 Use CSS to set (different for each div):

 Text color

 Text decoration

<title>Task 1</title>
    <style>
        .div1 p {
            color: red;
            text-decoration: underline;
            font-style: italic;
        }
        .div2 p {
            color: blue;
            text-decoration: line-through;
            font-style: italic;
        }
        .div3 p {
            color: pink;
            text-decoration: overline;
            font-style: italic;
        }
    </style>
</head>
<body>
    <div class="div1">
        <p>First color</p>
    </div>
    <div class="div2">
        <p>Second color</p>
    </div>
    <div class="div3">
        <p>Second color</p>
    </div>
</body>

Problem 2:

 Create 3 Divs

 Use CSS to mimic a flag – setting 3 color bands of your choosing for the colors

<title>task 2</title>
    <style>
        .flag {
            width: 300px;
            height: 200px;
            border: 1px solid black;
        }
        .band1, .band2, .band3 {
            width: 100%
        }

        .band1 {
            background-color: #ff5702;
            height: 33.33%;
        }
        .band2 {
            background-color: #cc0808;
            height: 33.33%;
        }
        .band3 {
            background-color: #aa9909;
            height: 33.33%;
        }


    </style>
</head>
<body>
    <div class="flag">
        <div class="band1"></div>
        <div class="band2"></div>
        <div class="band3"></div>
    </div>
</body>

Problem 3:

 Create a demonstration of the CSS Box model:

 A div element

 Borders for each boundary

<title>task 3</title>
    <style>
        .box {
            width: 200px;
            height: 100px;
            margin: 5px;
            padding: 40px;
            border: 5px solid yellow;
            background-color: lightgreen;
        }

        .container {
            width: 300px;
            height: 200px;
            border: 2px dashed black;
            padding: 10px;
        }
    </style>
</head>
<body>
    <div class = "container">
        <div class="box">
            This is our box model.
        </div>
    </div> 
</body>

Problem 4:

 Create a Table with 3 columns and three rows

 Populate the table with some basic text

 Style the first column of the table

 Make sure the styling is also applied to the header cell

<title>Task 4</title>
    <style>
        /*decorate the <table>*/
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            border: 1px solid #dd4543;
            padding: 8px;
            text-align: left;
        }
        td:first-child, th> :first-child {
            background-color: #333;
            font-weight: bold;
            color:aqua;

        }
    </style>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th>First Columns</th>
                <th>Second Columns</th>
                <th>Third Columns</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Row 1, Column 1</td>
                <td>Row 1, Column 2</td>
                <td>Row 1, Column 3</td>
            </tr>
            <tr>
                <td>Row 2, Column 1</td>
                <td>Row 2, Column 2</td>
                <td>Row 2, Column 3</td>
            </tr>
            <tr>
                <td>Row 3, Column 1</td>
                <td>Row 3, Column 2</td>
                <td>Row 3, Column 3</td>
            </tr>
        </tbody>
    </table>
</body>

Problem 5:

 Create a Table with 3 columns and three rows

 Populate the table with some basic text

 Apply background color to normal and alternating rows

<style>
        table {
            width: 100%;
            border-collapse: collapse;
        }
        tbody tr> :nth-child(odd) {
            background-color: antiquewhite;
        }
        tbody tr> :nth-child(even) {
            background-color: firebrick;
        }
    </style>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th>First Column</th>
                <th>Second Column</th>
                <th>Third Column</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Row 1, Column 1</td>
                <td>Row 1, Column 2</td>
                <td>Row 1, Column 3</td>
            </tr>
            <tr>
                <td>Row 2, Column 1</td>
                <td>Row 2, Column 2</td>
                <td>Row 2, Column 3</td>
            </tr>
            <tr>
                <td>Row 3, Column 1</td>
                <td>Row 3, Column 2</td>
                <td>Row 3, Column 3</td>
            </tr>
        </tbody>
    </table>
</body>

Problem 6:

 Create via HTML and CSS a demonstration of the Flex Layout:

 3 Elements

 Positioned horizontally

<title>Task 6</title>
    <style>
        .flex-container {
            display: flex;
            justify-content: space-around;
            align-items: center;
            height: 200px;
            background-color: blueviolet;
            border: 2px solid burlywood;
        }
        .flex-item {
            background-color: aliceblue;
            color: antiquewhite;
            padding: 20px;
            border-radius: 10px;
            text-align: center;
        }
        .flex-item-1 {
            flex: 1;
        }

        .flex-item-2 {
            flex: 2;
        }
        .flex-item-3 {
            flex: 3;
        }
    </style>
</head>
<body>
    <h1>CSS Flexbox Layout Example</h1>
    <div class="flex-container">
        <div class="flex-item flex-item-1">Item 1</div>
        <div class="flex-item flex-item-2">Item 2 (grows 2x)</div>
        <div class="flex-item flex-item-3">Item 3 (grows 3x)</div>
    </div>
</body>

Problem 7:

 Create via HTML and CSS a demonstration of the Grid Layout:

 3 Columns

 2 Rows

 Positioned horizontally

<title>Task 7</title>
    <style>
        .grid-container {
            display: grid;
            grid-template-columns: Col1 Col2 Col3;
            grid-gap: 10px;
            padding: 10px;
            background-color: darkcyan;
        }
        .grid-item {
            background-color: darkred;
            color: limegreen;
            padding: 20px;
            text-align: center;
            font-size: 18px;
            border-radius: 5px;
        }
    </style>
</head>
<body>
    <h1>CSS Grid Example</h1>
    <div class="grid-container">
        <div class="grid-container">Item 1</div>
        <div class="grid-container">Item 2</div>
        <div class="grid-container">Item 3</div>
        <div class="grid-container">Item 4</div>
        <div class="grid-container">Item 5</div>
        <div class="grid-container">Item 6</div>
    </div>
</body>