
水平和垂直居中
实际上,以百分比为单位的高度和宽度会使居中更加容易。您只需将div左端和左端偏移一半的面积即可。
因此,如果您的身高是40%,则100%-40%= 60%。因此,您需要上下30%。其次是:30%的技巧。
仅水平居中
使用内联块。但是,此处的其他答案不适用于IE 8及更低版本。您必须为此使用CSS hack或条件样式。这是黑客版本:
.inlineblock { display: inline-block; zoom: 1; display*: inline; }编辑通过使用媒体查询,您可以结合两种技术来达到所需的效果。唯一的并发症是身高。您使用嵌套的div在%宽度和
@media (max-width: 1000px) { .center{} .center-inner{left:25%;top:25%;position:absolute;width:50%;height:300px;background:#f0f;text-align:center;max-width:500px;max-height:500px;}}@media (min-width: 1000px) { .center{left:50%;top:25%;position:absolute;} .center-inner{width:500px;height:100%;margin-left:-250px;height:300px;background:#f0f;text-align:center;max-width:500px;max-height:500px;}}欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)