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

JS CSS HTML - creating a button that maintains the same width independent of text amount

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

我省略了 display:inline-block 所以 .main 占据了整个宽度:

我也移动了 border: black 1px solid; 所以它现在属于 #btn.

我也给了#btn固定宽度

    .main{
    text-align: center;
    margin-top: 15%;
    position: relative;
}
button{
  cursor: pointer;

}
#btn {
    font-size: 2em;
    font-weight: bold;
    padding: 1em;
          border: black 1px solid;
          width:30%;

  }

我是这样做的:

 const colours = ['red', 'green', 'blue']
    let colorIndex = -1;
    function myFunction(){
        colorIndex += 1;
        if (colorIndex > colours.length -1) colorIndex = 0;
        document.getElementById("btn").innerHTML = colours[colorIndex]
        document.getElementById("btn").style.width="9%";
    }

    <!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>
        <link rel="stylesheet" href="colourflipper.css">
    </head>
    <body>
        <button id="btn" onclick="myFunction();myyFunction()">Background Colour</button>
    </body>
    <script src="colourflipper.js"></script>
    </html>

    button{
        position: relative;
        margin-top: 15%;
        margin-left: 48%;
        cursor: pointer;
    }

据我所知,您已将边框应用到 .main 而不是 #btn,这就是为什么您会在按钮周围看到一个框。

我将 .main 设为块元素,以便它覆盖整个宽度,并为按钮元素指定 30% 的固定宽度。

<!DOCTYPE html>
<html lang="en">
<head>
<style>
top{
    display: flex;
    justify-content: space-around;
    border: black 2px solid;
    background-color: chartreuse;
}
.main{
    text-align: center;
    margin-top: 15%;
    display: block; /* made it block to cover the whole width */
    position: relative;
    /* removed the border from here due to which you were seeing another box */
}
button{
  cursor: pointer;
}
#btn {
    margin: auto;
    font-size: 2em;
    font-weight: bold;
    padding: 1em;
    width: 30%; /* gave the button a fix  width */
    border: black 1px solid;
  }
</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>