【问题标题】:vertically align content within Chrome在 Chrome 中垂直对齐内容
【发布时间】:2016-08-28 06:29:14
【问题描述】:

我遇到了这样一种情况,即 flex 框在 Chrome 中无法按我希望的方式工作,但它在其他浏览器中工作(除了 iOS 移动设备)。

我正在努力垂直对齐 Chrome 中的任何内容,但适用于其他所有内容。有什么想法吗?

另外,有谁知道我可以动态地将 div 拉伸到 div 类内容的特定百分比的方法,这也可以在 chrome 中使用?

提前致谢。演示和屏幕截图见底部。

HTML

<div class="container">
  <div class="content">
    <h2>Ticket System <span style="color:#339933; font-weight:bold;">Open</span> Customer Ticket List</h2>
    <a class="BlueButton" href="ticket_view_cust_ticket.php">Open Customer Tickets</a>
    <a class="BlueButton" href="ticket_view_cust_ticket_rejected.php">Rejected Customer Tickets</a>
    <div class="centerContent">
      There are currently no open customer tickets
    </div>
  </div>
</div>

CSS

html,body
{
    height: 100vh;
}

body
{
   display: table; 
   margin: 0 auto;
   font-family: Tahoma,Arial,Sans-Serif;
}

.container
{  
  height: 98vh;
  vertical-align: middle;
    width: 70vw;
    min-width:1024px;
    margin-left:auto;
    margin-right:auto;
    margin-top: 1vh;
    display: flex;
    flex-direction: column;
}

.content
{
    background-color: #ff0000;
    flex: auto;
    webkit-flex-direction: column;
    -moz-flex-direction: column;
    -ms-flex-direction: column;
    -o-flex-direction: column;
    flex-direction: column;
    webkit-flex-shrink: 0;
    -moz-flex-shrink: 0;
    -ms-flex-shrink: 0;
    -o-flex-shrink: 0;
    flex-shrink: 0;
    text-align: center;
    font-size: 12px;
    padding-top:20px;
    min-height:600px;
}

.centerContent
{
    height: 95%;
    padding: 0;
    margin: 0;
    display: -webkit-box;
    display: -moz-box;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    align-items: center;
    justify-content: center;
}

演示 - https://jsfiddle.net/qr2tpgo6/1/

容器截图 - http://prntscr.com/azp8bk

火狐 - http://prntscr.com/azp4oj

Chrome - http://prntscr.com/azp4hy

【问题讨论】:

  • 而不是垂直对齐,使用 align-items : center;看看是否可行
  • 对齐项目:居中;没有影响。事实上,它只是搞砸了布局。还有@Epodax 好地方。我通常发布 PHP 问题,习惯的力量!

标签: html css flexbox


【解决方案1】:

您的容器缺少 display: flex,因此 flex 属性不起作用。

添加这个:

.content
{
    background-color: #ff0000;
    flex: auto;
    flex-direction: column;
    flex-shrink: 0;
    text-align: center;
    font-size: 12px;
    padding-top:20px;
    min-height:600px;

    display: flex;             /* new; establish flex container */
    justify-content: center;   /* new; center children vertically */
}

Revised Fiddle

【讨论】:

  • 这确实有效,但它并没有给我太多的控制权,因为 .container 在整个过程中都被使用,这就是为什么我希望使用单独的 div 来控制垂直对齐。
  • 您可以使用嵌套的弹性容器(在弹性项目上)将子元素垂直和水平居中。每个父母/孩子都可以这样居中。这可能会有所帮助:stackoverflow.com/a/33049198/3597276
猜你喜欢
  • 2016-10-19
  • 2015-03-07
  • 2014-03-04
  • 2012-08-28
  • 2016-11-20
  • 1970-01-01
  • 2011-04-28
  • 2015-06-21
相关资源
最近更新 更多