الأسبوع 4: أساسيات CSS
CSS | المحددات، نموذج الصندوق، الألوان، الخطوط، الخلفيات
جامعة الأقصى
أساسيات CSS
أوراق الأنماط المتتالية (Cascading Style Sheets) - نجعل الويب جميلًا
الأسبوع 04
1. ما هي CSS؟ مقدمة
CSS - أوراق الأنماط المتتالية (Cascading Style Sheets)
CSS لغة تنسيق تصف كيف تُعرض عناصر HTML على الشاشة. فهي تتحكم في تخطيط الصفحة والألوان والخطوط والمسافات وغير ذلك الكثير. وتفصل CSS بين المحتوى (HTML) وطريقة العرض (التنسيق).
- Cascading (متتالية) - تأتي التنسيقات من عدة مصادر وتتتابع، والقاعدة الأكثر تحديدًا هي التي تفوز
- Style (نمط) - المظهر المرئي للعناصر
- Sheets (أوراق) - ملفات منفصلة (.css) أو كتل تنسيق داخل الصفحة
ثلاث طرق لإضافة CSS
1. التنسيق المضمَّن (Inline CSS) - يُكتب مباشرة على العنصر باستخدام السمة style:
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) - داخل الوسم <style> في قسم <head>:
HTML - تنسيق داخلي (Internal CSS)
<head>
<style>
h1 {
color: red;
font-size: 24px;
}
p {
background: yellow;
padding: 10px;
}
</style>
</head>
3. التنسيق الخارجي (External CSS) - ملف .css منفصل يُربط بالصفحة عبر <link> (الطريقة الموصى بها):
HTML - ربط ملف CSS خارجي
<head>
<link rel="stylesheet" href="style.css">
</head>
style.css - الملف الخارجي
h1 {
color: red;
font-size: 24px;
}
p {
background: yellow;
padding: 10px;
}
أفضل ممارسة
استخدم دائمًا ملفات CSS الخارجية في المشاريع الحقيقية؛ فهي تجعل الكود منظّمًا وقابلًا لإعادة الاستخدام وأسهل في الصيانة. وتجنّب التنسيق المضمَّن لأنه صعب الصيانة ويصعب تجاوزه.
صياغة CSS (Syntax)
كل قاعدة في CSS تتكون من محدِّد (Selector) وكتلة تصريحات (Declaration Block):
صياغة CSS
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) استهداف العناصر
المحدِّدات الأساسية
| المحدِّد |
الصياغة |
الوصف |
| العنصر (Element) |
p { } |
يختار جميع عناصر <p> |
| الصنف (Class) |
.intro { } |
يختار العناصر التي تحمل class="intro" |
| المعرّف (ID) |
#main { } |
يختار العنصر الذي يحمل id="main" |
| الشامل (Universal) |
* { } |
يختار جميع العناصر |
| التجميع (Grouping) |
h1, h2, p { } |
يختار عدة عناصر في وقت واحد |
CSS - المحدِّدات الأساسية
/* 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;
}
محدِّدات العلاقات (Combinators)
| نوع العلاقة |
الصياغة |
الوصف |
| الأحفاد (Descendant) |
div p { } |
جميع عناصر <p> داخل <div> (على أي مستوى) |
| الأبناء المباشرون (Child) |
div > p { } |
عناصر <p> التي هي أبناء مباشرون لـ <div> فقط |
| الأخ المجاور (Adjacent Sibling) |
h1 + p { } |
أول <p> يأتي مباشرة بعد <h1> |
| الإخوة عمومًا (General Sibling) |
h1 ~ p { } |
جميع إخوة <p> الذين يأتون بعد <h1> |
CSS - محدِّدات العلاقات
/* 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 - محدِّدات السمات
/* 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)
تختار الأصناف الزائفة العناصر بناءً على حالتها أو موقعها في شجرة الصفحة (DOM).
CSS - الأصناف الزائفة
/* :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
:nth-child(2) = الابن الثاني | :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)
تُنشئ العناصر الزائفة عناصر افتراضية غير موجودة فعليًا في كود HTML، وتُكتب باستخدام :: (نقطتان رأسيتان مزدوجتان).
CSS - العناصر الزائفة
/* ::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;
}
مهم
العنصران ::before و ::after يحتاجان إلى الخاصية content حتى يعملا، حتى لو كانت نصًا فارغًا content: "". ولا يعملان مع العناصر الفارغة (void elements) مثل <img> أو <input>.
4. الأولوية والتتالي (Specificity & Cascade) كيف تُحلّ التعارضات
التتالي (Cascade)
عندما تستهدف عدة قواعد CSS العنصر نفسه، يحلّ المتصفح التعارض بالاعتماد على:
- المصدر والأهمية: !important > تنسيقات المطوّر > التنسيقات الافتراضية للمتصفح
- الأولوية (Specificity): المحدِّدات الأكثر تحديدًا هي التي تفوز
- ترتيب الكتابة: القاعدة المكتوبة لاحقًا تتغلب على السابقة (عند تساوي الأولوية)
حساب الأولوية
تُحسب الأولوية كدرجة من 4 أجزاء: (المضمَّن inline، المعرّفات IDs، الأصناف Classes، العناصر Elements)
| المحدِّد |
الأولوية |
الدرجة |
* |
(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: 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; }
تجنّب !important
استخدام !important يجعل اكتشاف الأخطاء صعبًا جدًا. بدلًا من ذلك، استخدم محدِّدات أكثر تحديدًا أو أعد ترتيب كود CSS. ولا تستخدمها إلا كحل أخير.
5. الألوان أنظمة الألوان في CSS
صيغ الألوان
CSS - صيغ الألوان
/* 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); }
عيّنات ألوان
#ff0000
#4caf50
#2196f3
#ff9800
rgba 50%
hsl بنفسجي
صيغة HSL سهلة الفهم
غالبًا ما يكون التعامل مع HSL أسهل: درجة اللون (Hue) هي موقع اللون على عجلة الألوان (0=أحمر، 120=أخضر، 240=أزرق)، والتشبّع (Saturation) هو شدة اللون (0%=رمادي، 100%=زاهٍ)، والإضاءة (Lightness) هي درجة السطوع (0%=أسود، 50%=طبيعي، 100%=أبيض).
6. خصائص النص والخط الطباعة (Typography)
خصائص الخط (Font)
CSS - خصائص الخط
/* 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)
CSS - خصائص النص
/* 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;
}
أمثلة حيّة على النصوص
Spaced Heading
نص أخضر مائل
نص بخط سفلي متموّج
تأثير النص المتوهّج
7. الخلفيات الألوان والصور والتدرّجات (Gradients)
خصائص الخلفية
CSS - خصائص الخلفية
/* 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 - التدرّجات اللونية
/* 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;
}
أمثلة على التدرّجات
8. نموذج الصندوق (Box Model) أساس تخطيط الصفحة
فهم نموذج الصندوق
كل عنصر في HTML عبارة عن صندوق مستطيل. ويتكون نموذج الصندوق (من الداخل إلى الخارج) من: المحتوى (Content)، والحشوة (Padding)، والحد (Border)، والهامش (Margin).
الهامش (Margin)
الحد (Border)
الحشوة (Padding)
المحتوى (Content)
CSS - خصائص نموذج الصندوق
/* 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) */
استخدم border-box دائمًا
أضف * { box-sizing: border-box; } في بداية كل ملف CSS. فبدونها تُضاف الحشوة والحد إلى العرض، مما يجعل التخطيط غير متوقع. أما مع border-box فإن الصندوق الذي عرضه 300px يبقى 300px حتى مع وجود الحشوة والحد.
9. الحدود والزوايا الدائرية (Border & Border Radius) تزيين الصناديق
خصائص الحدود
CSS - خصائص الحدود
/* 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 */
أنماط الحدود
solid
dashed
dotted
double
radius: 10px
50%
10. ظل الصندوق (Box Shadow) إضافة العمق
الخاصية box-shadow
CSS - ظل الصندوق
/* 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);
}
أمثلة على الظلال
بسيط
مرتفع
ملوّن
داخلي
متوهّج
11. الشفافية والظهور (Opacity & Visibility) الإظهار والإخفاء
التحكم في ظهور العناصر
CSS - الشفافية والظهور
/* 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
*/
مستويات الشفافية
1.0
0.8
0.6
0.4
0.2