The countdown text is refusing to remain centered within the border in Javascript

I am struggling to center text within a border. The text should be perfectly aligned in the middle of the border.

The text appears slightly to the right and the number slightly to the left

I was able to resolve this issue in Chrome, but it persists on CodePen, Safari, and Internet Explorer. Here is the CodePen link

Here is my HTML code:

// Set the date we're counting down to
  var countDownDate = new Date("November 05, 2018 9:30:00 GMT").getTime();

  // Update the countdown timer every second
  var x = setInterval(function() {
    // Get today's date and time
    var now = new Date().getTime();

    // Calculate the distance between now and the count down date
    var distance = countDownDate - now;

    // Perform time calculations for days, hours, minutes, and seconds
    var days = Math.floor(distance / (1000 * 60 * 60 * 24));
    var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((distance % (1000 * 60)) / 1000);

    // Display the countdown elements with appropriate styling
    document.getElementById("Countdown_Days").innerHTML =
      days + "<span>" + "<br>" + "Days" + "</span>";

    document.getElementById("Countdown_Hours").innerHTML =
      hours + "<span>" + "<br>" + "Hours" + "</span>";

    document.getElementById("Countdown_Minutes").innerHTML =
      minutes + "<span>" + "<br>" + "Minutes" + "</span>";

    document.getElementById("Countdown_Seconds").innerHTML =
      seconds + "<span>" + "<br>" + "Seconds" + "</span>";
  });
      
h2 {
        border: solid 1px #000;
        border-radius: 100%;
        display: inline-flex;
        padding: 30px 25px;
        font-size: 20px !important;
        color: #fff;
        font-weight: 600;
        line-height: 25px;
        height: 125px;
        width: 125px;
        margin: auto;
        text-align: center;
        margin-left: 5px;
      }
        
<head>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
      </head>
      
      <div class="overlay">
        <div class="row marginnone">
          <div class="col-lg-7 col-md-12 center">
            <div class="countdown">
              <h1>Launch In</h1>
              <h2 id="Countdown_Days">58<span><br>Days</span></h2>
              <h2 id="Countdown_Hours">22<span><br>Hours</span></h2>
              <h2 id="Countdown_Minutes">42<span><br>Minutes</span></h2>
              <h2 id="Countdown_Seconds">40<span><br>Seconds</span></h2>
            </div>
          </div>
        </div>
      </div>

Any assistance would be greatly appreciated! Thank you!

Answer №1

I have made adjustments to the flex-direction of the H2 elements by replacing span and br elements with div elements. Did this achieve the desired outcome?

// Set the date we're counting down to
var countDownDate = new Date("November 05, 2018 9:30:00 GMT").getTime();

// setting the location and time zone for said location
// var city = ' London';
// var offset = '+1.0 ';

// Update the count down every 1 second
var x = setInterval(function() {
  // Get todays date and time
  var now = new Date().getTime();

  // Find the distance between now and the count down date
  var distance = countDownDate - now;

  // Time calculations for weeks, days, hours, minutes and seconds
  // var weeks = Math.floor(distance / (1000 * 60 * 60 * 24 * 7));
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);

  // Output the result in an element with id="demo"
  document.getElementById("Countdown_Days").innerHTML = `<div>${days}</div><div>Days</div>`;

  document.getElementById("Countdown_Hours").innerHTML =`<div>${hours}</div><div>Hours</div>`;

  document.getElementById("Countdown_Minutes").innerHTML = `<div>${minutes}</div><div>Minutes</div>`;

  document.getElementById("Countdown_Seconds").innerHTML = `<div>${seconds}</div><div>Seconds</div>`;
});
h2 {
  align-items: center;
  border: solid 1px #000;
  border-radius: 100%;
  display: inline-flex;
  flex-direction: column;
  padding: 30px 25px;
  font-size: 20px !important;
  color: #fff;
  font-weight: 600;
  line-height: 25px;
  height: 125px;
  width: 125px;
  margin: auto;
  margin-left: 5px;
}
<head>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>

<div class="overlay">
  <div class="row marginnone">
    <div class="col-lg-7 col-md-12 center">
      <div class="countdown">
        <h1>Launch In</h1>
        <h2 id="Countdown_Days">58<span><br>Days</span></h2>
        <h2 id="Countdown_Hours">22<span><br>Hours</span></h2>
        <h2 id="Countdown_Minutes">42<span><br>Minutes</span></h2>
        <h2 id="Countdown_Seconds">40<span><br>Seconds</span></h2>
      </div>
    </div>
  </div>
