bootstrap 如何让div内部垂直居中?

选中要居中的元素,然后设置其css属性:transform: translateY(50%);

在这里插入图片描述
代码:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    <title>Bootstrap 101 Template</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
    <script src="js/jquery-3.2.1.min.js"></script>
    <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
    <script src="js/bootstrap.min.js"></script>
    <style>
        /*将整个登入的div垂直居中*/
        #all {
            transform: translateY(50%);  /*向下移动自身的一半*/
            background-color: white;
            height: 390px;
        }
        body {
            background-image: url("img/1.png");
            /*整个图片作为背景,不够自动拉伸*/
            background-size: cover;
        }

    </style>

</head>
<body>
<div class="container">
    <div class="col-md-offset-3 col-sm-6" id="all">
        <h2 style="text-align: center">管理员登入</h2>
        <form class="form-horizontal">
            <!--设置用户名-->
            <div class="form-group">
                <label for="userName" class="col-sm-2 control-label col-md-offset-2">用户名:</label>
                <div class="col-sm-6">
                    <input type="text" class="form-control" id="userName" placeholder="请输入用户名">
                </div>
            </div>
            <!--设置密码-->
            <div class="form-group">
                <label for="passwd" class="col-sm-2 control-label col-md-offset-2">密码:</label>
                <div class="col-sm-6">
                    <input type="password" class="form-control" id="passwd" placeholder="请输入密码">
                </div>
            </div>
            <!--验证码-->
            <div class="form-group">
                <label for="captcha" class="col-sm-2 control-label col-md-offset-2">验证码:</label>
                <div class="col-sm-3">
                    <input type="text" class="form-control" id="captcha" placeholder="请输入验证码">
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-4 col-sm-6">
                    <div class="checkbox">
                        <label>
                            <input type="checkbox"> 保存密码
                        </label>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-4 col-sm-4">
                    <button type="submit" class="btn btn-default btn-primary btn-block">登入</button>
                </div>
            </div>
        </form>
        <!--设置警告-->
        <div class="form-group">
            <div class="col-sm-offset-3 col-sm-6">
                <div class="alert alert-warning alert-dismissible" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <strong> 登入失败!</strong>
                </div>
            </div>
        </div>
    </div>

</div>
</body>
</html>
  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ElegantCodingWH

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值