-2

I am not sure how to centre the password input field, password text and submit button. I've tried text-align centre but I'm new and don't have enough experience to know a different way to do it.

Any help is appreciated!

note: if you refresh the page too often the api gets limited and the GIF disappears, i wouldve just changed it to an image for this question but i thought that might have something to do with it.

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

2 Answers2

0

You can make use of flexbox's align-items and justify-content properties:

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>
Spectric
  • 30,714
  • 6
  • 20
  • 43
-1

For the password css class add these values:

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

Make sure that the parent element is taking 100% of the windoe

Another approach would be to create display flex in .passwordcss and add

 .passwordcss > * {
margin: 0 auto;
}

Margin auto will align everything to the center, given parent is 100% width and display is flex

Dharman
  • 30,962
  • 25
  • 85
  • 135
MrGeet
  • 9
  • 1