ProductPromotion
Logo

0x3d.Site

is designed for aggregating information.

Structuring Your Electron App with HTML, CSS, and JavaScript

Welcome back to the second lesson of your Electron journey! Last time, we got a simple 'Hello, World!' app up and running—pretty cool, right? Well, now it’s time to take things up a notch. In this tutorial, we’ll dive into how to add structure to your app using HTML, CSS, and JavaScript.

Here’s the beauty of Electron: you can use the same front-end tools (HTML, CSS, JS) that you’ve already worked with when building websites. This time, instead of living in the browser, your code will power a desktop app. Yes, we’re taking the web from Chrome, Firefox, or Safari, and planting it firmly onto your computer's desktop. Let’s get into it!

What You’ll Learn:

  • How to build a basic user interface using HTML
  • How to style your app with CSS
  • How to add functionality using JavaScript

Key Takeaway:

You’re not just building for browsers anymore. The web is expanding onto desktops with Electron, and you're driving the ship.

Step 1: Setting Up Your Project Files

Before we get into the code, let’s quickly revisit the structure of your Electron app. Here’s a simplified version of what your project folder should look like after the last tutorial:

my-electron-app/
    ├── main.js
    ├── index.html
    ├── package.json
    └── node_modules/

If you already have this, great! Now, we’re going to add a little more structure. Create two new folders:

my-electron-app/
    ├── assets/
    │   ├── styles.css
    │   └── script.js
    ├── main.js
    ├── index.html
    ├── package.json
    └── node_modules/
  • assets/: This will hold your styles and scripts. It’s good practice to keep things organized in separate folders.

  • styles.css: The file where you’ll add your CSS to make your app look good.

  • script.js: The file where you’ll write JavaScript to make the app interactive.

Let’s start by setting up these files and linking them together.

Step 2: Writing Your HTML Structure

First, we’ll focus on index.html. In this step, we’ll create the basic HTML structure for your app’s user interface (UI). Open index.html and replace the content with the following code:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Electron App</title>
    <link rel="stylesheet" href="assets/styles.css">
  </head>
  <body>
    <header>
      <h1>My Cool Electron App</h1>
    </header>

    <main>
      <button id="magicBtn">Click me for magic!</button>
      <p id="message"></p>
    </main>

    <script src="assets/script.js"></script>
  </body>
</html>

Breaking Down the HTML

  • <head>: Here, we include the metadata for the app. Most importantly, we link the CSS file (assets/styles.css) to style the app, and we set up the character encoding and responsive design.

  • <body>: This contains the actual content of your app. We have a header with a title, a button, and a paragraph (<p>) where we’ll display some text when the button is clicked. This will be handled by JavaScript, which we’ll link at the bottom.

  • <script>: At the end of the HTML document, we link the JavaScript file (assets/script.js) that will make our button do something.

This is a basic structure, but it’s clean and easy to extend later. You could add more sections, buttons, images, or forms as needed.

Step 3: Adding Style with CSS

Next, let’s make the app look better with some CSS. Open the styles.css file inside the assets folder and add the following code:

/* General Reset */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: Arial, sans-serif;
  background-color: #f4f4f4;
  color: #333;
  text-align: center;
  padding: 20px;
}

header {
  background-color: #6200ea;
  color: white;
  padding: 20px;
  margin-bottom: 20px;
}

h1 {
  font-size: 2em;
}

main {
  margin: 20px 0;
}

button {
  padding: 10px 20px;
  background-color: #6200ea;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 1em;
}

button:hover {
  background-color: #3700b3;
}

p {
  margin-top: 20px;
  font-size: 1.2em;
}

Breaking Down the CSS

  • Reset Styles: We start by resetting the margin and padding for all elements to ensure consistent spacing across different browsers.

  • Body Styling: We set a background color, font, and some basic text styling for the body of the app.

  • Header: The header gets a nice purple background with white text. We also give it some padding to space it out.

  • Buttons: Buttons are styled with a matching purple color, white text, and rounded corners. When the user hovers over the button, the background color gets darker for a cool effect.

  • Paragraph: The paragraph where our message will be displayed is styled with a larger font size.

Step 4: Adding Interactivity with JavaScript

Finally, let’s add some basic functionality with JavaScript. Open script.js inside the assets folder and add the following code:

// Get the button and message elements
const magicBtn = document.getElementById('magicBtn');
const message = document.getElementById('message');

// Add an event listener to the button
magicBtn.addEventListener('click', () => {
  message.textContent = '✨ Ta-da! Magic happened! ✨';
});

Breaking Down the JavaScript

  • DOM Selection: We use document.getElementById to select both the button (magicBtn) and the paragraph (message).

  • Event Listener: We add an event listener to the button so that when the user clicks it, the paragraph’s text is updated with a magical message: "✨ Ta-da! Magic happened! ✨".

This is a simple script, but it’s a good introduction to how Electron works with DOM manipulation and interactivity. You can now build on this, adding more complex logic and interaction.

Step 5: Running Your Updated App

Now that we’ve updated our HTML, CSS, and JavaScript, it’s time to see it in action. Open your terminal, navigate to your project folder (my-electron-app), and run:

npx electron .

If everything is set up correctly, your Electron app should open with a sleek new look, a button, and some interactive functionality. When you click the button, you should see the message "✨ Ta-da! Magic happened! ✨" appear below it.

Step 6: Next Steps and Ideas for Expansion

Now that you’ve structured your app with HTML, CSS, and JavaScript, the possibilities are endless. You’ve created the foundation for a desktop app, and you can now start thinking about adding more complex features. Here are a few ideas:

  1. Add More Interactions: Maybe add more buttons or a form for user input. Play around with different JavaScript events.

  2. Include More Styling: Make your app visually appealing with advanced CSS features like transitions, animations, or even CSS frameworks like Bootstrap or TailwindCSS.

  3. Experiment with Layouts: Use CSS Grid or Flexbox to create more advanced layouts, such as a sidebar or multi-column structure.

  4. Add More Pages: You could extend your app by adding multiple pages. For example, you might want a settings page or an about page. This can be done using HTML and JavaScript routing within Electron.

Conclusion

Congratulations! You’ve just learned how to structure an Electron app using HTML, CSS, and JavaScript. The great thing about Electron is that it allows you to use the web technologies you already know to build desktop applications. With a bit of creativity and some additional JavaScript, you can take this app to the next level.

In this lesson, you built a simple interface, styled it with CSS, and added some functionality with JavaScript. And this is just the beginning! With what you’ve learned so far, you have all the tools needed to start creating more complex, user-friendly Electron apps.

In the next lesson, we’ll dive into communication between the frontend and backend, so your app can start doing some real-world tasks. Keep going—you’re doing great!

Electron for Desktop Apps

Learn how to build desktop apps with our easy mini-course on Electron! In five simple tutorials, you’ll set up your environment, design your app using HTML, CSS, and JavaScript, and make it more interactive with notifications and system tray features. Perfect for beginners and web developers, this course will help you turn your web skills into real desktop applications. Start building your first Electron app today!

Questions & Answers

to widen your perspective.

Tools

available to use.

Providers

to have an visit.

Resouces

to browse on more.
0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory