18910140161

JavaScript-JS CSS HTML-创建一个与文本数量无关的保持相同宽度的按钮-堆栈溢出

顺晟科技

2022-10-19 11:48:06

167

我有一个按钮,当单击时,它会改变背景颜色。然而,我希望按钮在页面的中心(已经使用了相对位置),然而,当我试图为按钮创建一个宽度时,另一个框出现了一个薄边框。我想有一个按钮相同的大小和文本居中,无论什么文本(最多20个字符)


顺晟科技:

尝试考虑这个

可能对您有所帮助

我省略了so取全宽:

我还移动了它现在属于的。

我也给出了固定宽度。

   <!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;
  }
</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>

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

我将。main设置为块元素,这样它就覆盖了整个宽度,并给按钮元素设置了30%的固定宽度。

   <!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>
  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航