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 |
|
Internal |
|
|
External | Separate |
|
3. Selectors (Critical — know all of these)
Selector | Syntax | Targets |
|---|---|---|
Universal |
| All elements |
Element |
| All |
Class |
| Elements with |
ID |
| Element with |
Grouping |
| All listed elements |
Descendant |
| All |
Child |
|
|
Adjacent Sibling |
|
|
General Sibling |
| All |
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 |
|---|---|---|
| Absolute | Fixed pixel — precise control |
| Absolute | 1/72 inch — use for print only |
| Relative | % of parent element |
| Relative | Relative to current element's font size (compounds with nesting!) |
| Relative | Relative to root ( |
| Relative | % of viewport width |
| Relative | % of viewport height |
If base font = 16px:
1.5em = 24px. Watch for compounding with nestedem!
8. CSS Colors
Format | Syntax | Notes |
|---|---|---|
Hex |
|
|
RGB |
| 0–255 per channel |
RGBA |
| a = 0 (transparent) → 1 (opaque) |
HSL |
| h=0–360°, s/l=0–100% |
HSLA |
| + alpha channel |
Named |
| 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 |
|---|---|
| Default — normal document flow, |
| Offset from its own normal position; still occupies original space |
| Positioned relative to nearest positioned ancestor (non-static); removed from flow |
| Relative to browser window; stays put on scroll |
| 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
positionother thanstatic
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 |
Less example: 17. Responsive Design Principles18. Specificity — How CSS Decides Which Rule WinsSpecificity order (highest → lowest):
When specificity ties, last rule in code wins (cascade). 19. Quick Reference — Common Mistakes
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 Problem 2: Create 3 Divs Use CSS to mimic a flag – setting 3 color bands of your choosing for the colors Problem 3: Create a demonstration of the CSS Box model: A div element Borders for each boundary 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 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 Problem 6: Create via HTML and CSS a demonstration of the Flex Layout: 3 Elements Positioned horizontally Problem 7: Create via HTML and CSS a demonstration of the Grid Layout: 3 Columns 2 Rows Positioned horizontally |