2016-04-29 47 views
0

我工作的番茄工作項目上的免費代碼陣營:如何在HTML中心的輸入域/ CSS /引導

https://www.freecodecamp.com/challenges/build-a-pomodoro-clock

而且我想我的居中輸入框,但我沒有取得太大的成功。有沒有人有任何想法?

var timeMin = 0; 
 
    var timeSec = 3; 
 
    var timerIntervalID = null; 
 

 
    function pad (str, max) { 
 
     str = str.toString(); 
 
     return str.length < max ? pad("0" + str, max) : str; 
 
    } 
 

 

 
    function updateTimer() { 
 
    \t var displayString = ""; 
 

 
    \t console.log("Update timer()"); 
 
    \t if (timeSec === 0) { 
 
    \t \t timeSec = 59; 
 
    \t \t timeMin--; 
 
    \t } else { 
 
    \t \t timeSec--; 
 
    \t } 
 

 
    \t displayString = timeMin + ":" + pad(timeSec, 2); 
 

 

 
    \t $(".timer").html(displayString); 
 

 
    \t if (timeMin < 1 && timeSec < 1) { 
 
    \t \t $(".timer").css('color', 'red'); 
 
    \t \t clearInterval(timerIntervalID); 
 
    \t \t alert("Pomodoro Over!") 
 
    \t } 
 
    } 
 

 
    function test() { 
 
    \t console.log("Test"); 
 
    } 
 

 
    $(document).ready(function() { 
 
    \t $("button").click(function() { 
 
    \t \t var whichButton = $(this).attr("value"); 
 
    \t \t console.log("Button pressed"); 
 
    \t \t switch(whichButton) { 
 
    \t \t \t case "start": 
 
    \t \t \t \t timerIntervalID = setInterval(updateTimer, 1000); 
 
    \t \t \t \t break; 
 
    \t \t \t case "reset": 
 
    \t \t \t \t timeMin = 0; 
 
    \t \t \t \t timeSec = 3; 
 
    \t \t \t \t if (timerIntervalID !== null) { 
 
    \t \t \t \t \t clearInterval(timerIntervalID); 
 
    \t \t \t \t } 
 

 
    \t \t \t \t $(".timer").css('color', 'black'); 
 
    \t \t \t \t displayString = timeMin + ":" + pad(timeSec, 2); 
 
    \t \t \t \t $(".timer").html(displayString); 
 
    \t \t \t \t break; 
 
    \t \t } 
 
    \t }); 
 
    });
.btn-primary { 
 
    \t width: 15rem; 
 
    \t margin: 0.2rem; 
 
    \t height: 5rem; 
 
    } 
 

 
    .btn-danger { 
 
    \t width: 15rem; 
 
    \t margin: 0.2rem; 
 
    \t height: 5rem; 
 
    } 
 

 
    input { 
 
      max-width: 4rem; 
 
      text-align:center; 
 
      display:block; 
 
      margin:0; 
 
    }
<head> 
 
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> 
 
     <link rel="stylesheet" type="text/css" href="styles.css"> 
 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 
 
     <script src="main.js"></script> 
 
    </head> 
 

 
    <html> 
 
    \t <head> 
 
    \t  <meta charset="utf-8"> 
 
    \t  <title>fccPomodoro</title> 
 
    \t </head> 
 
     <div class="container"> 
 
     <div class="jumbotron text-center"> 
 
      <h1>fccPomodoro</h1> 
 
       <h2 class="timer">Time Left: 25 minutes</h2> 
 
       <button type="button" class="btn btn-primary" value="start">Start Pomodoro</button> 
 
       <button type="button" class="btn btn-danger" value="reset">Reset</button> 
 
       <div class="form-group"> 
 
       <label for="min">Minutes:</label> 
 
       <input type="text" class="form-control" id="min" value="25"> 
 
       </div> 
 
       <div class="form-group"> 
 
       <label for="sec">Seconds:</label> 
 
       <input type="text" class="form-control" id="sec" value="00"> 
 
       </div> 
 
     </div> 
 
     </div> 
 
    </html>

styles.css 

.btn-primary { 
    width: 15rem; 
    margin: 0.2rem; 
    height: 5rem; 
} 

.btn-danger { 
    width: 15rem; 
    margin: 0.2rem; 
    height: 5rem; 
} 

