DevOverflow
DevOverflow

Reputation: 1702

How to set the height to 100% of the content inside the container?

How do I set the content inside container to the height of the parent block? How do you set the height to 100% for the content block that is inside the container, how do you deal with these situations?

body, html {
  margin:0;
  height: 100%;
}

.container {
  width:700px;
  margin:0 auto;
  padding: 0 15px;
}

.page_wraper {
  height:100%;
  display:flex;
  flex-direction:column;

}
.container {
  max-width:1000px;
  margin:0 auto;
}

header {
  flex: 0 0 auto;/*grow, shrink, basic*/
  height:50px;
  background:red;
  text-align:center;
  color:white
}

footer {
  flex: 0 0 auto;
  height:50px;
  background:black;
  color:white;
  text-align:center;
}

.content {
  color:white;
  background:grey;
  text-align:center;
  flex: 1 0 auto;
}

.inner_content {
  display:flex;
  height:100%;
  background:blue;
  flex-direction:column;
  justify-content: space-between;
}

.users {
  display:flex;
  justify-content:center;
  gap:20px;
}
.user_item {
  padding:10px;
  background:green;
}

.navigation {
  padding:20px;
}
<div class="page_wraper">
  <header>Header</header>
  <div class="content">
   <div class="container">
     <div class="inner_content">
       <div class="users">
         <div class="user_item">
           1
         </div>
          <div class="user_item">
           2
         </div>
          <div class="user_item">
           3
         </div>
       </div>
       <div class="navigation">
         <button>left</button>
         <button>right</button>
       </div>
     </div>
   </div>
  </div>
  <footer>Footer</footer>
</div>

I just need blue block inside container have 100% height of content so content height === inner_content height, but set 100% height to container isnt right way I suppose

Upvotes: 1

Views: 53

Answers (2)

tacoshy
tacoshy

Reputation: 13001

Add flexbox to the .content with flex-grow: .content { display: flex; flex-grow: 1; }

body, html {
  margin:0;
  height: 100%;
}

.container {
  width:700px;
  margin:0 auto;
  padding: 0 15px;
}

.page_wraper {
  height:100%;
  display:flex;
  flex-direction:column;

}
.container {
  max-width:1000px;
  margin:0 auto;
}

header {
  flex: 0 0 auto;/*grow, shrink, basic*/
  height:50px;
  background:red;
  text-align:center;
  color:white
}

footer {
  flex: 0 0 auto;
  height:50px;
  background:black;
  color:white;
  text-align:center;
}

.content {
  color:white;
  background:grey;
  text-align:center;
  flex: 1 0 auto;
  display: flex;
  flex-grow: 1;
}

.inner_content {
  display:flex;
  height:100%;
  background:blue;
  flex-direction:column;
  justify-content: space-between;
}

.users {
  display:flex;
  justify-content:center;
  gap:20px;
}
.user_item {
  padding:10px;
  background:green;
}

.navigation {
  padding:20px;
}
<div class="page_wraper">
  <header>Header</header>
  <div class="content">
   <div class="container">
     <div class="inner_content">
       <div class="users">
         <div class="user_item">
           1
         </div>
          <div class="user_item">
           2
         </div>
          <div class="user_item">
           3
         </div>
       </div>
       <div class="navigation">
         <button>left</button>
         <button>right</button>
       </div>
     </div>
   </div>
  </div>
  <footer>Footer</footer>
</div>

Upvotes: 4

Jason Ayala
Jason Ayala

Reputation: 152

.user_item {
 padding:10px;
 background:green;
 align-self: stretch;
}

This should stretch the entire Content and element to be same height as the parent. For reference Flexbox CSS Align-self

Upvotes: 1

Related Questions