Marcus
Marcus

Reputation: 6717

How do I make this inner div stretch to 100 % width?

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

Answers (3)

j08691
j08691

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

random_user_name
random_user_name

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

Updated Fiddle

Upvotes: 1

Sam
Sam

Reputation: 10123

Add the following to the #header-index rule:

display:table;
width:100%;

Upvotes: 1

Related Questions