Struggling to align an HTML email template to the center

Currently, I am immersed in a web project utilizing react and MUI. The code collects user input data, inserts it into an HTML template, and sends it via email. One issue I'm encountering is that the HTML content is not vertically centered when viewed in the email client (refer to image)1. To address this problem, I've applied the following CSS:

display: flex;
justify-content: center !important

Interestingly, testing the HTML individually shows that it does center properly. However, upon checking the email through Chrome or Firefox, the alignment fails. Inspecting the devtools reveals that justify-content: center !important is missing. Manually adding this value resolves the issue.

Below is a snippet of the code in question:

import dgoow from "../images/dgo 2.png";

export const emailBody = (event, date, channels, priority, countries) => {
  return `
<html>
<head>
  <style>
    * {
      font-family: Helvetica, Arial, sans-serif;
    }
    h1 {
      color: white;
    }
    h3,
    h5 {
      color: white;
    }
    table {
      border: none;
    }
    button {
      color: white;
      border-radius: 5px;
      font-family: Helvetica, Arial, sans-serif;
      font-weight: bold;
      font-size: 16px;
      cursor: pointer;
      background-color: #fd8204;
      border: none;
      padding: 1rem;
      margin-bottom: 2rem;
    }
    img {
      height: 70px;
      width: 150px;
    }
    #myElement{
      display: flex;
      justify-content: center !important;
    }
  </style>
</head>
<body id="myElement">
  <div id='dos' style="background-color: #050914;border-radius: 10px;padding: 3rem;width: 800px;margin: 3rem;"> 
  <table>
      <tr>
        <td style="text-align: start; padding: 1rem; width: 50%; position: relative;">
          <img src=${dgoow} alt="DWEGO logo" />
        </td>
        <td style="text-align: end; padding: 1rem; width: 50%; position: relative;">
          <img src=${dgoow} alt="DWEGO logo" />
        </td>
      </tr>
      <tr>
        <td colspan="2" style="text-align: center; font-size: 20px">
          <h1>HWRTE Notification</h1>
        </td>
      </tr>
      <tr>
        <td colspan="2" style="text-align: center; font-size: 20px">
          <h3>Event: ${event}</h3>
        </td>
      </tr>
      <tr>
        <td colspan="2" style="text-align: center; font-size: 20px">
          <h3>Date: ${date}</h3>
        </td>
      </tr>
      <tr>
        <td colspan="2" style="text-align: center; font-size: 20px">
          <h3>Channels: ${channels}</h3>
        </td>
      </tr>
      <tr>
        <td colspan="2" style="text-align: center; font-size: 20px">
          <h3>Priority: ${priority}</h3>
        </td>
      </tr>
      <tr>
        <td colspan="2" style="text-align: center; font-size: 20px">
          <h3>Countries: ${countries}</h3>
        </td>
      </tr>
      <tr>
        <td colspan="2" align="center">
          <a href="www.google.com" target="_blank">
            <button>JOIN THE BRIDGE</button>
          </a>
        </td>
      </tr>
    </table>
  </div>
</body>
</html>`;
};

In addition, there seems to be an issue with the images not loading correctly, although the cause remains unclear.

Answer №1

Based on information from a source, it seems that the use of display:grid/flex is not widely supported by email clients.

To address this issue, I made adjustments to the CSS code as shown below:

#myElement{
  display: flex;
  justify-content: center !important;
}

By changing it to:

#myElement{
  text-align:center";      
}

And it worked successfully.

For those utilizing nodemailer for email communication, there is a helpful guide on sending embedded images available at this website.

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

Choose an item from a list that does not have a particular class

Here is a list of items: <ul id='myList'> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> <li class='item-selected'>Item 4</li> <li>Item 5</li> & ...

Developing an Addon for Firefox using XUL and JavaScript

I am interested in creating a Firefox Addon that dynamically generates a button in the webpage's DOM when a specific page like www.google.com is loaded. This button, when clicked, will redirect to another page such as www.stackoverflow.com. It is imp ...

"Encountering issues with getJson function when used on a web hosting

Issue with Web Service JSON Connection: http://mohamedbadr.com/webservice/list.php File Fetching Results: http://contestlancer.com/web/getList.php Code for Getlist file: <!DOCTYPE HTML> <html> <head> <title>Hotel Promotion ...

How can I use the jQuery map function to separate IDs with commas?

