أساسيات الوضع الداكن والفاتح
جزء من قسم Practical Frontend في رحلة HTML على Coddy. الدرس 13 من 35.
يُعدّ الوضع الداكن/الفاتح ميزة شائعة تتيح للمستخدمين اختيار مخطط الألوان المفضّل لديهم. لننفّذ مفتاح تبديل أساسيًا للوضع الداكن/الفاتح باستخدام متغيرات CSS.
أولًا، عرّف متغيرات ألوان الوضع الفاتح في العنصر الجذر وطبّقها على العناصر المختلفة:
: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 {
--background-color: #333333;
--text-color: #f5f5f5;
--heading-color: #ffffff;
}الآن، تتحكم الفئة .dark-mode في السمة، وعند إضافة هذه الفئة إلى <body>، تتغير المتغيرات وتُحدّث جميع العناصر التي تستخدمها تلقائيًا.
<body class="dark-mode">
<!-- محتوى صفحتك هنا -->
</body>للتبديل بين الأوضاع ديناميكيًا، نحتاج إلى قليل من المساعدة من لغة أخرى تُسمى JavaScript (JS) لإضافة أو إزالة الفئة .dark-mode. في هذا الدرس، يمكنك اختبار ذلك يدويًا بإضافة الفئة أو إزالتها من علامة <body>.
التحدي
سهلمهمتك:
- أضف فئة
.dark-modeداخل قسم<style>. - تجاوز متغيرات CSS لتعيين ألوان داكنة لـ background وtext وheadings وbutton.
- اختبر ذلك بإضافة
class="dark-mode"إلى وسم<body>.
جرّب بنفسك
<!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)تحدي المراجعةتدرّب بنفسك: Playground لـ Web