Reputation: 6717
I have one header
, and two div
elements. The outer div
is vertically centered in my header
element. I want the inner div
to stretch to 100% width. How do i do this?
Check this fiddle for reference. I want the blue section to take up 100 % of the total width.
Here's the code aswell:
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%;
}
Upvotes: 0
Views: 782
Reputation: 208032
Instead of changing the display to use tables and table cells, you can use positioning to center the inner div with:
#header-index {
background-color: red;
}
#header-index-inner {
width: 100%;
height: 400px;
}
#inner-div {
background-color: blue;
top: 50%;
position: relative;
transform: translate(0%, -50%);
}
<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>
Upvotes: 1
Reputation: 26180
The issue is that #header-index-inner
is display: table-cell
, and it is not stretching 100%.
The parent needs to be assigned display: table
, as well as width 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%;
}
Upvotes: 1
Reputation: 10123
Add the following to the #header-index rule:
display:table;
width:100%;
Upvotes: 1