code: <script> $("#save_other").click(function(e){ e.preventDefault(); question = $(".question:checked").map(function() { return this.id; }).get().join(","); alert(question); ...

Issue: receiving an error message "Error: spawn wslvar ENOENT" while attempting to run yarn storybook on

Currently, I am attempting to follow a tutorial found at The tutorial provides the following set of commands: # Clone the template npx degit chromaui/intro-storybook-react-template taskbox cd taskbox # Install dependencies yarn # Run the test runner (J ...

Ways to set a default image for an <img> tag

I am looking to set a default image to the img tag in case the user has not selected a profile picture for their account. Here is the current output: http://jsfiddle.net/LvsYc/2973/ Check out the default image here: This is the script being used: funct ...

React project automatically refreshing when local server generates new files

I have developed a tool for developers that allows them to retrieve device data from a database for a specific time period, generate plots using matplotlib, save the plots locally, and display them on a webpage. The frontend is built using create-react-app ...

Encountering a Next-auth issue while attempting to access a provider

I encountered an issue while attempting to retrieve provider data in the login component: TypeError: (0 , next_auth_react__WEBPACK_IMPORTED_MODULE_3__.providers) is not a function Here is my code along with a simple test to fetch the provider data import ...

Pausing a video in a JWPlayer iframe with JavaScript

How can I stop a JWPlayer iframe using this code? Here is the HTML code: <a href="#" onclick="popup('video'); stopvideo();" >Click To Close</a> <iframe src="<?php echo get_template_directory_uri(); ?>/scalerokuengine.php" i ...

What is the best way to adjust the mobile menu in Wordpress so that it takes up half of the screen width?

Encountering an issue after installing the "Mobile Navigation" Wordpress Plugin to implement a mobile menu on my custom theme that currently occupies 100% of the screen width. How can I adjust it to occupy only 50% of the screen width, as shown in the conc ...

Guide to autoplay an uploaded mp4 video in a flask application

I've been working on a Flask application that includes an upload button for videos in mp4 format. I'm facing an issue where after uploading a video, it only shows a generic grey screen without playing the video. I want to ensure that once a video ...

How to update an Array<Object> State in ReactJS without causing mutation

In my program, I store an array of objects containing meta information. This is the format for each object. this.state.slotData [{ availability: boolean, id: number, car: { RegistrationNumber : string, ...

The Fullcalendar display is distorted due to the CSS being loaded after the calculations for the layout

I am utilizing the FullCalendar jQuery plugin within a web application framework plugin. Additional CSS files such as fullcalendar.css are dynamically loaded in JavaScript: $("<link rel='stylesheet' type='text/css' href='fullc ...

The parameter type `SetStateAction<EventDetails[] | undefined>` does not allow for assigning arguments

I am currently facing an issue in a ReactJS project where I am trying to update state that was initially set up like this: const [eventsData, setEventsData] = useState<EventDetails[]>(); Every time I try to update the state using setEventsData(obj), ...

How can you prevent a tag from firing in Google Tag Manager by identifying a particular script included in the HTML code

Seeking advice on modifying our Google Tag Manager container as I am a novice when it comes to GTM. Encountering an issue with IE8 on pages utilizing Fusion Charts resulting in a javascript error in gtm.js. Upon investigation, it appears to be related to a ...

Adaptable Image Functionality in Jquery Carousel

I've encountered an issue with my images within a jquery slider. While the slider itself is functioning properly, I am facing a couple of challenges. Initially, I aimed to make the images responsive, but upon removing the height property, the content ...

NextJs experiencing hydration issue due to a basic array.map operation

Having an odd hydration issue while working with NextJS. I could use some assistance figuring out what's causing it. I have an object that is being processed synchronously. When I stringify this object and render it, everything works fine without any ...

Links and buttons unresponsive in React modal window

My current challenge involves utilizing ReactDOM.createPortal to create a global modal that can be accessed from any page within the React app. The modal opens and displays correctly, but I have encountered an issue where the and elements inside the moda ...

Firestore Error: Unable to use collection().where() method

I'm currently working on filtering my documents in Firebase Firestore by checking if the 'users' array in a document contains the user's email address. Within the 'chat' collection, there are documents with IDs: '[email&# ...

Creating new accounts with SQL in HTML code

I am working on a query to input any information entered into a form into the database. I have written some code, but I believe there is an issue with the query itself as I am unsure how to include variables in a SQL query. If anyone could assist me with t ...