html - How to center a DIV created by Javascript? -


here code counter (modified www.w3schools.com code).

i'm struggling horizontally center output. have tried wrap in div , use following techniques center div, no success, pointers appreciated:

  1. using css style outside div text-align: center;
  2. using css style outside div margin-left: auto; & margin-right:auto;
  3. using javascript set margin property of output horizontally align;
<!doctype html> <html> <head>     <style>         .bigger {             text-align: center;             font-size: 1.35em;         }         .counter_p {             text-align: center;             font-size: 6em;             padding: 0px 0px 0px 0px;             line-height: 1em;         }         .counter_label {             font-size: 0.2em;             line-height: 1em;         }          }     </style> </head> <body>     <div class="counter_p" id="counter"></div>     <script>         function pad(str, max) {             return str.length < max ? pad("0" + str, max) : str;         }          // set date we're counting down         var countdowndate = new date("sep 1, 2017 15:37:25").gettime();          // update count down every 1 second         var x = setinterval(function() {              // todays date , time             var = new date().gettime();              // find distance between count down date             var distance = countdowndate - now;              // time calculations days, hours, minutes , seconds             var days = pad(string(math.floor(distance / (1000 * 60 * 60 * 24))),2);             var hours = pad(string(math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))),2);              var minutes = pad(string(math.floor((distance % (1000 * 60 * 60)) / (1000 * 60))),2);             var seconds = pad(string(math.floor((distance % (1000 * 60)) / 1000)),2);               // attempt @ horizontally centering output             document.getelementbyid('counter').style.margin = '0 auto';              // output result in element id="counter"             document.getelementbyid("counter").innerhtml =                 "<div style='text-align:center'>"                 +   "<table border='0'>"                 +       "<tr>"                 +           "<td>"                 +               "<div>"+ days + "</div>"                 +               "<div class='counter_label'>days</div>"                 +           "</td>"                 +           "<td class='bigger'>|</td>"                 +           "<td>"                 +               "<div>" + hours + "</div>"                 +               "<div class='counter_label'>hours</div>"                 +           "</td>"                 +           "<td class='bigger'>|</td>"                 +           "<td>"                 +               "<div>" + minutes + "</div>"                 +               "<div class='counter_label' >mins</div>"                 +           "</td>"                 +           "<td class='bigger'>|</td>"                 +           "<td>"                 +               "<div>" + seconds + "</div>"                 +               "<div class='counter_label'>secs</div>"                 +           "</td>"                 +           "<td>"                 +       "</tr>"                 +   "</table>"                 +"</div>";               // if count down over, write text             if (distance < 0) {                 clearinterval(x);                 document.getelementbyid("counter").innerhtml = "countdown complete";             }         }, 1000);     </script> </body> </html> 

creating container div hold countdown text works in case (using css flexbox justify-content set "center") - flexbox works correctly on window resize:

<!doctype html>      <html>      <head>      <style>          .bigger {        text-align: center;        font-size: 1.35em;      }      .counter_p {        text-align: center;        font-size: 6em;        padding:0px 0px 0px 0px;        line-height:1em;        }      .counter_label {          font-size:0.2em;          line-height:1em;      }      .container {        display: flex;        justify-content: center;      }        }      </style>      </head>      <body>      <div class="container">          <div class="counter_p" id="counter"></div>      </div>      <script>          function pad (str, max) {            return str.length < max ? pad("0" + str, max) : str;          }        // set date we're counting down      var countdowndate = new date("sep 1, 2017 15:37:25").gettime();        // update count down every 1 second      var x = setinterval(function() {            // todays date , time          var = new date().gettime();            // find distance between count down date          var distance = countdowndate - now;            // time calculations days, hours, minutes , seconds          var days = pad(string(math.floor(distance / (1000 * 60 * 60 * 24))),2);          var hours = pad(string(math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))),2);            var minutes = pad(string(math.floor((distance % (1000 * 60 * 60)) / (1000 * 60))),2);          var seconds = pad(string(math.floor((distance % (1000 * 60)) / 1000)),2);            // output result in element id="counter"        document.getelementbyid("counter").innerhtml = "<div style='text-align:center'><table border='0'><tr><td><div>"+ days + "</div><div  class='counter_label' >days</div></td><td class='bigger'>|</td><td><div>" + hours + "</div><div class='counter_label'>hours</div></td><td class='bigger'>|</td><td><div>"          + minutes + "</div><div class='counter_label' >mins</div></td><td class='bigger'>|</td><td><div>" + seconds + "</div><div class='counter_label'>secs</div></td><td></tr></table></div>";                // if count down over, write text           if (distance < 0) {              clearinterval(x);        document.getelementbyid("counter").innerhtml = "countdown complete";          }      }, 1000);      </script>        </body>      </html> 

note: css flexbox not supported in older browsers, see css flexible box layout module


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -