我使用bootstrap创建如下的UI:
------------------------------------------------ | | | div-outside | | ---------------------------- | | | | | | | | | | | div-inside | | | | | | | | | | | ---------------------------- | | | | | -------------------------------------------------
代码是:
<div id="app-layout-body" class="container-fluid body-content">
<div id="div-register" class="center-block" style="width:350px;margin-top:80px;">
....
</div>
</div>
要在内部div处设置内部div中心,我使用class =&#34; center-block&#34;。如果屏幕宽度超过特定数字(如1000px),则效果很好,但如果屏幕宽度不够大,则内部div将与外部div的右侧相邻:
---------------------------------------- | | | div-outside | | ---------------------------- | | | | | | | | | | left | div-inside | | | margin | | | | | | | | ---------------------------- | | | | | -----------------------------------------
使用Chrome的开发者工具,我发现当内部div不居中时,左边距将是固定数字(似乎有一个下限)。但是当屏幕的宽度在边界值之上变化时,左边距数量将根据宽度而变化,从而使内部div始终居中。实际上,当内部div不是水平居中时,如果我手动调整左边距数,则内部div将居中。
答案 0 :(得分:1)
Bootstrap .center-block {
display: block;
margin-right: auto;
margin-left: auto;
}
只包含
require("$_SERVER[DOCUMENT_ROOT]/connect.php");
应该有其他东西在你的容器中添加一些其他规则。从Inspector中,尝试检查您正在谈论的CSS(固定的左边距)是从哪个文件中添加的。
答案 1 :(得分:1)
你的例子正在运作。问题是如果屏幕宽度小于而不是350px,则布局不起作用,因为您选择了固定 宽度。
如果@Transient
应始终居中,请使用 cols 和偏移:
<div>
有关Bootstrap's grid offsetting的更多信息。
答案 2 :(得分:-1)
我认为这是你正在尝试做的事情。
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div id="app-layout-body">
<div id="div-register"><strong>I'm a centered Div inside another centered Div..</strong>
<p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked
up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus
Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a
line in section 1.10.32.</p>
</div>
</div>
</div>
&#13;
React.render
&#13;