【发布时间】:2016-05-04 06:13:33
【问题描述】:
很难让 2 个 div 在其各自的父级中水平和垂直居中,这些父级以 50% 的宽度和 100% 的高度并排放置。
这就是我所拥有的(Codepen 方便)。
*** HTML ***
<div class="parent">
<div class="left">
<div class="info">
<h1>This div should be centered <br>within the left 50%</h1>
<h2>Get in touch:</h2>
<h1>
+44 (0)1323 567 891<br>
+44 (0)1323 132 363<br>
info@somecompany.co.uk<br>
</h1>
</div>
</div>
<div class="right">
<div class="img-wrapper">
<img src="http://placehold.it/210x210/FE5000/FFF"/>
</div>
</div>
</div
*** CSS ***
.parent {
height: 100%;
width: 100%;
}
.parent:before, .parent:after {
display: table;
content: " ";
}
.parent:after {
clear: both;
}
.left, .right {
float: left;
height: 100%;
width: 50%;
}
.info {
width: 400px;
height: 280px;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
}
.right .img-wrapper {
width: 210px;
height: 210px;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: 0 auto;
}
【问题讨论】:
标签: html css responsive-design vertical-alignment