Lesson 20 / الدرس 20

Dark mode / الوضع الداكن

One prefix in Tailwind, one attribute in Bootstrap, and a set of design decisions that neither of them makes for you — because a dark theme is not a light one inverted.

بادئة واحدة في Tailwind وسمة واحدة في Bootstrap، ومجموعة قرارات تصميم لا يتخذها أي منهما عنك — لأن السمة الداكنة ليست الفاتحة مقلوبة.

TAILWIND — a prefix, like a breakpoint
  <div class="bg-white text-slate-900 dark:bg-slate-900 dark:text-slate-100">

BOOTSTRAP — an attribute on the html element
  <html data-bs-theme="dark">
  Every component follows automatically.
Bootstrap's is less work and less control: one attribute themes everything. Tailwind's is more work and complete control: you state both colours everywhere, which is verbose and means nothing is ever a surprise. Run the demonstration for what changes.

Four things that do not simply invert

  1. Shadows stop working. A shadow is the absence of light, and there is little left to remove on a dark surface. Lift a panel with a lighter background and a visible border instead.
  2. Pure black and pure white are both wrong. White text on pure black vibrates and is tiring to read; near-black and near-white are what every good dark theme uses.
  3. Saturated colours get brighter. A blue that reads calm on white glares on near-black. Dark themes generally want the same hue, lighter and less saturated.
  4. Images need attention. A logo that is black on transparent disappears. So does a screenshot of a white interface, which glows like a torch.

Following the reader, and letting them choose

The operating system already knows which the reader prefers, and CSS can ask: prefers-color-scheme, from the CSS course. Both frameworks use it by default, so a page can respect the reader's choice with no toggle and no JavaScript at all. Adding a toggle is a real feature — it needs somewhere to remember the choice — and it is worth doing only after the automatic version works.

A practical way to keep this manageable in Tailwind, where you would otherwise write dark: on hundreds of elements: define your colours as semantic names in the theme — surface, text, muted, border — and let those switch. Then a card is bg-surface text-text in both modes and the theme decides what those mean. That is how this site is built, and it is why its dark mode is one block of tokens rather than a thousand prefixes.

Try it live / جرّب بنفسك

Preview / المعاينة

Check yourself / اختبر نفسك

1. Why do shadows stop working in dark mode?

2. Why is white text on pure black a poor choice?

3. Does checking contrast in light mode tell you anything about dark mode?

Your task / مهمتك

Give one page a dark mode in both frameworks, following the reader's system preference with no toggle. Then audit it: list every text-on-background pair and measure its contrast in both themes, and find at least one thing that needed more than inverting — a shadow, an image, or a saturated colour. Say what you did about it.

أعطِ صفحة واحدة وضعًا داكنًا في الإطارين، متبعًا تفضيل نظام القارئ بلا مبدّل. ثم دقّقها: عدّد كل زوج نص-على-خلفية وقِس تباينه في السمتين، واعثر على شيء واحد على الأقل احتاج أكثر من القلب — ظل أو صورة أو لون مشبع. وقل ما فعلته بشأنه.

  • Dark mode following the system preference, in both وضع داكن يتبع تفضيل النظام، في الاثنين
  • Every pair measured in BOTH themes كل زوج مقيس في السمتين
  • At least one thing that needed more than inverting شيء واحد على الأقل احتاج أكثر من القلب
  • No pure black and no pure white لا أسود خالص ولا أبيض خالص
How do you want to submit? / كيف تريد التسليم؟