0

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%;
}
Marcus
  • 6,697
  • 11
  • 46
  • 89
  • 2
    Possible duplicate of [Why is width: 100% not working on div {display: table-cell}?](http://stackoverflow.com/questions/17736570/why-is-width-100-not-working-on-div-display-table-cell) – Sam Jan 07 '16 at 21:54

3 Answers3

1

Add the following to the #header-index rule:

display:table;
width:100%;
Sam
  • 9,933
  • 12
  • 68
  • 104
1

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%;
}

Updated Fiddle

random_user_name
  • 25,694
  • 7
  • 76
  • 115
1

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>
j08691
  • 204,283
  • 31
  • 260
  • 272