</div>

Answer №2

Implement this customized CSS in place of your current CSS:

.countdown {
  display: flex;
}

h2{
  border: solid 1px #000;
  border-radius: 100%;
  padding: 30px 25px;
  font-size: 20px !important;
  color: #fff;
  font-weight: 600;
  line-height: 25px;
  height: 125px;
  width: 125px;
  margin: auto;
  text-align: center;
  margin-left: 5px;
}

Your h1 element should be positioned above the countdown section

<div class="overlay">
  <div class="row marginnone">
   <div class="col-lg-7 col-md-12 center">
    <h1>Launch In</h1>
    <div class="countdown">
      <h2 id="Countdown_Days">58<span><br>Days</span></h2>
      <h2 id="Countdown_Hours">22<span><br>Hours</span></h2>
      <h2 id="Countdown_Minutes">42<span><br>Minutes</span></h2>
      <h2 id="Countdown_Seconds">40<span><br>Seconds</span></h2>
    </div>
   </div>
  </div>
 </div>

// Set the date we're counting down to
var countDownDate = new Date("November 05, 2018 9:30:00 GMT").getTime();

// setting the location and time zone for said location
// var city = ' London';
// var offset = '+1.0 ';

// Update the count down every 1 second
var x = setInterval(function() {
  // Get todays date and time
  var now = new Date().getTime();

  // Find the distance between now and the count down date
  var distance = countDownDate - now;

  // Time calculations for weeks, days, hours, minutes and seconds
  // var weeks = Math.floor(distance / (1000 * 60 * 60 * 24 * 7));
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);

  // Output the result in an element with id="demo"
  document.getElementById("Countdown_Days").innerHTML =
    days + "<span>" + "<br>" + "Days" + "</span>";

  document.getElementById("Countdown_Hours").innerHTML =
    hours + "<span>" + "<br>" + "Hours" + "</span>";

  document.getElementById("Countdown_Minutes").innerHTML =
    minutes + "<span>" + "<br>" + "Minutes" + "</span>";

  document.getElementById("Countdown_Seconds").innerHTML =
    seconds + "<span>" + "<br>" + "Seconds" + "</span>";
});
.countdown {
  display: flex;
}

h2{
  border: solid 1px #000;
  border-radius: 100%;
  padding: 30px 25px;
  font-size: 20px !important;
  color: #fff;
  font-weight: 600;
  line-height: 25px;
  height: 125px;
  width: 125px;
  margin: auto;
  text-align: center;
  margin-left: 5px;
}
<head>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>

<div class="overlay">
  <div class="row marginnone">
    <div class="col-lg-7 col-md-12 center">
      <h1>Launch In</h1>
      <div class="countdown">
        <h2 id="Countdown_Days">58<span><br>Days</span>           </h2>
        <h2 id="Countdown_Hours">22<span><br>Hours</span>           </h2>
        <h2 id="Countdown_Minutes">42<span>                           <br>Minutes</span>
        </h2>
        <h2 id="Countdown_Seconds">40<span>                        <br>Seconds</span>
        </h2>
      </div>
    </div>
  </div>
</div>

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

The test does not pass when attempting to use a shorthand operator to ascertain the truthfulness of

I've encountered an interesting issue with my unit test. It seems to work perfectly fine when I directly return true or false, but fails when I try to use a shorthand method to determine the result. Let's say I have a function called isMatched w ...

What is causing the inability for Angular to interpolate values now?

I am currently using the most recent version of Angular and encountering an issue with the following code (which functioned properly in the past). The ng-init is causing a crash with the following error: <tbody ng-controller="MyCtrl" id={{ row.id }} ng ...

Efficiently populating a dataTable with data using ajax

Here is the code I am using to load a data table: $(".dataTable").dataTable({"bProcessing": true, "sAjaxSource": "datatable"}); This file contains the data for the datatable: return '{ "aaData": [ [ "row 1 col 1 data", "row 1 col 2 data", "row 1 ...

Dealing with Unhandled Promise Rejections in Express.js

I'm providing all the necessary details for this question, however I am confused as to why my callback function is returning an Unhandled Promise Rejection even though I deliberately want to catch the error: (node:3144) UnhandledPromiseRejectionWarni ...

