這個問題在這里已經有了答案:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Righteous&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Amatic SC:wght@700&display=swap');
body{
font-family: 'Righteous', cursive;
/* font-family: 'Amatic SC', cursive; */
font-size: 1.3em;
color: white;
background-color: black;
}
/* making the font style same */
input,button{
font-family: inherit;
}
/* styling the field set and aligning to center */
fieldset{
max-width: max-content;
background-color: black;
margin-left: auto;
margin-right: auto;
}
/* night mode */
/* to hide the defualt checkbox and align it to middle */
.toggle{
vertical-align: middle;
opacity:0;
}
/* giving night image for checkbox */
.toggle label{
background:url(night.png) left center no-repeat;
background-size: 1.5em;
padding-left: 1.5em;
}
/* giving day image for checkbox */
.toggle:checked label{
background:url(day.png) left center no-repeat;
background-size: 1.5em;
}
/* to align the checkbox and label to center, kinda works? but this just sends it more towards left*/
.aligner{
position: relative;
margin-left: 50vw;
margin-right: 50vw;
}
/* night/day toggle ends */
</style>
</head>
<body>
<form>
<!-- to align the checkbox and label -->
<div class="aligner">
<input type="checkbox" id="toggle" class="toggle" onclick="darkMode();">
<!-- added no breaking space becuase it was breaking -->
<label for=toggle id="toglab">Night Mode On</label>
</div>
<!-- main form -->
<fieldset id="fieldset">
<legend>Sign In</legend>
<!-- email -->
<label for="email">Email:</label>
<br>
<input type="email" name="email" id="email">
<br><br>
<!--email -->
<!-- password -->
<label for="password">Password:</label>
<br>
<input type="password" id="password">
<br>
<!-- password -->
<!-- show password -->
<br>
<label for="checkbox" id="showpass">Show Password:</label>
<input type="checkbox" id="checkbox" onClick=showPass();>
<!-- show password -->
<br><br>
<!-- Submit -->
<button type="submit">Submit</button>
</fieldset>
</form>
<!-- js starts -->
<script>
function showPass(){
var x=document.getElementById("password");
var y=document.getElementById("showpass");
if(x.type=="password"){
x.type="text";
y.innerHTML="Hide Password";
}
else{
x.type="password";
y.innerHTML="Show password";
}
}
function darkMode(){
var a=document.getElementById("toglab");
var b=document.getElementById("toggle");
var body=document.body;
var fieldset=document.getElementById("fieldset");
if(b.checked==true){
a.innerHTML="Day Mode On";
body.style.backgroundColor="white";
body.style.color="black";
fieldset.style.backgroundColor="white"
}
else{
a.innerHTML="Night Mode ON";
body.style.backgroundColor="black";
body.style.color="white";
fieldset.style.backgroundColor="black"
}
}
</script>
<!-- js ends -->
</body>
</html>
很抱歉粘貼長代碼,但我希望我已經提供了一切可能。我已經嘗試了一些以前的問題方法,例如 display-inline、display-flexbox 等,但它們似乎都不起作用。
使用 Div 將 Night Mode On 文本居中對齊,也使用 nbsp;否則它正在破裂。
uj5u.com熱心網友回復:
您可以使用display:flex;andjustify-content:center;將其定位在中心
.aligner{
position: relative;
display: flex;
margin-right: 6vw;
justify-content: center;
}
uj5u.com熱心網友回復:
我編輯了你的一些 CSS。彎曲它然后居中。讓我知道這是不是你的意思?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Righteous&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Amatic SC:wght@700&display=swap');
body{
font-family: 'Righteous', cursive;
/* font-family: 'Amatic SC', cursive; */
font-size: 1.3em;
color: white;
background-color: black;
}
/* making the font style same */
input,button{
font-family: inherit;
}
/* styling the field set and aligning to center */
fieldset{
max-width: max-content;
background-color: black;
margin-left: auto;
margin-right: auto;
}
/* night mode */
/* to hide the defualt checkbox and align it to middle */
.toggle{
vertical-align: middle;
opacity:0;
}
/* giving night image for checkbox */
.toggle label{
background:url(night.png) center no-repeat;
background-size: 1.5em;
}
/* giving day image for checkbox */
.toggle:checked label{
background:url(day.png) center no-repeat;
background-size: 1.5em;
}
/* to align the checkbox and label to center, kinda works? but this just sends it more towards left*/
.aligner{
position: relative;
display:flex;
justify-content:center;
}
/* night/day toggle ends */
</style>
</head>
<body>
<form>
<!-- to align the checkbox and label -->
<div class="aligner">
<input type="checkbox" id="toggle" class="toggle" onclick="darkMode();">
<!-- added no breaking space becuase it was breaking -->
<label for=toggle id="toglab">Night Mode On</label>
</div>
<!-- main form -->
<fieldset id="fieldset">
<legend>Sign In</legend>
<!-- email -->
<label for="email">Email:</label>
<br>
<input type="email" name="email" id="email">
<br><br>
<!--email -->
<!-- password -->
<label for="password">Password:</label>
<br>
<input type="password" id="password">
<br>
<!-- password -->
<!-- show password -->
<br>
<label for="checkbox" id="showpass">Show Password:</label>
<input type="checkbox" id="checkbox" onClick=showPass();>
<!-- show password -->
<br><br>
<!-- Submit -->
<button type="submit">Submit</button>
</fieldset>
</form>
<!-- js starts -->
<script>
function showPass(){
var x=document.getElementById("password");
var y=document.getElementById("showpass");
if(x.type=="password"){
x.type="text";
y.innerHTML="Hide Password";
}
else{
x.type="password";
y.innerHTML="Show password";
}
}
function darkMode(){
var a=document.getElementById("toglab");
var b=document.getElementById("toggle");
var body=document.body;
var fieldset=document.getElementById("fieldset");
if(b.checked==true){
a.innerHTML="Day Mode On";
body.style.backgroundColor="white";
body.style.color="black";
fieldset.style.backgroundColor="white"
}
else{
a.innerHTML="Night Mode ON";
body.style.backgroundColor="black";
body.style.color="white";
fieldset.style.backgroundColor="black"
}
}
</script>
<!-- js ends -->
</body>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/474057.html
標籤:javascript html css
