我正在尝试使用bootstrap框架(Bootstrap 4 alpha)使应用程序登陆页面。在尝试将所有内容对齐时,我在jumbotron id中使用了text-align:center
。除了电子邮件之外的所有内容都是一致的。
是否有任何方法可以将电子邮件表单与中心对齐,并确保无论网页宽度如何,它都保持在中心位置。
这是样式表中的代码。
.jumbotron
{
background-image: url(background.jpg);
text-align: center;
margin-top: 50px;
}
这是主要代码。
<div class="jumbotron" id="jumbotron">
<h1 class="display-3">My Awesome App!</h1>
<p class="lead">This is why YOU should download this fantastic app!</p>
<hr class="m-y-2">
<p>Want to know more? Join our mailing list!</p>
<form class="form-inline" id = "emailbar">
<div class="form-group">
<label class="sr-only" for="email">Email address</label>
<div class="input-group">
<div class="input-group-addon">@</div>
<input type="email" class="form-control" id="email" placeholder="Your email">
</div>
</div>
<button type="submit" class="btn btn-primary">Sign up</button>
</form>
</div>
答案 0 :(得分:0)
由于您的表单有display: flex
,因此它会覆盖其父级的整个宽度。
此text-align: center
类中的jumbotron
也不会应用于表单元素,因为它们是弹性项目。
因此,在form-inline
班级
将其更改为display: inline-flex
(text-align: center
将适用)
添加justify-content: center;
(Flexbox属性在行方向水平对齐)
仅定位emailbar
,请执行此操作
#emailbar {
display: inline-flex;
}
或者
#emailbar {
justify-content: center;
}
答案 1 :(得分:0)
只需在jumbotron
中添加css部分即可获得该中心。
.jumbotron {
background-image: url(background.jpg);
text-align: center;
margin-top: 50px;
display: flex;/*Add this*/
flex-direction: column; /*Add this*/
align-items: center; /*Add this*/
}
<强> Working fiddle 强>
.jumbotron {
display: flex;/*Add this*/
flex-direction: column; /*Add this*/
align-items: center; /*Add this*/
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<div class="jumbotron" id="jumbotron">
<h1 class="display-3">My Awesome App!</h1>
<p class="lead">This is why YOU should download this fantastic app!</p>
<hr class="m-y-2">
<p>Want to know more? Join our mailing list!</p>
<form class="form-inline" id="emailbar">
<div class="form-group">
<label class="sr-only" for="email">Email address</label>
<div class="input-group">
<div class="input-group-addon">@</div>
<input type="email" class="form-control" id="email" placeholder="Your email">
</div>
</div>
<button type="submit" class="btn btn-primary">Sign up</button>
</form>
</div>
答案 2 :(得分:0)
没有理由增加额外的CSS。只需在表单上使用justify-content-center
...
https://www.codeply.com/go/7LjBkEtvVV
<form class="form-inline justify-content-center" id = "emailbar">
<div class="form-group">
<label class="sr-only" for="email">Email address</label>
<div class="input-group">
<div class="input-group-addon">@</div>
<input type="email" class="form-control" id="email" placeholder="Your email">
</div>
</div>
<button type="submit" class="btn btn-primary">Sign up</button>
</form>