Horizontally & Vertically centering 2 divs within two side by side 50% width columns

瘦欲@ 提交于 2019-12-01 12:15:00

问题


Having some trouble getting 2 divs to center horizontally & vertically within their respective parents which are side by side with 50% width and 100% height.

Here's what I've got (Codepen for ease).

Codepen Link

*** 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;
}

回答1:


I tried this. I'm not sure if this is what you are looking for. I added display: flex; to the .left and .right classes.

.parent {
    height: 100%;
    width: 100%;
}

.left, .right {
    float: left;
    height: 500px;
    width: 50%;
    display: flex;
}

.info {
    width: 400px;
    height: 280px;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    margin: auto;
}


来源:https://stackoverflow.com/questions/35034903/horizontally-vertically-centering-2-divs-within-two-side-by-side-50-width-col

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!