BS4 Dynamic Countdown Clock

I have successfully integrated a countdown timer into my website, utilizing the following HTML code:

<div id="clockdiv">
  <div>
    <span class="days"></span>
    <div class="smalltext">Days</div>
  </div>
  <div>
    <span class="hours"></span>
    <div class="smalltext">Hours</div>
  </div>
  <div>
    <span class="minutes"></span>
    <div class="smalltext">Minutes</div>
  </div>
  <div>
    <span class="seconds"></span>
    <div class="smalltext">Seconds</div>
  </div>
</div>

For further reference and demonstration, visit: https://codepen.io/Darlton29/pen/OJNzXEB

The challenge I am facing is creating a responsive design for mobile devices. I want the 4 time blocks to form a cube layout, stacked in pairs of 2 with even spacing. While I've made progress with one block, achieving the same for the others while maintaining consistency has proven difficult. My coding structure relies on Bootstrap 4.

CSS:

#clockdiv{
    font-family: sans-serif;
    color: #fff;
    display: inline-block;
    font-weight: 100;
    text-align: center;
    font-size: 30px;
}

#clockdiv > div{
    padding: 10px;
    border-radius: 3px;
    background: #00BF96;
    display: inline-block;
}

#clockdiv div > span{
    padding: 15px;
  padding-top: 10px;
  padding-bottom: 10px;
    border-radius: 3px;
    background: #00816A;
    display: inline-block;
}

.smalltext{
    padding-top: 2px;
  padding-bottom: -10px;
    font-size: 16px;
}

Answer №1

Problem solved! Here is the solution: https://codepen.io/Ultra_Hopeful/pen/bGpLeRK

function GetTimeRemaining(endtime) {
  var t = Date.parse(endtime) - Date.parse(new Date());
  var seconds = Math.floor((t / 1000) % 60);
  var minutes = Math.floor((t / 1000 / 60) % 60);
  var hours = Math.floor((t / (1000 * 60 * 60)) % 24);
  var days = Math.floor(t / (1000 * 60 * 60 * 24));
  return {
    'total': t,
    'days': days,
    'hours': hours,
    'minutes': minutes,
    'seconds': seconds
  };
}

function initializeCounter(id, endtime) {
  var countdown = document.getElementById(id);
  var daysSpan = countdown.querySelector('.days');
  var hoursSpan = countdown.querySelector('.hours');
  var minutesSpan = countdown.querySelector('.minutes');
  var secondsSpan = countdown.querySelector('.seconds');

  function updateCounter() {
    var t = GetTimeRemaining(endtime);

    daysSpan.innerHTML = ('0' + t.days).slice(-2);
    hoursSpan.innerHTML = ('0' + t.hours).slice(-2);
    minutesSpan.innerHTML = ('0' + t.minutes).slice(-2);
    secondsSpan.innerHTML = ('0' + t.seconds).slice(-2);

    if (t.total <= 0) {
      clearInterval(interval);
    }
  }

  updateCounter();
  var interval = setInterval(updateCounter, 1000);
}

var deadlineDate = new Date(Date.parse(new Date("sep 18, 2020 15:37:25")));

initializeCounter('counterDiv', deadlineDate);
#counterDiv{
  font-family: sans-serif;
  color: #fff;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-gap:5px;
  width:fit-content;
  font-weight: 100;
  text-align: center;
  font-size: 30px;
}
#counterDiv div{
  border-radius: 3px;
  padding:5px;
  background: #00BF96;
}

#counterDiv div > span{
  padding: 15px;
  padding-top: 10px;
  padding-bottom: 10px;
  border-radius: 3px;
  font-size:100%;
  background: #00816A;
}