input { 
     max-width: 4rem; 
     text-align:center; 
     display:block; 
     margin:0; 
} 

回答

1

您可以

margin: 0 auto 
在輸入框

input { 
    max-width: 4rem; 
    text-align:center; 
    display:block; 
    margin:0 auto; 
} 
+0

這個工作。謝謝! –

0

您是否嘗試過中心標籤內輸入代碼?

<center><input type="text" ></center> 
1

請參閱下面的CSS;

var timeMin = 0; 
 
    var timeSec = 3; 
 
    var timerIntervalID = null; 
 

 
    function pad (str, max) { 
 
     str = str.toString(); 
 
     return str.length < max ? pad("0" + str, max) : str; 
 
    } 
 

 

 
    function updateTimer() { 
 
    \t var displayString = ""; 
 

 
    \t console.log("Update timer()"); 
 
    \t if (timeSec === 0) { 
 
    \t \t timeSec = 59; 
 
    \t \t timeMin--; 
 
    \t } else { 
 
    \t \t timeSec--; 
 
    \t } 
 

 
    \t displayString = timeMin + ":" + pad(timeSec, 2); 
 

 

 
    \t $(".timer").html(displayString); 
 

 
    \t if (timeMin < 1 && timeSec < 1) { 
 
    \t \t $(".timer").css('color', 'red'); 
 
    \t \t clearInterval(timerIntervalID); 
 
    \t \t alert("Pomodoro Over!") 
 
    \t } 
 
    } 
 

 
    function test() { 
 
    \t console.log("Test"); 
 
    } 
 

 
    $(document).ready(function() { 
 
    \t $("button").click(function() { 
 
    \t \t var whichButton = $(this).attr("value"); 
 
    \t \t console.log("Button pressed"); 
 
    \t \t switch(whichButton) { 
 
    \t \t \t case "start": 
 
    \t \t \t \t timerIntervalID = setInterval(updateTimer, 1000); 
 
    \t \t \t \t break; 
 
    \t \t \t case "reset": 
 
    \t \t \t \t timeMin = 0; 
 
    \t \t \t \t timeSec = 3; 
 
    \t \t \t \t if (timerIntervalID !== null) { 
 
    \t \t \t \t \t clearInterval(timerIntervalID); 
 
    \t \t \t \t } 
 

 
    \t \t \t \t $(".timer").css('color', 'black'); 
 
    \t \t \t \t displayString = timeMin + ":" + pad(timeSec, 2); 
 
    \t \t \t \t $(".timer").html(displayString); 
 
    \t \t \t \t break; 
 
    \t \t } 
 
    \t }); 
 
    });
.btn-primary { 
 
    \t width: 15rem; 
 
    \t margin: 0.2rem; 
 
    \t height: 5rem; 
 
    } 
 

 
    .btn-danger { 
 
    \t width: 15rem; 
 
    \t margin: 0.2rem; 
 
    \t height: 5rem; 
 
    } 
 

 
    .form-group { 
 
      text-align:center; 
 
    } 
 

 
    .form-group input { 
 
      max-width: 4rem; 
 
      display:block; 
 
      margin:0 auto 0 auto; 
 
    }
<head> 
 
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> 
 
     <link rel="stylesheet" type="text/css" href="styles.css"> 
 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 
 
     <script src="main.js"></script> 
 
    </head> 
 

 
    <html> 
 
    \t <head> 
 
    \t  <meta charset="utf-8"> 
 
    \t  <title>fccPomodoro</title> 
 
    \t </head> 
 
     <div class="container"> 
 
     <div class="jumbotron text-center"> 
 
      <h1>fccPomodoro</h1> 
 
       <h2 class="timer">Time Left: 25 minutes</h2> 
 
       <button type="button" class="btn btn-primary" value="start">Start Pomodoro</button> 
 
       <button type="button" class="btn btn-danger" value="reset">Reset</button> 
 
       <div class="form-group"> 
 
       <label for="min">Minutes:</label> 
 
       <input type="text" class="form-control" id="min" value="25"> 
 
       </div> 
 
       <div class="form-group"> 
 
       <label for="sec">Seconds:</label> 
 
       <input type="text" class="form-control" id="sec" value="00"> 
 
       </div> 
 
     </div> 
 
     </div> 
 
    </html>