Creating a dazzling illuminated Icoshadron using Three.js

I set out to bring the brilliance of a brightly glowing Icoshadron Geometry to life in Three.js. No matter what code I tried, I couldn't achieve the desired glow effect. So, I took the approach of placing the Icoshadron Geometry into a Group with a M ...

Applying absolute positioning to the router-outlet element in Angular2 using CSS

I am working on a website with the following basic layout: <nav> Nav content here </nav> <router-outlet></router-outlet> I have a component that is being displayed in the router outlet, but I want it to appear on top of ...

Having trouble locating the 'EJS' Typescript for the Express framework

After attempting to implement EJS with TypeScript, I encountered an issue where I am able to use res.send without any problems. app.set('views', path.join(__dirname, 'src/views/')); app.set('view engine', 'ejs'); ...

What is the most effective way to declare a variable in TypeScript when assigning it within a class?

Is there a more efficient way to define the class variable accountHandler without using any? main.ts private accountHandler: any= {}; private requestLOB: string[] = []; constructor() { if (process.env.ENABLEBackendSwitch === "true&q ...

Creating a CSS button that spans multiple lines within a paragraph while maintaining the appearance of an anchor tag, even in

UPDATE: The solution provided by @Dinesh karthik works well, but unfortunately it does not support IE11. I am trying to make it compatible with IE11 as well. Is there a way to make a button look like an anchor tag within a paragraph? I want the button to ...

Unusual styling: Unexpected CSS :after impact on card layouts in Bootstrap 4

I'm currently utilizing a Bootstrap 4 masonry card layout for a particular project, and within this project, there is a <select> element that has been styled with custom CSS properties. One of the customized styles includes a caret created using ...

You cannot define headers once they have been sent to the client. If you encounter a NextJS redirect issue, consider using res

I encountered an error when redirecting to a link from NextJS -> _app.js -> getInitialProps. I am trying to cache the last user language in cookies and redirect to it when the page is reloaded. It's working and the user is redirected, but I&apos ...

Using the data () method to define Chart.js annotations in Vue.js can cause them to malfunction

I've encountered an issue while incorporating chartjs-plugin-annotations into a Vue.js application We have successfully integrated ChartJS charts into our Vue applications by defining the chart's config within the data () section of the componen ...

Receive a condensed version of several scripts

My shop's category pages display a list of products, and I need to change the position of a wishlist button for each product on the list. To achieve this, I created individual jQuery scripts for each product like so: jQuery('.product:nth-child( ...

Use the Segoe UI typeface on Internet Explorer for a sleek

I have implemented a custom Segoe UI font in my CSS file. While it displays correctly in Chrome, I am facing an issue where IE and Firefox are showing the default font in certain areas. Below is the snippet of the CSS code: body { margin:0px auto; ...

What is the best way to add a button to every row in Titanium Studio?

Is there a way to add a different button inside each row (createTableViewRow)? I have created five buttons using Titanium.UI.createButton, but I'm struggling to figure out how to place all five buttons in every row. Can someone provide some guidance o ...

Using PHP (FPDF) to showcase pictures loaded from an HTML form into a PDF document

Looking to create a unique HTML form that allows users to upload an image for display in a PDF file using the FPDF library in PHP. I attempted a basic code snippet, but it crashes unless I comment out the line $pdf->Image(...). While I can retrieve the ...

Dim the color of a date on the Sharepoint calendar

Trying to gray out specific days in a SharePoint calendar using the script editor. Looking to target individual days like Christmas day. Came across an article that uses code to gray out weekends, but I need something more precise... Not sure if this code ...

Why is it that my threshold for frustration in Bootstrap has been reached due to the ineffective grid system

It’s puzzling to me why my breakpoint is fixed at 800 px when I’ve specified it to be 991 (lg and lower) in Bootstrap 4. Here's the code snippet that I’m using: <div class="container-fluid"> <div class="row"> <div c ...

Elements featured in the header: logo, tagline, graphics, and more

I'm interested in designing a schema with the following elements: I'm contemplating whether it would be beneficial to have a separate container for the logo and slogan. Additionally, I am considering if it is advantageous to create a container f ...

Updating Time by Adding Minutes using ngFor Loop in Angular 4

I'm currently developing a scheduler that requires user input for start time and the time between two games. I am looking to display the time in a loop using ngFor, incrementing by minutes each time. How can I accomplish this within an Angular 4 HTML ...