.smalltext{
  padding: 4px 0px 5px 0px;
  font-size:16px;
}
/* Media Queries */
@media only screen and (max-width: 600px) { #counterDiv { grid-template-columns: 1fr 1fr; } }
@media only screen and (min-width: 600px) { #counterDiv { grid-template-columns: 1fr 1fr; } }
@media only screen and (min-width: 768px) { #counterDiv { grid-template-columns: 1fr 1fr 1fr 1fr; } }
@media only screen and (min-width: 992px) {...}
@media only screen and (min-width: 1200px) {...}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>
<section style="background-color: pink; color: white;">
  <div class="container-fluid mt-5">
    <h1>BIRTHDAY COUNTDOWN</h1>
    <div id="counterDiv">

      <div>
        <span class="days"></span>
        <div class="smalltext">Days</div>
      </div> 

      <div>
        <span class="hours"></span>
        <div class="smalltext">Hours</div>
      </div> 

      <div>
        <span class="minutes"></span>
        <div class="smalltext">Minutes</div>
      </div> 

      <div>
        <span class="seconds"></span>
        <div class="smalltext">Seconds</div>
      </div>

    </div>
  </div>
</section>

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

Pinia alert: The function "getActivePinia()" was invoked without an active Pinia instance. Could it be possible that you overlooked installing Pinia?

Despite having an action that dynamically updates the 'pending' state based on whether the data has been fetched, reactivity seems to be non-functional when used inside the component. This issue is referenced in the official Pinia documentation: ...

How can I prevent an image from scrolling on a webpage?

I am seeking a way to prevent an image from scrolling along with the rest of the page. I want the image to occupy a specific percentage of width, while allowing the browser to determine the appropriate height for display. The image's height should als ...

The issue with the NextJS Layout component is that it is failing to display the page content, with an error message indicating that objects cannot be used

I am embarking on my first project using Next JS and after watching several tutorial videos, I find the Layout component to be a perfect fit for my project! However, I am encountering an issue where the page content does not display. The Menu and Footer a ...

Values in Local Storage are not located upon first attempt, but after a reload they function properly

import {useEffect} from 'react'; import {useRouter} from 'next/router'; const AuthenticationGuard=props=>{ const {children,fallback} = props; const auth = useAuth(); const router=useRouter(); useEffect(()=>{ if(!r ...

Please refresh the page after acknowledging the error

I am facing an issue with my PHP page that triggers a javascript alert upon encountering an error. if (strpos($this->color,':') !== false) { echo '<script type="text/javascript">alert("Please use the RBG format of 255:2 ...

What is causing the poor color and contrast of equirectangular backgrounds in Three.js, and what steps can be taken to improve them?

Using Three.js for a website project with a construction company, I created 360° photospheres using my Google Pixel 5. The client also requested a 3D representation of one of their projects, making Three.js the ideal solution. Comparison: Original photo ...

What could be causing the issue with my css `content:` not functioning across different browsers, and how can I enhance cross-browser compatibility in all cases

Working on my first HTML/CSS project, I encountered a browser compatibility challenge. The code functions well in Firefox, IE, and Edge but fails in Chrome. Specifically, I am trying to make a button display alternating up and down arrows when clicked. Whi ...

Ways to conceal ad container when ad space remains empty

After setting up my ad HTML like this: <div class="ad-container"> <p>Ad slot #1</p> <div id="ad-slot-1" class="ad-slot"></div> </div> I implemented the code below to collapse the ad if ...

Improving Vue Method with Lodash?

In one of my Vue components, I have implemented logic in the watch prop that allows for sequential switching between elements when the down arrow key (key code 40) is pressed. While it is not too messy right now, there is a concern that it may become highl ...

Starting Web Server using File (file://) protocol

I'm currently using Quasar to develop a Vue SPA web app/page. For this project, the web app will only run by clicking on the index.html file generated by the Quasar packager. This package will not be distributed online or hosted on any domain. My mai ...

Unable to assign an IP address to an Express JS application

Struggling to test a specific endpoint in Express, but consistently encountering a 404 error. var express = require("express") var app = express() //var http = require('http').Server(app) app.get('/', function(req,res){ res. ...

Ways to access the HTML content of a component in vue.js

I have a unique html template displayed below CustomTemplate.vue <template> <div class="content custom-page"> <md-card class="page-display"> <md-card-header class="page-header"> <md-card- ...

What is the best way to align text and an arrow on the same line, with the text centered and the arrow positioned on the

.down { transform: rotate(45deg); -webkit-transform: rotate(45deg); } .arrow { border: solid black; border-width: 0 3px 3px 0; display: inline-block; padding: 30px; } <p>Down arrow: <i class="arrow down"></i></p> Looking to ...

Using Angular 4 Component to Invoke JavaScript/jQuery Code From an External File

I have written a jQuery code that is executed at ngAfterViewInit(). //myComponent.ts ngAfterViewInit() { $(function () { $('#myElement').click(function (e) { //the code works fine here }); } However, I want t ...

The background image does not appear on the animated div until the browser window is resized

Encountering an unusual problem - I have styled a div element using CSS to be initially hidden: .thediv { width: 100%; height: 87%; position: fixed; overflow: hidden; background-image: url(pics/FrameWithBG1280.png); background-attachment: fixe ...

Is there a way to begin using the :nth-child() selector from the last element instead of the first?

$('button').click(function(){ $('ul').find('li:nth-child(2)').css('color', '#f00') }) <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> < ...

Changing the color of an element on click using jQuery's .css() method,

There is a table and I am using fadeToggle on the list to hide and show the table. The background of the list is green color, but I want to change the list color when hiding the table and revert it back to green when clicking to show the table. Unfortunate ...

What is the best way to ensure that the maximum price is mandatory when entering a minimum price?

Essentially, the process works like this: if a person inputs a minimum price but forgets to input a maximum price, then presses search, it will not perform the search and a message will appear next to the max price field reminding them to enter a maximum ...

What is the best way to incorporate PHP code within a JavaScript function?

Is it possible to dynamically append the uploaded file name to the list displayed in ('.list')? The challenge lies in passing $_FILES["fileImage"]["name"] as an argument to a separate JavaScript function for appending, especially since the PHP sc ...

Utilizing the datepicker options function within a different function

I'm working on a function that utilizes a promise to retrieve data from an asynchronous ajax call: $("#mySelect").on('change', function() { var mySelectValue = $('#mySelect').val(); var promise = getAvailableDates(mySe ...