我想知道是否有人对调整页面大小时如何最好地迫使两个div居中提出建议。
这是相关的html:
<div id="body">
<div id="top"></div>
<div id="content">
<div id="box1"></div>
<div id="box2"></div>
</div>
<div id="footer"></div>
</div>
我一般都希望箱1和箱2的div对齐到左侧和右侧的对方。因此,我使用以下CSS:
#box1, #box2
{
float:left;
max-width:25em;
min-width:20em;
width:45%;
}
我要解决的问题是如何最好地使它们堆叠,然后在调整页面大小时居中。所需的外观是:
我知道我使用这样的媒体查询:
@media screen and (max-width: 800px)
{
#box1, #box2
{
/* SOMETHING */
}
}
以下内容应放在您的媒体查询中:
float: left;
。这是因为div
s是block
元素,将自动移至新行div
s,width
所以添加margin: 0 auto;
将使它们居中#box1 {
background-color: red;
}
#box2 {
background-color: blue;
}
#box1, #box2 {
float: left;
max-width: 25em;
min-width: 20em;
width: 45%;
}
@media screen and (max-width: 800px) {
#box1, #box2 {
float: none;
margin: 0 auto;
max-width: 25em;
min-width: 20em;
width: 45%;
}
}
<div id="box1">Box 1</div>
<div id="box2">Box 2</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句