【问题标题】:Horizontally & Vertically centering 2 divs within two side by side 50% width columns在两个并排 50% 宽度的列中水平和垂直居中 2 个 div
【发布时间】:2016-05-04 06:13:33
【问题描述】:

很难让 2 个 div 在其各自的父级中水平和垂直居中,这些父级以 50% 的宽度和 100% 的高度并排放置。

这就是我所拥有的(Codepen 方便)。

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

【问题讨论】:

    标签: html css responsive-design vertical-alignment


    【解决方案1】:

    我试过这个。我不确定这是否是你要找的。我添加了 display: flex;到 .left 和 .right 类。

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

    【讨论】:

    • 解决了!在这里添加显示 flex 是正确的方法,我注意到您将高度作为像素值,这消除了任何垂直响应,虽然百分比值不起作用,但我记得 VH 单位可以。谢谢你的帮助!这是解决方案:codepen.io/jacobbanner/pen/mVLbbP?editors=1100
    • 雅各布一点问题都没有。很高兴我能帮上忙。
    猜你喜欢
    • 1970-01-01
    • 2015-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-16
    • 2012-06-05
    相关资源
    最近更新 更多