-2

我不确定如何将密码输入字段、密码文本和提交按钮居中。我尝试过 text-align center 但我是新手,没有足够的经验来了解不同的方法。

任何帮助表示赞赏!

注意:如果您过于频繁地刷新页面,则 api 会受到限制并且 GIF 会消失,我会为这个问题将其更改为图像,但我认为这可能与它有关。

<!doctype HTML>
<html>
<head>
    <meta name="websiteinnit" content="width=device-width, initial-scale=1">
    <link rel=icon href="https://i.imgur.com/zWdbQf2.png">

  <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
  <script>
  $(function() {
    var xhr = $.get("http://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=no");
    xhr.done(function(data) { 
      $('.gif-bg').css('background-image', 'url(' + data.data.image_url + ')');
    });
  });
  </script>

</head>
<body>
  <div class = "usernamecss">


  
<div class = "passwordcss">

<label class = "password" for = "password">Password:</label><br>
<input class = "passwordinput" type= "Password" id="password" name="password">
  
</div>
</form>
<div-1>
  <input class = "submitbutton" type="submit" value="Submit">
</div-1>

<div class="gif-bg">
</div>

<style>
  body {
    margin: 100px;
    }
  .gif-bg {
    background:url('') no-repeat center center; 
    min-height:10%;
    min-width:10%;
    Padding: 50px;
    }

   
  .password {
    position:relative;
    font-family: monospace;
    font-size: 15px;
    text-align:center;
    outline:10px black;
    position:absolute;
  }
  .passwordinput {
    border-style: inset;
    border-radius: 4px;
    margin:4px;
    text-align:center;
  }
  .submitbutton {
    border-style: bold;
    border-radius: 4px;
    background-color: #BDD9BF;
    transition-duration: 0.4s;
    margin:4px;
  }
  .submitbutton:hover {
  background-color: grey;
  color: grey;
  }
  </style>
</body>
</html>

4

2 回答 2

0

您可以使用flexboxalign-itemsjustify-content属性:

html,
body {
  height: 100%;
  margin: 0;
}

body {
  display: flex;
  align-items: center;
  justify-content: center;
}
<!doctype HTML>
<html>

<head>
  <meta name="websiteinnit" content="width=device-width, initial-scale=1">
  <link rel=icon href="https://i.imgur.com/zWdbQf2.png">

  <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
  <script>
    $(function() {
      var xhr = $.get("http://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=no");
      xhr.done(function(data) {
        $('.gif-bg').css('background-image', 'url(' + data.data.image_url + ')');
      });
    });
  </script>

</head>

<body>
  <div class="usernamecss">
    <div class="passwordcss">
      <label class="password" for="password">Password:</label><br>
      <input class="passwordinput" type="Password" id="password" name="password">
    </div>
    <div-1>
      <input class="submitbutton" type="submit" value="Submit">
    </div-1>
    <div class="gif-bg">
    </div>
    </div>
    <style>
      body {
        margin: 100px;
      }
      
      .gif-bg {
        background: url('') no-repeat center center;
        min-height: 10%;
        min-width: 10%;
        Padding: 50px;
      }
      
      .password {
        position: relative;
        font-family: monospace;
        font-size: 15px;
        text-align: center;
        outline: 10px black;
        position: absolute;
      }
      
      .passwordinput {
        border-style: inset;
        border-radius: 4px;
        margin: 4px;
        text-align: center;
      }
      
      .submitbutton {
        border-style: bold;
        border-radius: 4px;
        background-color: #BDD9BF;
        transition-duration: 0.4s;
        margin: 4px;
      }
      
      .submitbutton:hover {
        background-color: grey;
        color: grey;
      }
    </style>
</body>

</html>

于 2021-07-25T23:36:24.527 回答
-1

对于密码 css 类,添加以下值:

        .passwordcss {
    display: flex;
    justify-content: center;
width: 100%;
        }

确保父元素占用 100% 的 windows

另一种方法是在 .passwordcss 中创建 display flex 并添加

 .passwordcss > * {
margin: 0 auto;
}

边距自动将所有内容对齐到中心,给定父级为 100% 宽度且显示为 flex

于 2021-07-25T23:31:50.837 回答