0Pricing
Electron Desktop App Development · 课时

集成 HTML、CSS 与 JavaScript

了解标准 Web 技术如何构成 Electron 用户界面的基础,从而运用您已有的 Web 开发经验

集成 HTML、CSS 与 JavaScript 是 CoddyKit 上的免费 Electron Desktop App Development 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Electron Desktop App Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Electron Desktop App Development 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Web UI with HTML, CSS, JS

Welcome! In Electron, you build desktop app interfaces using the same technologies you'd use for a website: HTML, CSS, and JavaScript.

This means your existing web development skills are directly transferable. Let's see how!

Meet the Renderer Process

An Electron app has a Main Process and one or more Renderer Processes.

  • The Main Process handles native OS interactions.
  • The Renderer Process is essentially a Chromium web browser instance. It's where your HTML, CSS, and JavaScript run, forming your app's user interface.

Think of each Electron window as its own renderer process.

HTML for Structure & Content

HTML (HyperText Markup Language) is the backbone of any web page, including your Electron app's UI.

It defines the structure and content:

  • Headings (<h1>, <h2>)
  • Paragraphs (<p>)
  • Buttons (<button>)

It's like the blueprint for your app's layout.

Displaying HTML Content

Your Electron main process loads an HTML file into a renderer process (a new window). This HTML file becomes your app's initial view.

Here's how `main.js` loads a simple `index.html` file:

The `index.html` might contain:<h1>Hello Electron!</h1><p>My first desktop UI.</p>

const { app, BrowserWindow } = require('electron');
const path = require('path');

function createWindow () {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: false,
      contextIsolation: true
    }
  });
  // Load the index.html file
  win.loadFile(path.join(__dirname, 'index.html'));
}

app.whenReady().then(createWindow);

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow();
  }
});

CSS for Styling Your UI

CSS (Cascading Style Sheets) is used to control the presentation of your HTML content. It dictates how your app looks.

With CSS, you can define:

  • Colors and backgrounds
  • Fonts and text sizes
  • Layout (margins, padding, positioning)

It turns your HTML blueprint into a visually appealing interface.

Linking & Applying Styles

You link a CSS file to your HTML using a <link> tag in the <head> section of your `index.html`.

For example, to style the previous `h1` element:

Your `index.html` would include:<link rel="stylesheet" href="style.css">

And `style.css` could contain:h1 { color: #3366ff; text-align: center; }

const { app, BrowserWindow } = require('electron');
const path = require('path');

function createWindow () {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: false,
      contextIsolation: true
    }
  });
  win.loadFile(path.join(__dirname, 'index.html'));
}

app.whenReady().then(createWindow);

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow();
  }
});

JavaScript for Interactivity

JavaScript (JS) adds dynamic behavior and interactivity to your Electron app's UI.

It allows you to:

  • Respond to user actions (clicks, key presses)
  • Manipulate HTML content and CSS styles
  • Fetch data from APIs

JS turns a static page into a dynamic, responsive application.

Implementing UI Logic with JS

You can embed JavaScript directly in your HTML using <script> tags or link external .js files.

Let's add a button and make it display an alert when clicked:

Your `index.html` would include:<button id="myButton">Click Me!</button><script>document.getElementById('myButton').addEventListener('click', () => { alert('Hello from Electron!'); });</script>

const { app, BrowserWindow } = require('electron');
const path = require('path');

function createWindow () {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: false,
      contextIsolation: true
    }
  });
  win.loadFile(path.join(__dirname, 'index.html'));
}

app.whenReady().then(createWindow);

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow();
  }
});

HTML, CSS, JS: A Unified Stack

Together, HTML, CSS, and JavaScript form a powerful and familiar stack for building Electron user interfaces.

  • HTML provides the content and structure.
  • CSS defines the visual presentation.
  • JavaScript adds interactivity and dynamic behavior.

This allows web developers to leverage their existing knowledge to create robust desktop applications.

Web Tech Roles

Which of the following statements correctly describe the role of HTML, CSS, and JavaScript in an Electron renderer process?

Recap: Web UI Basics

Today, we learned that Electron uses familiar web technologies – HTML, CSS, and JavaScript – to build desktop application user interfaces.

  • The renderer process is where these web technologies run.
  • HTML structures the content.
  • CSS styles the appearance.
  • JavaScript adds dynamic behavior.

You now understand the fundamental building blocks of an Electron UI. Next, we'll explore how to integrate popular frontend frameworks.

常见问题解答

「集成 HTML、CSS 与 JavaScript」课时是免费的吗?

是的 — 「集成 HTML、CSS 与 JavaScript」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Electron Desktop App Development 课程的其余内容,请升级到 CoddyKit PRO。 Electron Desktop App Development 课程共包含 4 节课。

「集成 HTML、CSS 与 JavaScript」这节课中我会学到什么?

了解标准 Web 技术如何构成 Electron 用户界面的基础,从而运用您已有的 Web 开发经验 你通过在浏览器中直接运行的动手代码来练习 Electron Desktop App Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Electron Desktop App Development 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Electron Desktop App Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「集成 HTML、CSS 与 JavaScript」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Electron Desktop App Development 课中编写并运行代码吗?

能。每节 Electron Desktop App Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 集成 HTML、CSS 与 JavaScript
  2. 使用前端框架
  3. 响应式桌面设计
  4. 主题与深色模式支持
← 返回 Electron Desktop App Development