【发布时间】:2017-01-17 08:57:59
【问题描述】:
我试图弄清楚如何将我的innerHTML 居中(垂直和水平),同时牢记内容的长度。这是我的 HTML 和 CSS。
<body>
<div class="container">
<p id="demo"></p>
<script>
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} document.getElementById("demo").innerHTML = "Computer: " + computerChoice;
var compare = function(choice1, choice2) {
if(choice1 === choice2) {
document.getElementById("demo").innerHTML = "The result is a tie!";
} else if (choice1 === "rock") {
if (choice2 === "scissors") {
document.getElementById("demo").innerHTML = "Congratulatioins, you win!";
} else {
document.getElementById("dmeo").innerHTML = "Sorry, the computer shows paper. You lost!";
}
} else if (choice1 === "paper") {
if (choice2 === "rock") {
document.getElementById("demo").innerHTML = "Congratulatioins, you win!";
} else {
document.getElementById("demo").innerHTML = "Sorry, the computer shows scissors. You lost!";
}
} else if (choice1 === "scissors") {
if (choice2 === "paper") {
document.getElementById("demo").innerHTML = "Congratulatioins, you win!";
} else {
document.getElementById("demo").innerHTML = "Sorry, the computer shows rock. You lost!";
}
}
}
compare (userChoice, computerChoice);
</script>
.container {
width: 50%;
margin: 0 auto;
padding: 10px;
}
body {
background-image: url(images/bg-img.jpg);
background-repeat: no-repeat;
background-attachment: fixed;
margin: 0;
}
#demo {
font-family: Impact;
font-size: 40px;
color: #cddc39;
text-shadow: 2px 4px 3px rgba(106, 209, 25, 0.62);
}
【问题讨论】:
-
网站对于在 css 中居中是否非常有用:howtocenterincss.com
标签: javascript html css function conditional