html - 如何在 CSS 显示 : table-row occupy 50% of the row? 中制作 2 个单元格

标签 html css

我有以下 HTML:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <div id="def" style="margin-bottom: 4rem; margin-left: auto; margin-right: auto; margin-top: 7.25rem; width: 90%;">

        <div id="abc" style="display: table-row; width: 100%;">
            <div style="width: 50%; height: 20px; background-color: green;">
                I want this to fill almost 45% of the width of the screen
            </div>
            <div style="width: 50%; height: 20px; background-color: red;">
                I want this to fill almost 45% of the width of the screen
            </div>
        </div>

    </div>
</body>
</html>

我试过上面的这段代码,但表格行内的 DIV 似乎没有扩展宽度。如果可能的话,我希望在 id 为 abc 的 div 中解决方案,因为我无法更改 div def

的样式

最佳答案

您可以执行以下操作以获得所需的结果

FIDDLE DEMO

CSS

.container {
    margin-bottom: 4rem;
    margin-left: auto;
    margin-right: auto;
    margin-top: 7.25rem;
    width: 90%;
    border:2px solid;
    background:yellow;
}
.subcontainer {
    display: table-row;
    width: 100%;
}
.first {
    width: 50%;
    height: 20px;
    background-color: green;
    float:left;/***Use This***/

}
.second {
    width: 50%;
    height: 20px;
    background-color: red;
    float:right;/***Use This***/
}

或者你可以使用table-cell

FIDDLE DEMO

.first {
    width: 50%;
    height: 20px;
    background-color: green;
    display:table-cell;/***Use This***/

}
.second {
    width: 50%;
    height: 20px;
    background-color: red;
    display:table-cell;/***Use This***/
}

编辑:单独编写 CSS 总是更好

关于html - 如何在 CSS 显示 : table-row occupy 50% of the row? 中制作 2 个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25360937/

相关文章:

html - 媒体查询设置

javascript - 你如何运行只有一个 CSS 类的 javascript?

css - 将鼠标悬停在任一元素上时更改图像和背景颜色

html - 背景大小设置不能与背景图像分离吗?

javascript - 按钮禁用且 ngIf 在相同条件下工作

html - 从 HTML 中提取商品价格

html - 具有自动高度的两列布局

html - 如何固定导航和右侧 block ,同时保持整个容器居中?

Javascript 文本框复制/粘贴换行丢失

javascript - 当手动和以编程方式更改文本字段的值时,是否应该触发 change 事件?