مقدمة عن CSS

ما هي CSS؟

CSS (Cascading Style Sheets) هي لغة تنسيق تستخدم لتنسيق صفحات HTML. تم تطويرها في عام 1996.

طرق ربط CSS:
  • Inline: داخل العنصر مباشرة
  • Internal: داخل <style> في <head>
  • External: ملف CSS منفصل
مثال External CSS:
<link rel="stylesheet" href="styles.css">
مثال CSS بسيط:
body {
    font-family: Arial, sans-serif;
    color: #333;
    background-color: #f4f4f4;
}

h1 {
    color: #007bff;
    font-size: 2em;
}

المحددات الأساسية

أنواع المحددات:

/* Element Selector */
p { color: blue; }

/* Class Selector */
.my-class { font-weight: bold; }

/* ID Selector */
#my-id { background: yellow; }

/* Universal Selector */
* { margin: 0; padding: 0; }

/* Grouping Selectors */
h1, h2, h3 { margin: 10px; }

المحددات المتقدمة

Descendant و Child Selectors:

/* Descendant (جميع الأحفاد) */
div p { color: red; }

/* Child (الأبناء المباشرون فقط) */
div > p { color: blue; }

/* Adjacent Sibling (الأخ المجاور) */
h1 + p { margin-top: 0; }

/* General Sibling (جميع الأخوة) */
h1 ~ p { color: green; }

الألوان والخلفيات

الألوان:

/* Named Colors */
p { color: red; }

/* Hex Colors */
p { color: #ff0000; }

/* RGB */
p { color: rgb(255, 0, 0); }

/* RGBA (مع الشفافية) */
p { color: rgba(255, 0, 0, 0.5); }
الخلفيات:
body {
    background-color: #f0f0f0;
    background-image: url("bg.jpg");
    background-repeat: no-repeat;
    background-position: center;
    background-size: cover;
}

النصوص

خصائص النص:

p {
    font-family: Arial, sans-serif;
    font-size: 16px;
    font-weight: bold;
    font-style: italic;
    text-align: center;
    text-decoration: underline;
    line-height: 1.6;
    letter-spacing: 2px;
    text-transform: uppercase;
}

المسافات والحدود

Margin و Padding:

div {
    margin: 10px;  /* جميع الجهات */
    margin: 10px 20px;  /* علوي/سفلي | يمين/يسار */
    margin: 10px 20px 30px 40px;  /* علوي | يمين | سفلي | يسار */
    
    padding: 15px;
    padding-top: 10px;
    padding-right: 20px;
}
الحدود:
div {
    border: 2px solid #000;
    border-width: 2px;
    border-style: solid;
    border-color: #000;
    border-radius: 10px;
}

نماذج الصندوق

Box Model:

كل عنصر في HTML هو صندوق يتكون من: Content, Padding, Border, Margin.

div {
    width: 300px;
    padding: 20px;
    border: 5px solid black;
    margin: 10px;
    box-sizing: border-box;  /* يجعل العرض يشمل padding و border */
}

العرض والارتفاع

Width و Height:

div {
    width: 300px;
    height: 200px;
    max-width: 100%;
    min-width: 200px;
    max-height: 500px;
    min-height: 100px;
}

Positioning

أنواع Positioning:

/* Static (افتراضي) */
div { position: static; }

/* Relative */
div { 
    position: relative;
    top: 10px;
    left: 20px;
}

/* Absolute */
div {
    position: absolute;
    top: 0;
    right: 0;
}

/* Fixed */
div {
    position: fixed;
    bottom: 0;
    left: 0;
}

/* Sticky */
div {
    position: sticky;
    top: 0;
}

Display

قيم Display:

/* Block */
div { display: block; }

/* Inline */
span { display: inline; }

/* Inline-Block */
div { display: inline-block; }

/* None (إخفاء) */
div { display: none; }

/* Flex */
div { display: flex; }

/* Grid */
div { display: grid; }

Float و Clear

Float:

img {
    float: left;
    margin-right: 20px;
}

.clearfix::after {
    content: "";
    display: table;
    clear: both;
}

Flexbox - المقدمة

ما هو Flexbox؟

Flexbox هو نظام تخطيط أحادي البعد لترتيب العناصر في صف أو عمود.

.container {
    display: flex;
}

Flexbox Properties

خصائص Flexbox:

.container {
    display: flex;
    justify-content: center;  /* المحور الرئيسي */
    align-items: center;  /* المحور الثانوي */
    gap: 20px;
}

.item {
    flex: 1;  /* ينمو لملء المساحة */
}

Flexbox Advanced

خصائص متقدمة:

.container {
    display: flex;
    flex-direction: row;  /* أو column */
    flex-wrap: wrap;
    align-content: space-between;
}

.item {
    flex-grow: 1;
    flex-shrink: 1;
    flex-basis: 200px;
}

CSS Grid - المقدمة

ما هو CSS Grid؟

CSS Grid هو نظام تخطيط ثنائي الأبعاد لترتيب العناصر في صفوف وأعمدة.

.container {
    display: grid;
}

Grid Properties

خصائص Grid:

.container {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: 100px 200px;
    gap: 20px;
}

.item {
    grid-column: 1 / 3;
    grid-row: 1 / 2;
}

Grid Areas

Grid Template Areas:

.container {
    display: grid;
    grid-template-areas:
        "header header header"
        "sidebar main main"
        "footer footer footer";
}

.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }

