javascript - 将左 div 垂直居中,同时保持右 div 标准
问题描述
我正在使用 bootstrap 4,我正在尝试使用该功能:在网页上找到垂直对齐:https ://getbootstrap.com/docs/4.0/layout/grid/
我已经阅读了我的代码几次,但无法弄清楚我做错了什么。
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<div class="pt-md-4 row justify-content-md-center">
<div class="col-md-5 col-lg-3">
<div class="col align-items-center">
<?php if($_POST){ if (isset($pin_sucess)){ ?>
<div class="alert alert-success" role="alert">
<h4 class="alert-heading">
<?php echo $pin_sucess; ?>
</h4>
</div>
<?php }} ?>
<form name="timeclock" action="<?=$_SERVER['REQUEST_URI']?>" method="POST">
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="pin">Pin</label>
<div class="col-sm-10">
<input type="text" name="pin" id="pin" class="form-control input-md" required="true">
</div>
</div>
<div class="form-group row">
<input type="hidden" name="csrf" value="<?php echo Token::generate();?>" />
<div class="col-sm-2 offset-sm-2">
<input type="submit" name="pin_submit" value="submit" class="btn btn-primary" />
</div>
</div>
</form>
</div>
</div>
<div class="col-md-7 col-lg-7">
Variable width content
<br />
<br /> Variable width content
<br />
<br /> Variable width content
<br />
<br /> Variable width content
</div>
</div>
页面上有 2 个 div,左侧的一个包含时钟表单,我希望它位于右侧的 div 之间。
我可能想更正正确的 DIV,如果它确实变长,它会变成右侧的滚动 div。
例如,如果右 div 长 2 英寸,那么左 div 将向下约 1 英寸左右。
解决方案
据我了解,您需要将表单 div(左侧)与右侧的内容 div(可以具有可变内容)垂直居中...为此,我刚刚介绍col align-self-center
了包含形式
完整的工作片段如下:
.higherDiv {
border: 1px solid black;
}
.higherDiv>.higherDiv {
border: 1px dashed lightblue;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<div class="pt-md-4 row justify-content-md-center higherDiv">
<!-- FORM DIV -->
<div class="col-md-5 col-lg-3 higherDiv col align-self-center">
<div class="col align-items-center">
<?php if($_POST){ if (isset($pin_sucess)){ ?>
<div class="alert alert-success" role="alert">
<h4 class="alert-heading">
<?php echo $pin_sucess; ?>
</h4>
</div>
<?php }} ?>
<form name="timeclock" action="<?=$_SERVER['REQUEST_URI']?>" method="POST">
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="pin">Pin</label>
<div class="col-sm-10">
<input type="text" name="pin" id="pin" class="form-control input-md" required="true">
</div>
</div>
<div class="form-group row">
<input type="hidden" name="csrf" value="<?php echo Token::generate();?>" />
<div class="col-sm-2 offset-sm-2">
<input type="submit" name="pin_submit" value="submit" class="btn btn-primary" />
</div>
</div>
</form>
</div>
</div>
<!-- VARIABLE CONTENT DIV -->
<div class="col-md-7 col-lg-7 higherDiv">
Variable width content
<br />
<br /> Variable width content
<br />
<br /> Variable width content
<br />
<br /> Variable width content
<br />
<br /> Variable width content
<br />
<br /> Variable width content
</div>
</div>
推荐阅读
- html - 浮动元素在包含块之外?
- javascript - 函数中逗号悬挂的 Eslint 解析错误
- python - 为什么我的多索引数据框具有重复的索引值?
- ios - 更改 UIDocumentBrowserViewController 创建文档按钮文本
- android - 将 Android App Bundle 上传到 Google Play 控制台 - 密钥签名错误
- spring - 使用 Tomcat(和 Spring REST)进行速率限制/节流
- git - 当我收到一个错误说存储库不存在(它确实存在)时,如何将本地存储库推送到 GitHub
- javascript - 如何访问外部摄像头?
- javascript - Javascript Regex:在逗号分隔的字符串中使用多个星号在两个星号之间捕获
- r - 如何在数据框中按列删除值?