html - 即使父表格单元格具有高度,CSS 中的高度 100% 也会给出 0

标签 html css html-table

<分区>

我有以下结构:

<th class="condition">
    <div class="progress_container">
        <div class="bar_container">
            <span class="progress_green" style="width:14%;"></span>
            <span class="progress_red" style="width:86%;"></span>
        </div>
        <div class="progress_text">14% %</div>
    </div>
</th>

我在表格中有一个 th 元素,类“condition”,chrome 说它的宽度和高度为 132.5 x 40。然后我在它里面有一个 div,类“progress_container”,它的宽度和高度都设置为100%,但出于某种奇怪的原因,结果是 132.5 x 0。正因为如此,其他一切都崩溃了(每个 child 的高度都是 0%)。为什么是这样?如何让父元素的高度起作用?

这是相关的 CSS:

.progress_container{
    position: relative;
    width: 100%;
    height: 100%;
}

.bar_container{
    position: absolute;
    text-align: center; 
    vertical-align: top;
    width: 101%;
    height: 100%;
    top: -0.5%;
    left: -0.5%;
}

.progress_green{
    display: inline-block;
    background-color: #33ff33;
    height: 25px;
    border-radius: 2px 0px 0px 2px;
    clear:both;
}

.progress_red{
    display: inline-block;
    background-color: #ff3333;
    height: 25px;
    border-radius: 0px 2px 2px 0px;
    clear:both;
}

.progress_text{
    position: absolute;
    text-align: center; 
    vertical-align: top;
    top: 5px;
    width:100%;
}

最佳答案

回答:

.bar_container {
   position: relative;
}

你有两个连续的绝对定位元素,应该有一个相对的,子元素可以是绝对的。您可以拥有更多绝对子级,但级别相同。

关于html - 即使父表格单元格具有高度,CSS 中的高度 100% 也会给出 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52052918/

上一篇:Javascript Jquery 向下滚动 1 页

下一篇:javascript - 在 app-root 中垂直对齐我的加载程序 - Angular2+

相关文章:

css - 如何阻止大字体超出 div?

javascript - jQuery 颜色缩放 html 表列

html - CSS:加载 SVG 和相对于其元素的位置

html - 是否可以使用仅 CSS 的容器包装器?

html - mailto href 是否曾被视为已访问?

jquery - 如何根据是否选中来设置复选框标签内的 DIV 样式?

jquery - 我怎样才能使这张图片始终位于div的中心?

javascript - jquery getElementsByClassName 查找部分类名

html - 将边框应用于 tfoot 的 CSS 选项?使用 "border-top"设计 tfoot 不起作用

javascript - 在 html 表中通过箭头键导航