【问题标题】:JS CSS HTML - creating a button that maintains the same width independent of text amountJS CSS HTML - 创建一个独立于文本数量保持相同宽度的按钮
【发布时间】:2021-08-28 02:25:32
【问题描述】:

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

【问题讨论】:

    标签: javascript html css button


    【解决方案1】:

    尝试考虑这个

    可能对您有所帮助

       <!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>

    【讨论】:

    • @goosey9 您只需要设置按钮 ID 的宽度和高度,如果您觉得有帮助,请告诉我!!
    • 整页中运行以获得准确的结果
    【解决方案2】:

    据我所知,您为 .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>
    

    【讨论】:

      【解决方案3】:

      我省略了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%;
      
        }
      

      【讨论】:

      • 这对我不起作用。它有一个占据整个宽度的框,按钮内容只是背景色
      【解决方案4】:

      我可以这样做:

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

      【讨论】:

      • 我的解决方案对您有帮助吗??
      • 刚刚看到您的解决方案正在寻找,谢谢 XD
      • 嘿 Yash,为什么位置:固定;在这方面非常重要。例如,如果我使用相对位置,它会创建跨越边界并且看起来不太好。
      • position: fixed; 相对于视口定位,这意味着即使页面滚动,它也始终保持在同一位置。 top、right、bottom 和 left 属性用于定位元素。一个固定的元素不会在它通常所在的页面中留下空隙。
      • position: relative; 是相对于其正常位置定位的。设置相对定位元素的 top、right、bottom 和 left 属性将导致它远离其正常位置进行调整。其他内容不会被调整以适应元素留下的任何间隙。
      猜你喜欢
      • 2016-06-28
      • 2016-08-11
      • 1970-01-01
      • 2010-10-08
      • 1970-01-01
      • 2020-05-23
      • 2022-06-12
      • 2015-08-30
      • 1970-01-01
      相关资源
      最近更新 更多