CSS and styling best practices for Electron desktop applications — theming, custom titlebar, typography, scrollbars, and DPI scaling
box-sizing: border-box/* Root theme variables */
:root,
[data-theme="light"] {
--bg-primary: #ffffff;
--bg-secondary: #f5f5f5;
--text-primary: #1a1a1a;
--text-secondary: #666666;
--accent: #0066cc;
--border: #e0e0e0;
}
[data-theme="dark"] {
--bg-primary: #1e1e1e;
--bg-secondary: #2d2d2d;
--text-primary: #f0f0f0;
--text-secondary: #999999;
--accent: #4da3ff;
--border: #404040;
}
Use data-theme attribute on the HTML root element, not prefers-color-scheme alone.
Main process (IPC):
import { nativeTheme, ipcMain } from 'electron';
ipcMain.handle('get-theme', () => nativeTheme.shouldUseDarkColors ? 'dark' : 'light');
ipcMain.on('set-theme', (_, theme: 'light' | 'dark') => {
nativeTheme.themeSource = theme;
});
Renderer (apply on load and on change):
document.documentElement.dataset.theme = await window.electronAPI.getTheme();
Use frame: false in BrowserWindow options, then build a custom titlebar in HTML.
.titlebar {
-webkit-app-region: drag; /* Makes it draggable */
height: 32px;
display: flex;
align-items: center;
}
/* All interactive elements inside must opt out of drag */
.titlebar button,
.titlebar select,
.titlebar input {
-webkit-app-region: no-drag;
}
body {
font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
font-size: 13px;
line-height: 1.4;
color: var(--text-primary);
}
.text-secondary {
font-size: 11px;
color: var(--text-secondary);
}
::-webkit-scrollbar {
width: 6px;
height: 6px;
}
::-webkit-scrollbar-track {
background: var(--bg-secondary);
}
::-webkit-scrollbar-thumb {
background: var(--border);
border-radius: 3px;
}
::-webkit-scrollbar-thumb:hover {
background: var(--text-secondary);
}
/* Interactive feedback: 100–150ms */
button {
transition: background-color 150ms ease, opacity 150ms ease;
}
/* Theme switch: 200ms */
body {
transition: background-color 200ms ease, color 200ms ease;
}
/* GPU-accelerate only opacity and transform */
.animated {
will-change: opacity, transform;
}
340×280new BrowserWindow({
width: 340,
height: 280,
frame: false,
resizable: false,
})