الحركات

Transition:

button {
    background: blue;
    transition: background 0.3s, transform 0.3s;
}

button:hover {
    background: red;
    transform: scale(1.1);
}

التحويلات

Transform:

div {
    transform: translateX(50px);
    transform: translateY(50px);
    transform: rotate(45deg);
    transform: scale(1.5);
    transform: skew(10deg);
    
    /* دمج التحويلات */
    transform: translate(50px, 50px) rotate(45deg) scale(1.5);
}

Animations

@keyframes:

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

@keyframes slide {
    0% { transform: translateX(0); }
    50% { transform: translateX(100px); }
    100% { transform: translateX(0); }
}

.element {
    animation: fadeIn 1s ease-in-out;
    animation: slide 2s infinite;
}

Media Queries

Responsive Design:

/* Mobile First */
.container {
    width: 100%;
}

/* Tablet */
@media (min-width: 768px) {
    .container {
        width: 750px;
    }
}

/* Desktop */
@media (min-width: 1024px) {
    .container {
        width: 1200px;
    }
}

Pseudo-classes

Pseudo-classes شائعة:

a:hover { color: red; }
a:active { color: blue; }
a:focus { outline: 2px solid green; }
a:visited { color: purple; }

input:checked { background: green; }
input:disabled { opacity: 0.5; }

p:first-child { font-weight: bold; }
p:last-child { margin-bottom: 0; }
p:nth-child(2) { color: blue; }

Pseudo-elements

::before و ::after:

p::before {
    content: "→ ";
    color: blue;
}

p::after {
    content: " ←";
    color: red;
}

p::first-line {
    font-weight: bold;
}

p::first-letter {
    font-size: 2em;
    color: red;
}

Variables

CSS Custom Properties:

:root {
    --primary-color: #007bff;
    --secondary-color: #6c757d;
    --font-size: 16px;
}

body {
    color: var(--primary-color);
    font-size: var(--font-size);
}

.button {
    background: var(--primary-color);
}

مشروع صفحة متجاوبة

صفحة متجاوبة بسيطة:

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
}

.row {
    display: flex;
    flex-wrap: wrap;
    margin: -10px;
}

.col {
    flex: 1;
    padding: 10px;
}

@media (max-width: 768px) {
    .col {
        flex: 100%;
    }
}

مشروع Dashboard

تخطيط Dashboard:

.dashboard {
    display: grid;
    grid-template-areas:
        "header header header"
        "sidebar main main"
        "footer footer footer";
    grid-template-columns: 250px 1fr 1fr;
    grid-template-rows: 60px 1fr 40px;
    height: 100vh;
}

.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }

مشروع Landing Page

تصميم Landing Page:

.hero {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    color: white;
}

.hero h1 {
    font-size: 3em;
    animation: fadeIn 1s;
}

.features {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 30px;
    padding: 50px 20px;
}

Best Practices

أفضل الممارسات:

  • استخدم BEM naming convention
  • نظم CSS في ملفات منفصلة
  • استخدم CSS Variables
  • اكتب CSS mobile-first
  • استخدم Flexbox و Grid بدلاً من Float
  • قلل من استخدام !important
  • استخدم shorthand properties

أدوات التطوير

Browser DevTools:

  • Inspect Element: F12 أو Right-click → Inspect
  • Console: لاختبار JavaScript
  • Network Tab: لمراقبة الطلبات
  • Responsive Mode: لاختبار التصميم المتجاوب
أدوات مفيدة:
  • Chrome DevTools
  • Firefox Developer Tools
  • VS Code Extensions
  • CSS Validator

مشروع موقع كامل

موقع كامل مع CSS:

/* Reset */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

/* Variables */
:root {
    --primary: #007bff;
    --secondary: #6c757d;
    --success: #28a745;
    --font-main: 'Arial', sans-serif;
}

/* Layout */
body {
    font-family: var(--font-main);
    line-height: 1.6;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 20px;
}

/* Header */
header {
    background: var(--primary);
    color: white;
    padding: 1rem 0;
}

/* Navigation */
nav ul {
    list-style: none;
    display: flex;
    gap: 20px;
}

/* Main Content */
main {
    display: grid;
    grid-template-columns: 1fr 3fr;
    gap: 30px;
    padding: 30px 0;
}

/* Footer */
footer {
    background: #333;
    color: white;
    text-align: center;
    padding: 20px 0;
}

/* Responsive */
@media (max-width: 768px) {
    main {
        grid-template-columns: 1fr;
    }
}