css - 如何使这个内部 div 拉伸(stretch)到 100% 宽度?

标签 css html

我有一个 header 和两个 div 元素。外部 div 在我的 header 元素中垂直居中。我希望内部 div 拉伸(stretch)到 100% 宽度。我该怎么做呢?

检查 this fiddle以供引用。我希望蓝色部分占总宽度的 100%。

代码如下:

HTML:

<header id="header-index">
    <div id="header-index-inner">
        <div id="inner-div">

                <h1>Header</h1>               
                <h5>Subheader1 </h5>
                <h5>Subheader2 </h5>

        </div>
    </div>
</header>

CSS:

#header-index{    
    background-color: red;
}

#header-index-inner{   
    width: 100%;
    display: table-cell;
    vertical-align: middle;
    height: 400px;
}

#inner-div{
  background-color:blue;
  width:100%;
}

最佳答案

问题是 #header-index-innerdisplay: table-cell 没有拉伸(stretch) 100%。

父级需要分配display: table,以及宽度100%:

#header-index {    
    background-color: red;
    display: table;
    width: 100%;
}

#header-index-inner {   
    width: 100%;
    display: table-cell;
    vertical-align: middle;
    height: 400px;
}

#inner-div{
  background-color:blue;
  width:100%;
}

Updated Fiddle

关于css - 如何使这个内部 div 拉伸(stretch)到 100% 宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34665795/

相关文章:

html - UL列表网格堆叠奇怪

html - 悬停在第一个 child 身上会影响最后一个 child

html - 是什么导致元素不响应边距/填充?

javascript - 在 Fixed Header Uncaught TypeError top undefined 中

jquery - 更改字符串中文本的样式

javascript - 主体标签上的位置和缩放元素,背景大小设置为包含

javascript - 使用 css 在 react 选择中设置我的占位符样式

html - Rowspan 单元格大小不一样

html - 文本的固定位置背景剪辑

javascript - anchor 链接在 Accordion 列表中不起作用