Week 4: CSS Basics
CSS | Selectors, Box Model, Colors, Fonts, Backgrounds
Al-Aqsa University
CSS Basics
Cascading Style Sheets - Making the web beautiful
Week 04
1. What is CSS? Introduction
CSS - Cascading Style Sheets
CSS is a stylesheet language that describes how HTML elements should be displayed on screen. It controls layout, colors, fonts, spacing, and much more. CSS separates content (HTML) from presentation (styling).
- Cascading - Styles cascade from multiple sources and the most specific rule wins
- Style - Visual appearance of elements
- Sheets - Separate files (.css) or embedded style blocks
Three Ways to Add CSS
1. Inline CSS - Directly on the element using the style attribute:
HTML - Inline CSS
<h1 style="color: red; font-size: 24px;">Hello World</h1>
<p style="background: yellow; padding: 10px;">Styled paragraph</p>
2. Internal CSS - Inside a <style> tag in the <head>:
HTML - Internal CSS
<head>
<style>
h1 {
color: red;
font-size: 24px;
}
p {
background: yellow;
padding: 10px;
}
</style>
</head>
3. External CSS - Separate .css file linked with <link> (recommended):
HTML - Linking External CSS
<head>
<link rel="stylesheet" href="style.css">
</head>
style.css - External File
h1 {
color: red;
font-size: 24px;
}
p {
background: yellow;
padding: 10px;
}
Best Practice
Always use external CSS files for real projects. It keeps your code organized, reusable, and easier to maintain. Inline styles should be avoided as they are hard to maintain and override.
CSS Syntax
Every CSS rule consists of a selector and a declaration block:
CSS Syntax
selector {
property: value; /* This is a declaration */
property2: value2;
}
/* Example */
h1 {
color: #4caf50; /* text color */
font-size: 32px; /* text size */
text-align: center; /* alignment */
}
2. CSS Selectors Targeting Elements
Basic Selectors
| Selector |
Syntax |
Description |
| Element |
p { } |
Selects all <p> elements |
| Class |
.intro { } |
Selects elements with class="intro" |
| ID |
#main { } |
Selects the element with id="main" |
| Universal |
* { } |
Selects all elements |
| Grouping |
h1, h2, p { } |
Selects multiple elements at once |
CSS - Basic Selectors
/* Element selector - targets all paragraphs */
p {
color: #b0b0c0;
line-height: 1.6;
}
/* Class selector - targets elements with class="highlight" */
.highlight {
background-color: yellow;
padding: 5px;
}
/* ID selector - targets the element with id="header" */
#header {
background: #1a1a2e;
padding: 20px;
}
/* Universal selector - targets everything */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* Grouping - same styles for multiple selectors */
h1, h2, h3 {
font-family: 'Inter', sans-serif;
color: #fff;
}
Combinator Selectors
| Combinator |
Syntax |
Description |
| Descendant |
div p { } |
All <p> inside <div> (any depth) |
| Child |
div > p { } |
Direct <p> children of <div> only |
| Adjacent Sibling |
h1 + p { } |
First <p> immediately after <h1> |
| General Sibling |
h1 ~ p { } |
All <p> siblings after <h1> |
CSS - Combinator Selectors
/* Descendant: any p inside .card (even nested) */
.card p {
color: #999;
}
/* Child: only direct li children of .menu */
.menu > li {
display: inline-block;
padding: 10px 15px;
}
/* Adjacent sibling: first p right after h2 */
h2 + p {
font-size: 1.1rem;
color: #4caf50;
}
/* General sibling: all p after h2 at same level */
h2 ~ p {
margin-bottom: 10px;
}
Attribute Selectors
CSS - Attribute Selectors
/* Elements with a title attribute */
[title] {
cursor: help;
}
/* Input with type="text" */
input[type="text"] {
border: 2px solid #4caf50;
padding: 8px;
}
/* Links that start with "https" */
a[href^="https"] {
color: #4caf50;
}
/* Links that end with ".pdf" */
a[href$=".pdf"] {
color: #e91e63;
}
/* Elements with class containing "btn" */
[class*="btn"] {
cursor: pointer;
border-radius: 5px;
}
3. Pseudo-classes & Pseudo-elements Dynamic Styling
Pseudo-classes
Pseudo-classes select elements based on their state or position in the DOM.
CSS - Pseudo-classes
/* :hover - when mouse is over the element */
a:hover {
color: #4caf50;
text-decoration: underline;
}
/* :focus - when element is focused (clicked/tabbed) */
input:focus {
border-color: #4caf50;
outline: none;
box-shadow: 0 0 5px rgba(76, 175, 80, 0.5);
}
/* :active - when element is being clicked */
button:active {
transform: scale(0.98);
background: #388e3c;
}
/* :first-child - first element among siblings */
li:first-child {
font-weight: bold;
color: #4caf50;
}
/* :last-child - last element among siblings */
li:last-child {
border-bottom: none;
}
/* :nth-child(n) - select by position */
tr:nth-child(even) {
background: rgba(255,255,255,0.03);
}
tr:nth-child(odd) {
background: rgba(255,255,255,0.06);
}
/* :nth-child with formula: every 3rd element */
li:nth-child(3n) {
color: #ff9800;
}
/* :not() - select everything except */
p:not(.special) {
color: #999;
}
input:not([type="submit"]) {
border: 1px solid #555;
}
nth-child Patterns
:nth-child(2) = 2nd child | :nth-child(even) = 2, 4, 6... | :nth-child(odd) = 1, 3, 5... | :nth-child(3n) = 3, 6, 9... | :nth-child(3n+1) = 1, 4, 7...
Pseudo-elements
Pseudo-elements create virtual elements that don't exist in the HTML. They use :: (double colon).
CSS - Pseudo-elements
/* ::before - insert content before element */
.note::before {
content: "Note: ";
font-weight: bold;
color: #4caf50;
}
/* ::after - insert content after element */
.external-link::after {
content: " ↗";
color: #4caf50;
}
/* ::first-line - style the first line of text */
p::first-line {
font-weight: bold;
color: #fff;
}
/* ::first-letter - style the first letter */
p.drop-cap::first-letter {
font-size: 3rem;
float: left;
line-height: 1;
margin-right: 8px;
color: #4caf50;
}
/* Practical example: decorative line */
.section-title::after {
content: "";
display: block;
width: 50px;
height: 3px;
background: #4caf50;
margin-top: 8px;
}
Important
::before and ::after require the content property to work, even if it is just an empty string content: "". They do not work on void elements like <img> or <input>.
4. Specificity & Cascade How Conflicts Are Resolved
The Cascade
When multiple CSS rules target the same element, the browser resolves conflicts using:
- Origin & Importance: !important > author styles > browser defaults
- Specificity: More specific selectors win
- Source Order: Later rules override earlier ones (when specificity is equal)
Specificity Calculation
Specificity is calculated as a 4-part score: (inline, IDs, Classes, Elements)
| Selector |
Specificity |
Score |
* |
(0, 0, 0, 0) |
0 |
p |
(0, 0, 0, 1) |
1 |
.intro |
(0, 0, 1, 0) |
10 |
p.intro |
(0, 0, 1, 1) |
11 |
#header |
(0, 1, 0, 0) |
100 |
#header .nav li |
(0, 1, 1, 1) |
111 |
style="..." |
(1, 0, 0, 0) |
1000 |
CSS - Specificity Example
/* Specificity: 0,0,0,1 */
p { color: blue; }
/* Specificity: 0,0,1,0 -- WINS over element */
.text { color: green; }
/* Specificity: 0,1,0,0 -- WINS over class */
#main { color: red; }
/* !important overrides everything (avoid using it!) */
p { color: purple !important; }
Avoid !important
Using !important makes debugging very difficult. Instead, use more specific selectors or restructure your CSS. It should only be used as a last resort.
5. Colors Color Systems in CSS
Color Formats
CSS - Color Formats
/* Named colors (140+ built-in names) */
.box1 { color: red; }
.box2 { color: tomato; }
.box3 { color: cornflowerblue; }
/* Hexadecimal (#RRGGBB or #RGB shorthand) */
.hex1 { color: #ff0000; } /* red */
.hex2 { color: #4caf50; } /* green */
.hex3 { color: #f00; } /* shorthand for #ff0000 */
.hex4 { color: #4caf5080; } /* with alpha (50% opacity) */
/* RGB - Red, Green, Blue (0-255) */
.rgb1 { color: rgb(255, 0, 0); } /* red */
.rgb2 { color: rgb(76, 175, 80); } /* green */
/* RGBA - RGB + Alpha (0-1 transparency) */
.rgba1 { color: rgba(255, 0, 0, 0.5); } /* 50% transparent red */
.rgba2 { background: rgba(0, 0, 0, 0.7); } /* 70% opaque black */
/* HSL - Hue (0-360), Saturation (%), Lightness (%) */
.hsl1 { color: hsl(0, 100%, 50%); } /* red */
.hsl2 { color: hsl(122, 39%, 49%); } /* green */
.hsl3 { color: hsl(240, 100%, 50%); } /* blue */
/* HSLA - HSL + Alpha */
.hsla1 { color: hsla(122, 39%, 49%, 0.5); }
Color Swatches
#ff0000
#4caf50
#2196f3
#ff9800
rgba 50%
hsl purple
HSL is Intuitive
HSL is often easier to work with: Hue is the color wheel (0=red, 120=green, 240=blue), Saturation is intensity (0%=gray, 100%=vivid), Lightness is brightness (0%=black, 50%=normal, 100%=white).
6. Text & Font Properties Typography
Font Properties
CSS - Font Properties
/* font-family: set the typeface (use fallback fonts!) */
body {
font-family: 'Inter', 'Helvetica Neue', Arial, sans-serif;
}
.code {
font-family: 'Courier New', Consolas, monospace;
}
/* font-size: text size */
h1 { font-size: 2.5rem; } /* relative to root */
h2 { font-size: 2rem; }
p { font-size: 16px; } /* absolute pixels */
.small { font-size: 0.875em; } /* relative to parent */
/* font-weight: boldness (100-900 or keywords) */
.light { font-weight: 300; }
.normal { font-weight: 400; } /* same as normal */
.semi { font-weight: 600; }
.bold { font-weight: 700; } /* same as bold */
.black { font-weight: 900; }
/* font-style: italic */
.italic { font-style: italic; }
.normal-style { font-style: normal; }
Text Properties
CSS - Text Properties
/* line-height: space between lines */
p {
line-height: 1.6; /* multiplier (recommended) */
}
/* text-align: horizontal alignment */
.left { text-align: left; }
.center { text-align: center; }
.right { text-align: right; }
.justify { text-align: justify; }
/* text-decoration: lines on text */
a { text-decoration: none; } /* remove underline */
.underline { text-decoration: underline; }
.strike { text-decoration: line-through; }
.overline { text-decoration: overline; }
/* Custom underline */
.fancy { text-decoration: underline wavy #4caf50; }
/* text-transform: change case */
.upper { text-transform: uppercase; } /* ALL CAPS */
.lower { text-transform: lowercase; } /* all lower */
.capital { text-transform: capitalize; } /* First Letter */
/* letter-spacing and word-spacing */
.spaced { letter-spacing: 2px; }
.wide { word-spacing: 5px; }
/* text-shadow */
.shadow {
text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
}
.glow {
text-shadow: 0 0 10px #4caf50, 0 0 20px #4caf50;
}
Live Text Examples
Spaced Heading
Italic green text
Wavy underline text
Glowing text effect
7. Backgrounds Colors, Images & Gradients
Background Properties
CSS - Background Properties
/* background-color */
.box {
background-color: #1e1e2e;
}
/* background-image */
.hero {
background-image: url('hero.jpg');
background-size: cover; /* cover | contain | px/% */
background-position: center; /* center | top | bottom | px */
background-repeat: no-repeat; /* repeat | repeat-x | repeat-y */
background-attachment: fixed; /* scroll | fixed | local */
}
/* Shorthand */
.hero {
background: url('hero.jpg') center/cover no-repeat fixed;
}
Gradients
CSS - Gradients
/* Linear gradient (direction, color stops) */
.gradient1 {
background: linear-gradient(to right, #4caf50, #2196f3);
}
.gradient2 {
background: linear-gradient(135deg, #667eea, #764ba2);
}
.gradient3 {
background: linear-gradient(to bottom, #0f0f1a, #1a1a2e, #16213e);
}
/* Radial gradient (shape, color stops) */
.radial1 {
background: radial-gradient(circle, #4caf50, #0f0f1a);
}
.radial2 {
background: radial-gradient(ellipse at top left, #ff9800 0%, transparent 50%);
}
/* Gradient as overlay on image */
.overlay {
background: linear-gradient(rgba(0,0,0,0.7), rgba(0,0,0,0.3)), url('bg.jpg') center/cover;
}
Gradient Examples
8. The Box Model Layout Foundation
Understanding the Box Model
Every HTML element is a rectangular box. The box model consists of: Content, Padding, Border, and Margin (from inside to outside).
Margin
Border
Padding
Content
CSS - Box Model Properties
/* Width and Height */
.box {
width: 300px;
height: 200px;
min-width: 200px;
max-width: 100%;
min-height: 100px;
}
/* Padding (space INSIDE the border) */
.box {
padding: 20px; /* all sides */
padding: 10px 20px; /* top/bottom left/right */
padding: 10px 20px 15px; /* top left/right bottom */
padding: 10px 20px 15px 5px; /* top right bottom left */
padding-top: 10px;
padding-right: 20px;
padding-bottom: 15px;
padding-left: 5px;
}
/* Margin (space OUTSIDE the border) */
.box {
margin: 20px; /* all sides */
margin: 20px auto; /* center horizontally */
margin-top: 30px;
margin-bottom: 0;
}
/* box-sizing: border-box (IMPORTANT!) */
* {
box-sizing: border-box; /* width includes padding+border */
}
/* Without border-box: total width = width + padding + border */
/* With border-box: total width = width (padding+border are inside) */
Always Use border-box
Add * { box-sizing: border-box; } at the top of every CSS file. Without it, padding and border are ADDED to the width, making layouts unpredictable. With border-box, a 300px-wide box stays 300px even with padding and border.
9. Borders & Border Radius Decorating Boxes
Border Properties
CSS - Border Properties
/* Border shorthand: width style color */
.box {
border: 2px solid #4caf50;
}
/* Individual properties */
.box {
border-width: 2px;
border-style: solid; /* solid|dashed|dotted|double|groove|ridge|inset|outset|none */
border-color: #4caf50;
}
/* Individual sides */
.box {
border-top: 3px solid #4caf50;
border-right: 1px dashed #888;
border-bottom: 3px solid #4caf50;
border-left: none;
}
/* Border radius (rounded corners) */
.rounded { border-radius: 10px; } /* all corners */
.circle { border-radius: 50%; } /* perfect circle */
.pill { border-radius: 50px; } /* pill shape */
.custom { border-radius: 10px 0 10px 0; } /* diagonal corners */
.top-only { border-radius: 10px 10px 0 0; } /* top corners only */
Border Styles
solid
dashed
dotted
double
radius: 10px
50%
10. Box Shadow Adding Depth
box-shadow Property
CSS - Box Shadow
/* box-shadow: x-offset y-offset blur spread color */
/* Simple shadow */
.shadow1 {
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
}
/* Elevated card effect */
.shadow2 {
box-shadow: 0 8px 25px rgba(0, 0, 0, 0.4);
}
/* Colored shadow */
.shadow3 {
box-shadow: 0 5px 20px rgba(76, 175, 80, 0.4);
}
/* Inset shadow (inner shadow) */
.shadow4 {
box-shadow: inset 0 2px 10px rgba(0, 0, 0, 0.5);
}
/* Multiple shadows */
.shadow5 {
box-shadow:
0 2px 5px rgba(0,0,0,0.2),
0 8px 20px rgba(0,0,0,0.15),
0 0 0 1px rgba(255,255,255,0.05);
}
/* Glow effect */
.glow {
box-shadow: 0 0 15px #4caf50, 0 0 30px rgba(76,175,80,0.3);
}
Shadow Examples
Simple
Elevated
Colored
Inset
Glow
11. Opacity & Visibility Show and Hide
Controlling Visibility
CSS - Opacity & Visibility
/* opacity: 0 (invisible) to 1 (fully visible) */
.faded {
opacity: 0.5; /* 50% transparent - affects ALL children */
}
/* visibility: hides element but KEEPS its space */
.hidden {
visibility: hidden; /* invisible but takes up space */
}
.visible {
visibility: visible; /* default */
}
/* display: none - removes element from layout completely */
.gone {
display: none; /* invisible AND takes no space */
}
/* Comparison:
opacity: 0 = invisible, takes space, clickable
visibility: hidden = invisible, takes space, NOT clickable
display: none = invisible, NO space, NOT clickable
*/
Opacity Levels
1.0
0.8
0.6
0.4
0.2