简体   繁体   English

CSS,HTML居中问题

[英]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: 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: 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 . 您可能需要尝试FlexBox In my opinion, it's the single best thing that ever happened to CSS. 在我看来,这是CSS发生过的最好的事情。

See my CodePen for an example using your HTML. 有关使用HTML的示例,请参见我的CodePen

These 3 lines of CSS are the most important: 这三行CSS最重要:

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. 编辑 :我的CodePen可能看起来不完全居中,但这是因为height: 100vh在CodePen中实际上并不起作用。 But it'll work outside of CodePen. 但是它将在CodePen之外运行。

Edit 2 : Updated codepen 编辑2 :更新的Codepen

Maybe you could try, in css: 也许您可以在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类,还是要换一种外观?

.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. 引导程序的列每行必须为12。 In your case you have to add one div after every input. 对于您的情况,您必须在每次输入后添加一个div。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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