简体   繁体   English

为什么百分比高度不适用于display:table-cell?

[英]Why doesn't percentage height work with display: table-cell?

I'm trying to center both horizontally and vertically a div inside an outer div. 我试图在水平和垂直方向上将div放在外部div中。 It works when the outer div has specific width and height set in pixels like 它适用于外部div具有特定的宽度和高度设置像素

#countdownBox {
    width: 700px;
    height: 500px;
    display: table-cell;
    vertical-align: middle;
    text-align: center;
}

but it fails when the height and width are percentages like 但是当高度和宽度都是百分比时,它会失败

#countdownBox {
    width: 100%;
    height: 100%;
    display: table-cell;
    vertical-align: middle;
    text-align: center;
}

Why is this and is there a work around? 为什么会这样,是否有解决方法?

EDIT Here is the HTML with container CSS: 编辑这是带有容器CSS的HTML:

#countdownBoxContainer {
width: 100%;
height: 100%;
position: absolute;
bottom: 0;
left: 0;
z-index: 3;
}

<div id="countdownBoxContainer">
    <div id="countdownBox">
      <div>
        hi there
      </div>
    </div>
 </div>

Based on the fact that you have width and height of 100% declared I'm going to assume that you're not expecting anyone to have to scroll here. 基于您宣布宽度和高度为100%这一事实,我将假设您不希望任何人必须在这里滚动。 See this Fiddle where I have a working solution based on those parameters. 看到这个小提琴 ,我有一个基于这些参数的工作解决方案。

Remember that display: table-cell; 记住display: table-cell; acts exactly like <td> elements and they won't render correctly unless they're in a <table> (or a container that is display: table; ). <td>元素完全相同,除非它们位于<table> (或者是display: table;的容器)中,否则它们将无法正确呈现。

The other problem is that <html> and <body> aren't necessarily the height of the screen if the content is very small. 另一个问题是,如果内容非常小, <html><body>不一定是屏幕的高度。 html, body { height: 100%; } html, body { height: 100%; } fixes this but it's a bit hacky. html, body { height: 100%; }修复这一点,但它是一个有点哈克。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有固定百分比的表格单元格不适用于许多项目 - Table-cell with fixed percentage doesn't work with many items display =“table-cell”用于垂直对齐不起作用 - display=“table-cell” for vertical alignment doesn't work “溢出:滚动”不适用于“显示:表格单元” - “Overflow:scroll” doesn't work for “display: table-cell” 垂直对齐不起作用(带有表格单元格显示) - Vertical-align doesn't work (with table-cell display) 如何给显示一个div:table-cell一个百分比高度? - How to give a div with display: table-cell a percentage height? 显示:表格的表格单元格宽度(百分比) - Display: table-cell width of box (percentage) 为什么没有display:none隐藏display:table-cell元素? - Why doesn't display:none hide a display:table-cell element? IE使用display:table-cell忽略100%高度div中的块div中的百分比高度 - IE ignores percentage height in block div inside 100% height div with display:table-cell 垂直对齐标签li的项目? (我也尝试过“显示:表格单元格”,但不起作用) - Vertical-align for item of tag li? (i tried also “display: table-cell” but doesn't work) 使用显示的基于高度,垂直对齐,基于百分比的img:表格/表格单元格 - Height-based, vertically-aligned, percentage-based img using display: table/table-cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM