简体   繁体   中英

Problems with centering in CSS, HTML

I can't get to center a component perfectly I want this form to be in the middle and that works, but I can't get the background to be around it perfectly, I've tried millions of things can someone give me a hand? Thank you in advanced.

HTML:

    <!DOCTYPE html>
        <html>
        <head>
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
            <link rel="stylesheet" type="text/css" href="css/style.css">
            <title>Accounts</title>
        </head>
        <body>
            <div class="authform">
                <form class="my-form form-horizontal">
                    <div class="color">
                        <fieldset>

                            <label class="col-md-4 control-label" for="username"></label>  
                            <div class="col-md-5">
                                <center><input id="username" name="username" placeholder="Username" class="form-control input-md a-in" required="" type="text"></center>
                            </div>

                            <br><br><br>

                            <label class="col-md-4 control-label" for="password"></label>
                            <div class="col-md-5">
                                <center><input id="password" name="password" placeholder="Password" class="form-control input-md a-in" required="" type="password"></center>
                            </div>

                            <br><br><br>

                            <label class="col-md-4 control-label" for="send"></label>
                            <div class="col-md-4">
                                <button id="send" name="send" class="btn">Auth</button>
                            </div>

                        </fieldset>
                    </div>
                </form>
            </div>

            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
            <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

</body>
</html>

CSS:

.authform {

  padding-top: 15%;
  width: 50%;
  margin: auto;
  float: center;

}
.color {

  margin: auto;
  float: center;
  background-color: #2ecc71;
  border-radius: 25px;
  border: 2px solid #2ecc71;
  padding: 20px;

}

You might want to try FlexBox . In my opinion, it's the single best thing that ever happened to CSS.

See my CodePen for an example using your HTML.

These 3 lines of CSS are the most important:

display: flex;
align-items: center;
justify-content: center;

Edit : My CodePen might not look perfectly centered, but that's because height: 100vh doesn't really work in CodePen. But it'll work outside of CodePen.

Edit 2 : Updated codepen

Maybe you could try, in css:

text-align:center;
margin-left:(half of the Pixels of the screen) 
margin-top:(half of the Pixels of the screen)

Couldn't you simply apply the background and border to the .authform class or are you going for a different look?

.authform {
  padding: 15% 20px 20px;
  width: 50%;
  margin: auto;
  background-color: #2ecc71;
  border-radius: 25px;
  border: 2px solid #2ecc71;
}

You are using bootstrap for your inputs. Bootstrap's columns must be 12 in every row. In your case you have to add one div after every input.

<label class="col-md-4 control-label" for="username"></label>  
<div class="col-md-4">
    <center><input id="username" name="username" placeholder="Username" class="form-control input-md a-in" required="" type="text"></center>
</div>
<div class="col-md-4"></div>

or

<label class="col-md-3 control-label" for="username"></label>  
<div class="col-md-6">
    <center><input id="username" name="username" placeholder="Username" class="form-control input-md a-in" required="" type="text"></center>
</div>
<div class="col-md-3"></div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM