html - CSS、HTML 居中问题

标签 html css

我无法让组件完美居中我一只手? 先谢谢你。

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;

}

最佳答案

您可能想尝试 FlexBox .在我看来,这是 CSS 有史以来最棒的事情。

查看我的 CodePen例如使用您的 HTML。

这 3 行 CSS 是最重要的:

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

编辑:我的 CodePen 可能看起来没有完全居中,但那是因为 height: 100vh 在 CodePen 中实际上不起作用。但它可以在 CodePen 之外工作。

编辑 2:更新代码笔

关于html - CSS、HTML 居中问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42820488/

相关文章:

javascript - 在 CSS/JS 中可以有一个半透明层覆盖另一个层而不会阻塞/捕获点击

css - HTML : sticky header jumps a little bit when scrolling

html - AngularJS 中的 ng-include 标签和 SVG 图像

jquery - Chrome 在使用 Flexie 时无法正确显示 div,(IE 工作正常)

android - HTML 5 应用程序将文件下载到与应用程序相同的目录

php - 如何从 imap_body 结果中仅提取 HTML

css 样式表作为组件参数/单个组件具有不同的样式表

jquery - slider 中垂直居中字幕

javascript - wasm/dotnet 完整性属性对于 Github 页面上的 Blazor 应用程序无效

css - 在 CSS 中声明一组样式时 "."和 "#"有什么区别?