The margins on the ejs file and html file are inconsistent, while the background color is displaying as gray instead of white

I've noticed that the margins of my page appear differently when comparing the HTML file in the browser to the EJS file. My setup involves Node/Express and Bootstrap 4, with the page initially built using HTML. I aimed for the content to stretch from one side of the page to the other on the HTML version.

Upon inspecting the CSS code, it seems like I specified the width and height as well as setting the margin to zero to achieve the desired effect:

html,body {
  height: 100%;
  width: 100%;
}
{
    margin:0;
}
body {
    overflow-x: hidden;
} 

The corresponding HTML snippet creating a pink band across the screen looks as follows:

<div> 
  <div class="row"> 
     <div class="col-lg-12 pl-5 pr-5 pb-5 pt-5 text-light" style="font-size:30px; background-color: pink;">
         My Website
     </div>
  </div> 
</div> 

While this arrangement worked perfectly in HTML, converting the same page to an EJS format resulted in a strange white space of about 1.3cm between the div and the edge of the screen. Additionally, the white backgrounds turned into a pale gray shade inexplicably.

Despite not introducing any new CSS styles causing this color shift, the issue only affects EJS files and not the original HTML versions.

No additional external styling is present aside from a messages partial EJS file that abstains from styling information. Each main page such as the homepage or about page should be self-contained in terms of CSS and HTML.

This unexpected gray background remains unexplained, especially considering that Bootstrap 4 functionality appears to be intact following some initial hiccups with dropdown menus resolved by localizing the CSS file instead of relying on a CDN link – but the margin discrepancy persists independently of these changes.

Answer №1

It seems a bit unusual, so I decided to verify it with my Bootstrap 4 template. The pink background extends smoothly from the left edge to the right edge without any white spaces. Here is the complete template for your reference. It could be an issue related to software installation. Please ensure that Bootstrap, jQuery, and popper.js are correctly installed in your project. Feel free to reach out if you need further assistance.

<!DOCTYPE html>
<html lang="en">

<head>
    <!-- Required meta tags should always come first -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta http-equiv="x-ua-compatible" content="ie=edge">

    <!-- Bootstrap CSS -->

    <!-- build:css css/main.css -->
    <link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="node_modules/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="node_modules/bootstrap-social/bootstrap-social.css">
    <link rel="stylesheet" href="css/styles.css">
    <!-- endbuild -->

   
</head>

<body>

    <div class="container-fluid"> 
        <div class="row"> 
           <div class="col-lg-12 pl-5 pr-5 pb-5 pt-5 text-light" style="font-size:30px; background-color: pink;">
               My Website
           </div>
        </div> 
      </div> 

   
    
    <!-- Include jQuery, Popper.js, and then Bootstrap JS. -->
    <!-- build:js js/main.js -->
    <script src="node_modules/jquery/dist/jquery.slim.min.js"></script>
    <script src="node_modules/popper.js/dist/umd/popper.min.js"></script>
    <script src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
    <script src="js/scripts.js"></script>
    <!-- endbuild -->

    

</body>

</html>

Answer №2

Bootstrap offers two classes called "container" and "container-fluid." When you want the content to expand from left edge to right edge, you should use the class "container-fluid." The code provided below should help resolve this issue. Please confirm if everything is working as expected

<div class="container-fluid"> 
  <div class="row"> 
     <div class="col-lg-12 pl-5 pr-5 pb-5 pt-5 text-light" style="font-size:30px; background-color: pink;">
         My Website
     </div>
  </div> 
</div> 

Answer №3

Your solution seems to be working perfectly fine for me. Just ensure that your backend code is correctly written as shown below:

const express = require('express');
const ejs = require('ejs');
const app = express();
app.set('view engine', 'ejs');
app.use(express.static('public'));

app.get('/', (req, res)=>{
    res.render('home', {Title: 'Welcome'});
});

app.listen(3000, ()=>{
    console.log('Server running on port 3000');
});

Confirm that your EJS file resides in a directory named 'views' and your CSS file is located in a folder named 'public'

Answer №4

The issue was identified - it was caused by the express-ejs-layouts module that was installed and utilized. It slipped my mind that I had implemented this module, which is why I didn't reference it in the initial question. Initially, I underestimated the impact it would have on the layout, assuming that both the ejs and express-ejs-layouts modules were necessary for proper functionality of ejs. Frankly speaking, I lacked understanding of the role the express-ejs-layouts module played.

After removing

const expressLayouts = require('express-ejs-layouts')
and the app.use(expressLayouts) middleware, the ejs files now render correctly. The margins align perfectly with the page edges when using container-fluid, eliminating the pale gray background.

I should also note that implementing hang-coder's suggestion to arrange the stylesheets in the correct sequence resolved issues with the html files. Although this wasn't the main query, it addressed problems with margins not displaying properly when utilizing container-fluid. I mistakenly placed the Bootstrap scripts above the jQuery scripts. This incorrect ordering in the ejs files contributed to the margin display problem, which I rectified just before inquiring about the margins. This adjustment was made as a prerequisite for resolving another issue with the dropdown menu that surfaced prior to the margins concern, though I wasn't certain if it impacted the margin problem.

Hence, the root cause of the problem did not pertain to Mac screens or how Chrome browser renders on Macs, as initially presumed.

The following script displays accurate margins in html files:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Bootstrap 4 Example</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- jQuery library -->
    <script 
     src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">. 
    </script>

    <!-- Popper JS -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>

    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" 
  href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

    <!-- Latest compiled JavaScript -->
    <script 
    src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js">. 
    </script>

  </head>
  <body>
    <div class="container-fluid bg-primary">
      <h1>My First Bootstrap Page</h1>
      <p>This is some text.</p>
    </div>
 </body>
