What is CSS & Why Do We Need It?
CSS Introduction · Inline Styles · 3 Ways to Add CSS
CSS = Cascading Style Sheets — هي اللغة اللي بتتحكم في شكل صفحات الويب. يعني لو الـ HTML هو "الهيكل العظمي" للصفحة، فالـ CSS هو "الملابس والديكور" اللي بيخليها تبان حلوة.
/* selector { property: value; } */
h1 {
color: crimson;
font-size: 2em;
background: #f0f0f0;
}
p {
font-family: Verdana, sans-serif;
margin: 20px;
line-height: 1.6;
}
What is CSS? = اللغة اللي بتستخدم لعمل style للـ HTML documents — بتتحكم في colors, fonts, layouts
How does it work? = CSS rules بتستهدف HTML elements وبتحدد ازاي تتعرض
Why use CSS? = ملف CSS واحد بيتحكم في صفحات كتير → وفر وقت وثبات
المشكلة (قبل CSS):
كل page كان فيها style attributes في كل element → كود كتير جداً ومش منظم
<font color="red" size="3"> → كانوا بيحطوها في كل مكان!<!-- كل صفحة كانت زي دي! -->
<h1><font color="red" size="6">Title</font></h1>
<p><font color="blue">Paragraph</font></p>
<!-- ×100 pages = nightmare! -->
الحل (CSS):
W3C عملت CSS عشان تفصل الـ structure (HTML) عن الـ presentation (CSS)
h1 { color: red; font-size: 2em; }
p { color: blue; }
/* غيّره مرة واحدة → اتغير للموقع كله! */
الـ W3C هي اللي صممت CSS عشان تحل مشكلة الـ formatting في HTML
بتحط الـ CSS مباشرة على الـ element نفسه. مش موصى بيها إلا لو عايز تعمل تغيير لعنصر واحد بس.
Font Family Example:
<h1 style="font-family:verdana;">This is a heading</h1>
<p style="font-family:courier;">This is a paragraph.</p>
Colors and Sizes:
<p>I am normal</p>
<p style="color:red;">I am red</p>
<p style="color:blue;">I am blue</p>
<p style="font-size:50px;">I am big</p>
Combined Styles:
<h1 style="color:blue;text-align:center;">Styled Heading</h1>
style attribute مباشرة على الـ HTML element. ممكن تحط فيه أي CSS property.مباشرة على العنصر. بس لو عندك elements كتير ده بيكون مزعج.
<p style="color:red;">
Red text
</p>
جوا الـ <head> في <style> tag. كويس لو الصفحة دي ليها style مختلف.
<head>
<style>
p { color: red; }
</style>
</head>
ملف .css منفصل. الأفضل لمواقع فيها صفحات كتير — غيّر ملف واحد يتغير كل حاجة!
<head>
<link rel="stylesheet"
type="text/css"
href="style.css">
</head>
• الـ External CSS file مش المفروض يحتوي على HTML tags — CSS فقط!
• الـ link element بيتحط في الـ <head> section
• الـ type="text/css" هو نوع الـ MIME للـ CSS files
CSS Syntax & Cascading Order
Rule Anatomy · Priority · Multiple Stylesheets · Comments
Selector = بيحدد أنهي HTML element هتطبق عليه الـ style
Declaration Block = كل حاجة جوا الـ { } — ممكن يكون فيها declarations كتير
Property = اسم الـ style اللي عايز تغيره (color, font-size, ...)
Value = القيمة بتاعة الـ property (red, 12px, ...)
Declaration = property + value مع بعض زي "color:red"
"color:red" can be called = Declaration (مش Selector ومش Value)
لما يبقى في conflict بين rules — أنهي rule يتطبق؟ في ترتيب محدد للـ priority:
/* External file: mystyle.css */
h1 { color: navy; } ← Priority 3
/* Internal: inside <style> in <head> */
h1 { color: orange; } ← Priority 2 (wins over external!)
/* Inline on element */
<h1 style="color:pink"> ← Priority 1 (WINS everything!)
/* Multiple stylesheets — last one wins! */
<link href="mystyle.css"> ← h1: color blue
<link href="mystyle2.css"> ← h1: color red → هذا يفوز!
Inline style هو الأعلى دايماً. External هو قبل browser default بس.
/* Single-line comment */
p { color: red; }
/* This is
a multi-line
comment */
p { color: red; }
/* Temporarily disabled:
p { color: blue; }
*/
• بتبدأ بـ
/* وبتخلص بـ */• الـ browser بيتجاهلها خالص
• ممكن تكون single-line أو multi-line
• بنستخدمها لشرح الكود أو لـ disable مؤقت لـ rules
⚠️ مفيش // comments في CSS زي JavaScript!
Quiz — CSS Foundations
أسئلة حقيقية من امتحانات mid-2022 / mid-2023 / final-2024 / final-2025
Simple CSS Selectors
Element · Class · ID · Universal · Grouping
| Selector | Syntax | بيستهدف إيه؟ | مثال |
|---|---|---|---|
| Element | p { } | كل <p> elements في الصفحة | p { color: red; } |
| Class | .center { } | كل elements عندها class="center" | .center { text-align: center; } |
| ID | #para1 { } | element واحد فقط بـ id="para1" | #para1 { color: blue; } |
| Universal | * { } | كل الـ elements في الصفحة | * { margin: 0; } |
| Grouping | h1, h2, p { } | مجموعة selectors مع بعض | h1,h2,p { color: navy; } |
p {
text-align: center;
color: red;
}
/* كل <p> في الصفحة بتتأثر */
<p>Styled!</p>
<div>Not styled</div>
<p>Also Styled!</p>
#para1 {
text-align: center;
color: red;
}
/* element واحد بس بـ id="para1" */
<p id="para2">Not styled</p>
/* 1. كل elements بالـ class */
.center { text-align: center; }
/* 2. نوع محدد بالـ class فقط */
p.center { text-align: center; }
/* بس <p> elements مع class="center" */
/* 3. Element بـ classes كتير */
<p class="center large">Two classes!</p>
<p class="center">Styled by p.center</p>
<div>Not styled</div>
/* Universal — كل حاجة في الصفحة */
* {
text-align: center;
color: blue;
}
/* Grouping — بدل ما تكرر */
/* قبل: */
h1 { text-align: center; color: red; }
h2 { text-align: center; color: red; }
p { text-align: center; color: red; }
/* بعد — بالـ Grouping: */
h1, h2, p {
text-align: center;
color: red;
}
الـ Universal بيستهدف كل الـ elements مش بس الـ p. عايز كل الـ p بس؟ استخدم element selector:
p { }Combinator Selectors — النوع الأصعب!
Descendant · Child · Adjacent Sibling · General Sibling
| Combinator | Symbol | المعنى | مثال |
|---|---|---|---|
| Descendant | space | كل F جوا E بأي مستوى | div p |
| Child | > | F اللي هو direct child لـ E فقط | div > p |
| Adjacent Sibling | + | أول F مباشرة بعد E (نفس الـ parent) | div + p |
| General Sibling | ~ | كل F بعد E (نفس الـ parent) | div ~ p |
دي الـ HTML اللي بتيجي في كل الامتحانات. لازم تحفظها وتعرف تحللها:
<h2 id="id1">Hello World</h2>
<p>Welcome</p> ← p قبل الـ div
<div>
<p>Paragraph 1.</p> ← direct child of div ✓
<p>Paragraph 2.</p> ← direct child of div ✓
<section>
<p>Paragraph 3.</p> ← inside section, NOT direct child ✗
</section>
</div>
<p>Paragraph 4.</p> ← sibling after div ✓
<p>Paragraph 5.</p> ← sibling after div ✓
كل <p> جوا الـ div بأي مستوى
P3 جوا section جوا div → تحت.
P3 جوا <section> — مش direct child
∴ مش بتتأثر!
أول <p> مباشرة بعد <div>
P5 مش immediately after.
• div > p = 2 (مش 3!) — P3 جوا section
• div ~ p = 2 (مش 3!) — P قبل div مش بيتحسب
• div + p = 1 — الأولانية بس immediately بعد div
div > p {
background-color: yellow;
}
/* فقط <p> direct children of <div> */
<p>✓ Direct child</p>
<span><p>✗ Grandchild</p></span>
</div>
div ~ p {
background-color: yellow;
}
/* كل <p> siblings بعد <div> */
<div></div>
<p>✓ After div</p>
<p>✓ Also after div</p>
CSS Pseudo-classes
:hover · :first-child · :empty · :focus · Anchor States
الـ Pseudo-class بتحدد حالة خاصة للـ element — زي لما الماوس عليه، أو أول element في الـ parent. Syntax: selector:pseudo-class { property: value; }
a:hover { color: hotpink; } /* hover state */
input:focus { border-color: blue; } /* focused input */
li:first-child { font-weight: bold; } /* first in list */
p:empty { display: none; } /* p with no children */
input:enabled { color: green; } /* enabled input */
input:disabled { color: gray; } /* disabled input */
الـ links ليها 4 states ممكن تستايلها:
a:link { color: #FF0000; } /* unvisited link — لم يزر بعد */
a:visited { color: #00FF00; } /* visited link — زاره */
a:hover { color: #FF00FF; } /* mouse over — ماوس فوقه */
a:active { color: #0000FF; } /* selected link — لما بيكليك عليه */
•
a:hover = لما الماوس فوق الـ link (mouse over)•
a:link = الـ link اللي مزاروش•
a:active = وقت الـ click•
a::hover بـ double colon → WRONG! — Pseudo-class بـ single colonp:first-child بتستهدف <p> element بس لو ده أول child بتاع الـ parent بتاعه.
<body>
<h2>CSS Selector</h2> ← أول child!
<p>Hello I am Here:</p> ← 2nd child
<p>Paragraph 2</p> ← 3rd child
<p>Hello P3</p> ← 4th child
</body>
p:first-child { color: red; }
p:first-child تعني:"أي <p> هو أول child لـ parent بتاعه"
في الـ body، أول child = <h2> مش <p>
∴ مفيش <p> هو first-child → 0 styled!
لو كان في div جوا الـ body:
<div><p>first</p></div>هنا الـ p ده first-child ✓
h2:first-child بيتطبق لأن h2 هو أول child لـ body. = 1 element ✓:enabled { color: green; }
:disabled { color: gray; }
:checked { accent-color: blue; }
/* مش ::enabled (double colon) */
/* Pseudo-class = single colon : */
سألوا "Which CSS selector selects the elements that are enabled?"
الإجابة الصح =
:enabled (single colon)::enabled = WRONG (double colon للـ pseudo-elements مش pseudo-classes)CSS Pseudo-elements
::first-line · ::first-letter · ::before · ::after · ::selection
الـ Pseudo-element بتستايل جزء معين من الـ element — زي أول حرف أو أول سطر. Syntax: selector::pseudo-element { }
Pseudo-class =
: (single) — state بتاع الـ elementPseudo-element =
:: (double) — جزء من الـ elementp::first-letter { font-size: 200%; } ← Drop cap
p::first-line { color: #0000FF; } ← أول سطر
h1::before { content: "★ "; } ← قبل
h2::after { content: " ★"; } ← بعد
::marker { color: red; } ← list bullet
::selection { background: yellow; } ← selected text
| Pseudo-element | بيعمل إيه؟ |
|---|---|
::first-line | بيستايل أول سطر في الـ text block — الـ browser بيحدد هو أول سطر إيه بناءً على عرض الـ viewport |
::first-letter | بيستايل أول حرف — زي الـ Drop Cap في الجرايد |
::before | بيضيف content قبل الـ element content — محتاج content property |
::after | بيضيف content بعد الـ element content |
::marker | بيستايل bullet أو number الـ list item |
::selection | بيستايل الـ text اللي المستخدم بيسيلكته بالماوس |
CSS Attribute Selectors
[attr] · [attr=val] · [attr^=val] · [attr$=val] · [attr*=val]
| Selector | المعنى | مثال |
|---|---|---|
[attr] | كل elements عندها الـ attribute ده (بأي قيمة) | a[target] |
[attr=v] | attribute بقيمة معينة exactly | a[target="_blank"] |
[attr~=v] | attribute value فيها الـ word ده | [class~="note"] |
[attr|=v] | attribute يساوي v أو يبدأ بـ v- (للغات) | [lang|="en"] |
[attr^=v] | attribute يبدأ بـ v | a[href^="https"] |
[attr$=v] | attribute ينتهي بـ v | a[href$=".pdf"] |
[attr*=v] | attribute يحتوي على v في أي مكان | [class*="card"] |
/* كل <a> عندها target attribute */
a[target] { background-color: yellow; }
/* فقط target="_blank" */
a[target="_blank"] { background-color: green; }
/* type="text" inputs */
input[type="text"] { border: 2px solid blue; }
الـ HTML: <a href="w3schools">, <a href="disney" target="_blank">, <a href="wiki" target="_top">
a[target=_blank] → disney فقط = 1 element
a[target] → disney + wiki = 2 elements (أي target attr)
a { } → كل الـ 3 links = 3 elements
[target=_blank] = اسمه Attribute Selector (مش anchor selector)
Quiz — CSS Selectors
أصعب جزء في الامتحانات — تمرن كويس!
div > p { color: red }, how many p elements will be styled from the exam HTML?div ~ p {color: blue}, the number of p elements that will be styled is:div + p { color: red}, how many p elements will be styled from the exam HTML?p:first-child {color:red;} on the CSS Selector exam HTML, how many elements will be styled?p.center {color: blue;} on the final-2025 exam HTML (which has <p class="center"> and <h1 class="center">), the number of elements that will be styled is:CSS Colors
Named · HEX · RGB · HSL · RGBA · HSLA
| نوع الـ Color | مثال | Note |
|---|---|---|
| Named | color: Tomato; | اسم باللون الإنجليزي |
| HEX | color: #FF6347; | #rrggbb |
| RGB | color: rgb(255,99,71); | 0–255 لكل channel |
| HSL | color: hsl(9,100%,64%); | Hue+Saturation+Lightness |
| RGBA | color: rgba(255,99,71,0.5); | + Opacity (0–1) |
| HSLA | color: hsla(9,100%,64%,0.5); | + Opacity (0–1) |
Valid color:
#000000 (6 digits hex) ✓Invalid:
#00000000 (8 digits) ✗color: red = color: #ff0000 — نفس الشيء!Tomato
DodgerBlue
Violet
Red
Lime
Blue
Black
White
rgb(255, 0, 0) /* Red 🔴 */
rgb(0, 128, 0) /* Green 🟢 */
rgb(0, 0, 255) /* Blue 🔵 */
rgb(0, 0, 0) /* Black ⚫ */
rgb(255, 255, 255) /* White ⚪ */
rgb(128, 128, 128) /* Gray 🩶 */
• كل قيمة من 0 لـ 255
• 0,0,0 = Black (كل الألوان absent)
• 255,255,255 = White (كل الألوان full)
• 128,128,128 = Gray (كل الألوان متساوية)
• RGBA = نفس RGB + alpha (opacity) من 0 لـ 1
#ff0000 /* Red */
#00ff00 /* Lime */
#0000ff /* Blue */
#000000 /* Black */
#ffffff /* White */
/* Shorthand — لو الـ pairs متشابهة: */
#f00 /* = #ff0000 */
#fff /* = #ffffff */
#000 /* = #000000 */
• 6 digits:
#rrggbb (الصح)•
#00000000 → WRONG (8 digits)•
#0000000000 → WRONG (10 digits)• Shorthand: #fff بدل #ffffff
• #ff0000 = red = rgb(255,0,0) = "red" — كلهم نفس اللون!
hsl(0, 100%, 50%) /* Red */
hsl(120, 100%, 50%) /* Lime */
hsl(240, 100%, 50%) /* Blue */
hsl(39, 100%, 50%) /* Orange */
hsl(0, 0%, 50%) /* Gray */
hsl(0, 0%, 0%) /* Black */
hsl(0, 0%, 100%) /* White */
Hue (0–360°):
0 = Red, 120 = Green, 240 = Blue
Saturation (%):
0% = gray, 100% = full color
Lightness (%):
0% = black, 50% = normal, 100% = white
CSS Backgrounds & Borders
background-color · background-image · background-attachment · Borders
body {
/* لون الخلفية */
background-color: lightblue;
/* صورة خلفية — بتتكرر by default */
background-image: url('bg.jpg');
/* إيه اللي بيحصل لو المسافة خلصت */
background-repeat: no-repeat;
/* موقع الصورة */
background-position: center top;
/* الصورة ثابتة ولا بتتحرك مع الـ scroll؟ */
background-attachment: fixed;
}
/* Shorthand */
body {
background: lightblue url('bg.jpg') no-repeat center;
}
background-attachment: fixed = الصورة بتفضل في مكانها حتى لو الصفحة اتـscrolledbackground-attachment: scroll = بتتحرك مع الـ scrollp {
border-style: solid;
border-width: 3px;
border-color: tomato;
}
/* Shorthand: width style color */
p { border: 3px solid tomato; }
/* Per-side control */
p {
border-top: 2px dotted blue;
border-right: 2px solid red;
border-bottom: 2px dashed green;
border-left: 2px double violet;
}
dotted, dashed, solid, double, groove, ridge, none, hidden
CSS Margin & Padding — Box Model
Margin (outside) · Padding (inside) · Shorthand
Margin = مسافة من بره العنصر (بين العنصر وجيرانه)
Padding = مسافة من جوا العنصر (بين الـ content والـ border)
"generate space around p element content, inside of any defined borders" → CSS Padding
"space between element and its neighboring element" → CSS Margin
/* Per side */
p {
margin-top: 100px;
margin-right: 150px;
margin-bottom: 100px;
margin-left: 80px;
}
/* Shorthand: top right bottom left */
p { margin: 100px 150px 100px 80px; }
/* Center horizontally */
div { width: 300px; margin: 0 auto; }
/* Per side */
div {
padding-top: 50px;
padding-right: 30px;
padding-bottom: 50px;
padding-left: 80px;
}
/* Shorthand: top right bottom left */
div { padding: 50px 30px 50px 80px; }
/* Best practice: */
* { box-sizing: border-box; }
Quiz — CSS Visual Properties
Colors, Backgrounds, Borders, Box Model
Responsive CSS & CSS Grid
Media Queries · Mobile-First · Grid System
الـ Responsive design بيخلي الموقع يشتغل كويس على أي حجم شاشة — desktop, tablet, mobile.
/* Base styles — for mobile first */
.container { width: 100%; padding: 10px; }
/* Tablet and above */
@media (min-width: 768px) {
.container { max-width: 720px; margin: 0 auto; }
}
/* Desktop and above */
@media (min-width: 1024px) {
.container { max-width: 960px; }
}
بتكتب الـ styles الأساسية للـ mobile أولاً، وبعدين بـ @media بتضيف styles للـ tablet والـ desktop. ده أفضل للـ performance.
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}
/* fr = fractional unit */
/* 3 equal columns */
/* Mix with other units: */
grid-template-columns: 60% 1fr 2fr;
Grid Container: display: grid (parent)
Grid Items: كل direct children
fr unit: fractional unit — بيقسم المساحة المتاحة
gap: المسافة بين الـ cells
1fr 2fr 1fr = الـ middle column ضعف الباقيين
Grid = 2D (rows + columns)
Flexbox = 1D (row OR column)
Modern CSS — Variables & Flexbox
Custom Properties · CSS Variables · Flexbox · Transitions
/* Define in :root — global scope */
:root {
--brand-color: #00AD72;
--font-size-base: 16px;
--spacing-md: 1.5rem;
}
/* Use with var() */
h1 { color: var(--brand-color); }
p { margin-bottom: var(--spacing-md); }
/* Dark mode override */
@media (prefers-color-scheme: dark) {
:root { --brand-color: #00FF99; }
}
/* Shorter version: */
:root { --primary: #007BC4; --gap: 1rem; }
p { color: var(--primary); margin: var(--gap); }
• بتعرّف القيمة مرة واحدة وبتستخدمها في أي مكان
• لو غيرت الـ variable، كل حاجة بتستخدمها بتتغير
• مناسبة للـ theming (light/dark mode)
• أسهل من Sass/Less في المشاريع الجديدة
:root = أعلى element في الـ document (زي <html>)
.container {
display: flex;
justify-content: space-between; /* horizontal */
align-items: center; /* vertical */
}
/* Transitions */
button {
transition: background-color 0.3s ease;
}
button:hover { background: navy; }
Use Grid for 2D layouts (rows AND columns at the same time)
Use Flexbox for 1D layouts (either row OR column)
CSS Specificity — Why Your Styles Get Overridden!
Specificity Score · !important · Best Practices
لما في تعارض بين rules، الـ browser بيحسب "specificity score" ويطبق الـ rule اللي عنده أعلى score.
p { color: red; } /* 0-0-1 LOSES */
.text { color: blue; } /* 0-1-0 */
#content p { color: green;} /* 1-0-1 WINS vs above */
/* Inline style ALWAYS wins (except !important) */
<p style="color:pink">
/* !important overrides everything (last resort!) */
p { color: red !important; }
/* Best Practice Order: */
/* Inline > ID > Class > Element */
• استخدم .class للـ styling مش #id (ID specificity عالي جداً وصعب تـoverride)
• ماتعمقش أكتر من 3 levels (div > ul > li > a > span — هيبقى صعب تتحكم فيه)
• !important = آخر حل مش الحل الأول!
❌ WRONG — Missing colon in declaration
.class { color: blue; font-weight; bold; }
^ هنا missing :
✅ CORRECT
.class { color: blue; font-weight: bold; }
❌ WRONG — CSS ID starting with digit
#1para { color: red; }
✅ CORRECT
#para1 { color: red; }
من الأسباب الشائعة اللي الـ CSS مش بتشتغل:
1. More specific rule overriding it
2. Syntax error (missing : أو ;)
3. CSS file not linked properly
4. Typo in selector name
5. Property not supported in browser
Quiz — Modern CSS & Specificity
Responsive, Grid, Variables, Specificity — أسئلة من final-2025
.class { color: blue; font-weight; bold; }.container p {color: green;} not apply to a paragraph inside a div with class "container"?p:empty {width: 100px; height: 20px; background: red;}, the number of elements that will be styled from the exam HTML (3 visible p tags, no empty ones) is:p:empty {color:red;}, how many elements will be styled from the final-2025 exam HTML?p:first-child { color: blue;} on the final-2025 exam HTML that starts with <h2>, how many elements will be styled?CSS Cheat Sheet — ملخص نهائي شامل
احفظ الصفحة دي قبل الامتحان!