أساسيات الوضع الداكن والفاتح
جزء من قسم Practical Frontend في رحلة HTML على Coddy — الدرس 13 من 35.
يعد الوضع الداكن/الفاتح ميزة شائعة تتيح للمستخدمين اختيار نظام الألوان المفضل لديهم. لنقم بتنفيذ مفتاح تبديل أساسي للوضع الداكن/الفاتح باستخدام متغيرات CSS.
أولاً، قم بتعريف متغيرات ألوان الوضع الفاتح (light mode) في العنصر الجذري (root element) وقم بتطبيقها على العناصر المختلفة:
:root {
--background-color: #ffffff;
--text-color: #333333;
--heading-color: #000000;
}
body {
background-color: var(--background-color);
color: var(--text-color);
}
h1, h2, h3 {
color: var(--heading-color);
}بعد ذلك، قم بإنشاء فئة للوضع الداكن (dark mode class) ستقوم بتجاوز هذه المتغيرات:
.dark-mode {
--background-color: #333333;
--text-color: #f5f5f5;
--heading-color: #ffffff;
}الآن، تتحكم الفئة .dark-mode في السمة — فعند إضافة هذه الفئة إلى <body>، تتغير المتغيرات وتتحدث جميع العناصر التي تستخدمها تلقائيًا.
<body class="dark-mode">
<!-- محتوى صفحتك هنا -->
</body>للتبديل بين الأوضاع ديناميكيًا، نحتاج إلى القليل من المساعدة من لغة أخرى تسمى جافا سكريبت (JS) لإضافة أو إزالة فئة .dark-mode. في هذا الدرس، يمكنك اختبار ذلك يدويًا عن طريق إضافة أو إزالة الفئة في وسم <body>.
التحدي
سهلمهمتك:
- أضف فئة
.dark-modeداخل قسم<style>. - قم بتجاوز متغيرات CSS لتعيين ألوان داكنة للخلفية، والنص، والعناوين، والأزرار.
- اختبرها عن طريق إضافة
class="dark-mode"إلى وسم<body>.
ورقة مرجعية
استخدم متغيرات CSS لتنفيذ سمات الوضع الداكن/الفاتح. قم بتعريف متغيرات الوضع الفاتح في :root:
:root {
--background-color: #ffffff;
--text-color: #333333;
--heading-color: #000000;
}قم بتطبيق المتغيرات على العناصر:
body {
background-color: var(--background-color);
color: var(--text-color);
}
h1, h2, h3 {
color: var(--heading-color);
}قم بإنشاء فئة (class) للوضع الداكن تقوم بتجاوز المتغيرات:
.dark-mode {
--background-color: #333333;
--text-color: #f5f5f5;
--heading-color: #ffffff;
}قم بتبديل السمات عن طريق إضافة/إزالة فئة .dark-mode إلى عنصر <body>:
<body class="dark-mode">جرّب بنفسك
<!DOCTYPE html>
<html>
<head>
<title>Dark/Light Mode Basics</title>
<style>
/* Default light theme */
:root {
--background-color: #f0f4f8;
--text-color: #222222;
--heading-color: #003366;
--button-bg: #ffcc00;
--button-text: #000000;
}
body {
background-color: var(--background-color);
color: var(--text-color);
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
header {
background-color: var(--heading-color);
color: white;
padding: 2rem;
text-align: center;
}
header h1 {
margin: 0;
font-size: 2rem;
}
main {
padding: 2rem;
}
h2 {
color: var(--heading-color);
margin-top: 1.5rem;
}
p {
line-height: 1.6;
}
.button {
display: inline-block;
margin-top: 1rem;
padding: 0.75rem 1.5rem;
background-color: var(--button-bg);
color: var(--button-text);
border: none;
border-radius: 5px;
text-decoration: none;
font-weight: bold;
cursor: pointer;
}
footer {
background-color: var(--heading-color);
color: white;
text-align: center;
padding: 1rem;
margin-top: 2rem;
}
</style>
</head>
<body>
<header>
<h1>Olympic Games 2025</h1>
<p>Celebrating sports, unity, and excellence</p>
</header>
<main>
<h2>Top Sports</h2>
<p>From track and field to swimming and gymnastics, the Olympics showcase the best athletes in the world.</p>
<a href="#" class="button">See Medal Winners</a>
<h2>Host Cities</h2>
<p>Explore the exciting host cities where athletes come together to compete at the highest level.</p>
<a href="#" class="button">Learn More</a>
<h2>History</h2>
<p>The Olympic Games have a rich history dating back to ancient Greece, promoting peace, friendship, and fair competition.</p>
</main>
<footer>
© 2025 Olympic Games. All rights reserved.
</footer>
</body>
</html>
يتضمن هذا الدرس اختبارًا قصيرًا. ابدأ الدرس للإجابة عليه وتتبّع تقدمك.
جميع دروس Practical Frontend
2إستراتيجية الهاتف المحمول أولاً
ماذا يعني "الهاتف المحمول أولاً"تنسيق النصوص للهاتف المحمول أولاًالتنقل للهاتف المحمول أولاًالصور للهاتف المحمول أولاًالنماذج للهاتف المحمول أولاًتحدي المراجعة5مكونات واجهة المستخدم
القائمة المنسدلةعلامات التبويبالشاراتتلميحات الأدواتلافتات الإشعاراتتحدي المراجعة3السمات والأنماط المرئية
السمات في CSSأساسيات الوضع الداكن والفاتحألوان التمييز والإبرازتنسيق الخطوط (Typography)تحدي المراجعة