</html>

To sum up, rearranging the jQuery and Bootstrap scripts and eliminating the express-ejs-layouts module successfully resolved the issue.

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

Why doesn't the Kellum method for CSS image replacement work with button elements?

Recently, I discovered that the kellum method doesn't work as effectively with HTML button elements. Specifically, it does work but requires additional text indent. To sum it up, here is the technique: text-indent: 100%; white-space: nowrap; overflow ...

Create an image of a static map from Google

I have incorporated the Google Static Map API into my Angularjs Application to display images of Google maps. Browse here for a glimpse Everything operates smoothly when there are fewer map coordinates. However, when I increase the number of map coordina ...

Can you explain the execution process of this Http.post method and provide details about the code path it follows

As I delve into the world of web development, one aspect that has me stumped is the functionality of the Http.post section within a project I stumbled upon on GitHub. Specifically, this pertains to an ExpressJS with Typescript repository I came across. So, ...

I encounter an error stating "Unable to access properties of undefined (reading 'forEach')" while using Node.js

column 1 column 2 data 1 data 2 data 3 data 4 app.post('/confirmation', function (req, res) { var connection = mysql.createConnection({ host: "localhost", user: "root", password: "", database: ' ...

What is the process for performing a contains query with the MongoDB node.js driver?

When attempting to search for items containing a specific substring, I am not receiving any results despite knowing that the data is correct for the query, including case: collection.find({name: "/.*" + keyword + ".*/"}).toArray(function(err, items) I ex ...

How can JavaScript Regular Expressions be used for Form Validation?

For my registration form, I am including fields such as userid, name, email, password, confirm password, and affiliation. Using regular expressions in JavaScript, I am validating these fields. I am attempting to display any validation errors within the for ...

The Node.js application is unable to execute due to the error: "View "error" could not be found in the views directory."

Currently, I am following a tutorial that covers the integration of social login with Passport and Node. You can find the tutorial here: Tutorial Link In line with the tutorial, I have started working on a project while utilizing Windows 10 operating syst ...

The image within the element is being cropped despite having a higher z-index

On my page, I am dynamically generating a table using an API request. Each row in the table has an icon that, when hovered over by the user, displays an image associated with that item. However, I seem to have misunderstood how z-index functions, as I have ...

Handling file uploads in ReactJS and managing files from the faux path C:/fakepath/file

When attempting to upload a file using a simple form that will be processed by my back-end python code, I encountered an issue where the uploaded file path shows as C:\fakepath\test.txt. After some research, I discovered that this behavior is du ...

Looking for assistance in establishing a connection between Node.js and Angular.js

I have a decent understanding of mongodb and node.js, but I'm struggling with angular.js. I need some help in figuring out how to retrieve data from my node.js code using angular.js. If there are any errors in my node.js code, please let me know. var ...

Working with requests and responses in functions in Node.js

When I swap the positions of req and res, for example (res,req)=>{ res.send();}, the server throws an error saying that res.send is not a function. Are we limited in how we position the req and res objects? Why does it show an error that res.send() is ...

What are the steps to verify and establish databases in CouchDB using Node.JS?

I'm currently working on developing a small web application and have encountered an issue. I initialize the database name in my code at the beginning, before the application runs. My goal is to verify if the specified database name exists on the serve ...

Alternative to Socket.io

I acknowledge that this post may be subject to scrutiny and closure due to its subjective nature, but it is intended solely for gathering information. As someone who is new to the realm of web sockets, I am familiar with the widely-used socket.io library. ...

The function iframe.scrollTo() is not effective for scrolling through Excel or PowerPoint documents on an iPad

I am trying to create a custom scrolling feature for iframe content specifically for iPad. Currently, I have set up my iframe like this: <div id="scroller" style="height: 300px; width: 100%; overflow: auto;"> <iframe height="100%" id="iframe" ...

What strategies can I use to meet a synchronous requirement while utilizing asynchronous logic in NodeJS?

I am currently implementing user validation and data modification functionality on a node.js application. In a parallel universe, within a single function I would: Retrieve the original record from the database Validate the user in LDAP to confirm owner ...

How should I proceed if both fields are encrypted by Mongoose Encryption?

const adminsSchema = new mongoose.Schema({ username: String, password: String, }); const secretKey = "CrackThis1"; adminsSchema.plugin(encrypt, { secret: secretKey, encryptedFields: ["password"] }); const AdminUser = ne ...

Ways to incorporate horizontal scrolling for a flex box container

I'm utilizing flexbox to design a horizontal slider in HTML. However, when using the code below: <!DOCTYPE html> <html lang="en> <head> <meta charset="UTF-8> <meta http-equiv="X-UA-Compatible" content="IE=edge> <met ...

Listening for changes in a variable using a YUI event listener

/* Creating an event listener for the submit button */ YAHOO.util.Event.addListener(webserver.result_form, 'submit', webserver.result_submit); In my main.js, I currently have this event listener set up. I was curious to know if in YUI there is ...

When you drag down on mobile Safari on an iPad, touch events may cease to fire in HTML5

When I implement event listeners to handle touch events like touchmove and touchstart document.addEventListener("touchstart", function(event){ event.preventDefault(); document.getElementById("fpsCounter").innerHTML = "Touch ...

Error: Cannot run yarn dev because node_modules/node/bin/node is missing

While running Next.js on my Windows machine, I am noticing that the file path is displaying as if it were a Linux path. I have already configured the node file path in the environment variable, but I'm still encountering the following error: yarn run ...