What could be causing the lack of styling in my Express application?

The styles are not rendering correctly on the specified route: 'http://localhost:5000/exams/add-new'

However, they are working fine on the following routes:

  • 'http://localhost:5000'
  • 'http://localhost:5000/exams'
  • 'http://localhost:5000/about'

The CSS file is located in the assets folder: assets/css/styles.css

This is the server.js configuration:

const express = require('express')
const path = require('path')
const app = express()

app.set('view engine', 'ejs')

app.use('/css', express.static(path.resolve(__dirname, 'assets/css')))

app.use('/', require('./server/routes/router'))

app.listen(5000)

Here is the code snippet from index.ejs in the views folder:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="css/styles.css" />
    <title>Home Page</title>
  </head>
  <body>
    <header>
      <div>
        <nav>
          <h1 class="logo">
            <a href="/">B<span>log</span></a>
          </h1>
          <ul>
            <li><a href="/">Home</a></li>
            <li><a href="/exams">Exams</a></li>
            <li><a href="/about">About</a></li>
          </ul>
        </nav>
      </div>
    </header>

    <main>
     <h1>Home Page</h1>
    </main>

  </body>
</html>

And here is the code for the exams page:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="css/styles.css" />
    <title>Home Page</title>
  </head>
  <body>
    <header>
      <div>
        <nav>
          <h1 class="logo">
            <a href="/">B<span>log</span></a>
          </h1>
          <ul>
            <li><a href="/">Home</a></li>
            <li><a href="/exams">Exams</a></li>
            <li><a href="/">About</a></li>
          </ul>
        </nav>
      </div>
    </header>

    <main>
      <div class="addNewQuestionsButton">
        <a href="/exams/add-new-questions">
          <span>Add new questions</span>
        </a>
      </div>
    </main>

  </body>
</html>

Please help me understand why this issue is occurring and what steps can be taken to resolve it.

Answer №1

Ensure that the CSS in your generated HTML is linked from the following path:

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

rather than

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

This approach will maintain uniformity regardless of the path of your current URL

Answer №2

To include CSS in HTML, specify the precise file path:

assets/css/styles.css

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Menu collapse alignment upon hovering

I've spent over an hour grappling with this problem and can't seem to find a solution. Whenever the navigation menu collapses and I hover my mouse over the SHOP item, it shifts the SHOP element to the left. When I move the cursor away, it return ...

Initiate a cascade delete action without physically removing the data

Is there a method to reset a User record similar to a factory reset without actually removing the user record, but still triggering the cascading delete process? ...

Divs that can be sorted are being shifted downwards within the swim lanes

JavaScript code snippet here... CSS code snippet here... HTML code snippet here... ...

Ways to expand flexbox to occupy the entire container

I have a scenario where I want two flexboxes in a container to fill the entire height of the container. The left flexbox should display an image that fills its entire space, while the right flexbox contains other content. However, I'm facing difficult ...

Tips on customizing styles for Material UI components using CSS override techniques

Currently, I am tackling a project that requires the use of Material UI components. To simplify things, let's focus on a basic functional component that includes two Material UI elements: a Button and a Text Field. import React from 'react'; ...

The flow of Express middleware

Imagine you have the following setup for an Express app: const express = require('express') const app = express() app.use((req, res, next) => { console.log('\n\nALWAYS') next() }) app.get('/a', (req, res) ...

Establishing the Time-to-Live (TTL) with Redis-OM and Node Object Mapping

Recently, I stumbled upon the exciting new Redis-OM Node Object Mapping feature. Although I have limited experience with Redis, I am eager to dive into it now. Currently, I have a basic function in place for creating rooms but I want these rooms to automa ...

What's the best way to position the <li> element beneath the hamburger icon?

Is there a way to place the list element 'Home' below the hamburger icon and logo in Bootstrap 4 in mobile mode? I attempted to display the logo as style="display: block" with no success. <div class="container"> <nav class="navbar nav ...

Highlight the active link in the parent node changes color when the child node is in focus

I've designed a CSS vertical menu with the code provided below. You can view the test page at . The issue I'm facing is that when hovering over the sub-menu items, the highlighted 'li' in the parent node reverts back to the non-hover c ...

Unique approaches: Custom layouts compared to a single adaptive layout for managing various device sizes and orientations

When designing layouts for desktop, tablet, and smartphone in both portrait and landscape orientations within a Single Page Application, is it more effective to separate the layouts as different resources and load them dynamically based on device detection ...

Angular Material - truncating selected text in a list

I'm having trouble implementing the Angular Material list with checkboxes, where the text needs to be truncated instead of word-wrapped due to limited UI space. I have modified an example on the Angular Material site to demonstrate the issue. The text ...

The scroll feature in JavaScript is malfunctioning

After countless hours of troubleshooting, I still can't figure out why the code snippet below is not working properly on my website at : <script> $(window).scroll(function () { if ($(window).scrollTop() > 400) { ...

What causes a globally declared array to remain empty in the global scope even after data is pushed to it within a callback function?

Initially, I set an empty array as the value for the global variable artistURLs. Then, within the Cheerio .each() iterator method, I push strings (stored in the local variable artistURL) into the artistURLs array. var request = require('request&apos ...

Using jQuery UI to create sortable lists that are connected and have hidden overflow features

How can I hide overflow from two fixed-height divs containing sortable lists connected to each other without affecting their connection? For example, if overflow is set to hidden, the list item doesn't display when dragged outside of the div. I' ...

Maximizing Content Width in FullCalendar v5 to Ensure Screen Compatibility

As I develop a calendar and timeline view using fullcalendar v5, my clients are requesting a month view that displays the entire month without any scroll bars. While I am aware of setting the contentHeight to auto, there seems to be no option for adjusting ...

Utilize socket.io to relay GET data from API to clients through broadcasting

I'm uncertain if my current approach is the best method as I can't find any examples to follow. My application features a Google map with multiple objects displayed on it. I aim to automatically update the objects' locations every couple of ...

What is the best way to send FormData from a React JS axios post request to a Node server?

I am facing an issue where the form data is not reaching the node server even though it shows in the network payload at the time of request. Below are the snippets from different files involved in the process. let formData = new FormData(); // fo ...

Viewing at full width on mobile exceeds 100% [React]

I'm attempting to make a div cover exactly 100% of the width on mobile devices, but no more. The code I'm using is as follows: Here is my React code: <div className="max-width-100vw"> HELLO I AM A UNIQUE SENTENCE HERE??? </div> And ...

Font-family declaration in CSS is unresponsive to button element

I've encountered an issue with changing the font of a button using CSS. Despite including the necessary declaration, the font does not seem to update as expected. After conducting some research on this problem: I came across this post, but unfortuna ...

Encountered an issue with dependency resolution while working with Redux

Despite attempting to use both --force and --legacy-peer-deps, I am still unable to resolve the issue. Currently running [email protected] on macOS. hiteshagarwal@Hiteshs-MBP kuber % npm i (node:48924) ExperimentalWarning: The fs.promises API is exp ...