Motywy CSS i obsługa trybu ciemnego
Nadadzą Państwo spójny styl stronom popupu i opcji, obsłużą jasne i ciemne motywy oraz uwzględnią preferencje użytkownika.
Motywy CSS i obsługa trybu ciemnego to bezpłatna lekcja Browser Extensions Development (Chrome & Edge) na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Browser Extensions Development (Chrome & Edge), a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Browser Extensions Development (Chrome & Edge) zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
Why Theming Matters
Your popup and options pages are small windows into your extension. Consistent, polished styling builds trust and makes the extension feel native to the browser.
Supporting dark mode respects users who set their system to a dark appearance.
Linking a Stylesheet
Keep styles in a separate CSS file and link it from your popup HTML for clean separation.
<head>
<link rel="stylesheet" href="popup.css">
</head>CSS Custom Properties
Define your color palette once with CSS variables. Then every component references the variable instead of a hard-coded color.
:root {
--bg: #ffffff;
--text: #1a1a1a;
--accent: #3367d6;
}Applying the Variables
Use the variables across your layout so a single change updates the whole theme.
body {
background: var(--bg);
color: var(--text);
}
button {
background: var(--accent);
}Detecting System Dark Mode
The prefers-color-scheme media query tells you the user's system theme. Override your variables inside it.
@media (prefers-color-scheme: dark) {
:root {
--bg: #1e1e1e;
--text: #f5f5f5;
}
}Sizing the Popup
Popups size themselves to their content. Set an explicit width so the layout is predictable and not too cramped.
body {
width: 320px;
margin: 0;
font-family: system-ui, sans-serif;
}A Manual Theme Toggle
Let users override the system theme by adding a class to the root element from JavaScript.
document.documentElement.classList.toggle('dark', isDark)Styling the Manual Theme
Pair the toggle class with its own variable overrides, mirroring the media query.
.dark {
--bg: #1e1e1e;
--text: #f5f5f5;
}Persisting the Choice
Save the user's theme preference with the storage API so it survives popup reopens and browser restarts.
chrome.storage.sync.set({ theme: 'dark' })Reusing Styles Across Pages
Share one CSS file between your popup and options page to keep them visually consistent and avoid duplicating colors and spacing rules.
<link rel="stylesheet" href="../shared/theme.css">Accessibility Considerations
Ensure enough contrast between text and background in both themes, and never rely on color alone to convey meaning. Test with browser dev tools' contrast checker.
Quick Check
Check your theming knowledge.
Recap
You learned to theme extension UI:
- Centralize colors in CSS custom properties
- Detect dark mode with
prefers-color-scheme - Offer a manual toggle and persist it in storage
- Share styles across popup and options pages
- Keep contrast accessible
A polished, theme-aware UI makes your extension feel professional.
Często zadawane pytania
Czy lekcja „Motywy CSS i obsługa trybu ciemnego” jest bezpłatna?
Tak — pełny tekst „Motywy CSS i obsługa trybu ciemnego” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Browser Extensions Development (Chrome & Edge), przejdź na CoddyKit PRO. Kurs Browser Extensions Development (Chrome & Edge) zawiera 4 lekcji w sumie.
Co nauczysz się w „Motywy CSS i obsługa trybu ciemnego”?
Nadadzą Państwo spójny styl stronom popupu i opcji, obsłużą jasne i ciemne motywy oraz uwzględnią preferencje użytkownika. Ćwiczysz Browser Extensions Development (Chrome & Edge) z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Browser Extensions Development (Chrome & Edge)?
Nie wymagamy żadnego doświadczenia. Browser Extensions Development (Chrome & Edge) w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Motywy CSS i obsługa trybu ciemnego”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Browser Extensions Development (Chrome & Edge)?
Tak. Każda lekcja Browser Extensions Development (Chrome & Edge) zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Tworzenie interaktywnego interfejsu popup
- Tworzenie strony opcji
- Komunikacja między komponentami interfejsu
- Motywy CSS i obsługa trybu ciemnego