gpt4 book ai didi

javascript - JS CSS HTML - 创建一个独立于文本数量而保持相同宽度的按钮

转载 作者:行者123 更新时间:2023-12-05 08:10:49 26 4
gpt4 key购买 nike

我有一个按钮,点击它会改变背景颜色。但是我希望按钮位于页面的中心(使用相对位置)但是当我尝试为按钮创建宽度时,另一个框会出现一个细边框。我想要一个大小相同的按钮,无论文本是什么,文本都居中(最多 20 个字符)

   <!DOCTYPE html>
<html lang="en">
<head>
<style>
top{
display: flex;
justify-content: space-around;
border: black 1px solid;
background-color: chartreuse;
}
.main{
text-align: center;
margin-top: 15%;
border: black 1px solid;
display: inline-block;
position: relative;
left: 40%;
}
button{
cursor: pointer;
}
#btn {
font-size: 2em;
font-weight: bold;
padding: 1em;
}
</style>
<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>Colour Flipper</title>
<link rel="stylesheet" href="colourflipper.css">
</head>
<body>
<div class="top">
<ul>Colour Flipper</ul>
<ul>Simple</ul>
<ul>Hex</ul>
</div>
<div class="main">
<button id="btn" onclick="myFunction();myyFunction()">
Background Colour
</button>
</div>
<script src="colourflipper.js"></script>
</body>
<script>
const colors = ["blue", "green", "yellow"];
let colorIndex = -1;

function myFunction(){
colorIndex += 1;
if (colorIndex > colors.length-1) colorIndex = 0;
document.body.style.backgroundColor = colors[colorIndex]
}
let colorSindex = -1;
function myyFunction(){
colorSindex +=1;
if (colorSindex > colors.length-1) colorSindex = 0;
document.querySelector('#btn').innerHTML = colors[colorSindex]
}
</script>
</html>

最佳答案

尝试考虑这个

可能对您有帮助

   <!DOCTYPE html>
<html lang="en">
<head>
<style>
top{
display: flex;
justify-content: space-around;
border: black 1px solid;
background-color: chartreuse;
}
.main{
text-align: center;
margin-top: 15%;
border: black 1px solid;
display: inline-block;
position: relative;
left: 40%;
}
button{
cursor: pointer;
}
#btn {
font-size: 2em;
font-weight: bold;
padding: 1em;
position: fixed;
width: 20%;
height: 20%;

}
</style>
<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>Colour Flipper</title>
<link rel="stylesheet" href="colourflipper.css">
</head>
<body>
<div class="top">
<ul>Colour Flipper</ul>
<ul>Simple</ul>
<ul>Hex</ul>
</div>
<div class="main">
<button id="btn" onclick="myFunction();myyFunction()">
Background Colour
</button>
</div>
<script src="colourflipper.js"></script>
</body>
<script>
const colors = ["blue", "green", "yellow"];
let colorIndex = -1;

function myFunction(){
colorIndex += 1;
if (colorIndex > colors.length-1) colorIndex = 0;
document.body.style.backgroundColor = colors[colorIndex]
}
let colorSindex = -1;
function myyFunction(){
colorSindex +=1;
if (colorSindex > colors.length-1) colorSindex = 0;
document.querySelector('#btn').innerHTML = colors[colorSindex]
}
</script>
</html>

关于javascript - JS CSS HTML - 创建一个独立于文本数量而保持相同宽度的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68960892/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com