Skip to content
the poor coder

difference between styling app.js and index

Poor Man
Published
Reading time
2 min read
On this page

Difference between styling app.js and index.html#

When it comes to styling web applications, there are two main ways to do it - by writing CSS code in a separate file and linking it to the HTML file, or by using JavaScript to manipulate the CSS styles directly. In the context of React applications, where we use JSX syntax and create components, the two main files we work with are usually "app.js" and "index.html".

Styling with CSS#

The most common way to style web applications is by writing CSS code in a separate file and linking it to the HTML file. This is usually done in the "index.html" file, where we add a link to our CSS file in the head section:

Text
<head>
  <link rel="stylesheet" href="styles.css">
</head>

Then we can write our CSS code in the "styles.css" file and use selectors to target specific HTML elements and apply styles to them. For example:

Text
body {
  background-color: #f5f5f5;
}

h1 {
  font-size: 2rem;
  color: #333;
}

This will set the background color of the body element to light gray and change the font size and color of all h1 elements.

Styling with JavaScript#

Another way to style web applications is by using JavaScript to manipulate the CSS styles directly. This can be done in the "app.js" file, where we can access the DOM elements and modify their styles using the style property:

Text
const myHeading = document.querySelector('h1');
myHeading.style.fontSize = '2rem';
myHeading.style.color = '#333';

This will select the first h1 element on the page and set its font size and color to the values specified.

While both methods can achieve the same result, there are some differences to consider. Using CSS allows us to separate our styling from our logic, making it easier to maintain and reuse styles across multiple pages. On the other hand, using JavaScript can be more flexible in certain situations, such as when we need to dynamically change styles based on user input or other events.

Ultimately, the choice between CSS and JavaScript for styling will depend on the specific needs of your application and your personal preferences as a developer.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC