Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS flex box with a fixed height still stretches [duplicate]

Tags:

css

flexbox

I'm trying to create a header with two columns that will fill to the parent container's height. I'm using display:flex for a responsive layout. When I set the header to have a fixed height, the two other sub-divs have a gap between them and the header.

div#container {
    padding:20px;
    background:#F1F1F1;
    display: flex;
    flex-flow:row wrap;
    height:540px;
}
.header{width:100%; height:40px; background:#ccc;}
.content {
    width:150px;
    background:#ddd;
    padding:10px;
    margin-left: 10px;
}

enter image description here

Fiddle

EDIT

The duplicate question is a nice description of the flex properties, but I'm not seeing an example like this addressed in it. If the height:40px is removed from the header, the header div will stretch down to the other divs. When the header height is specified, a gap exists.

like image 543
jim31415 Avatar asked Dec 18 '17 16:12

jim31415


People also ask

How do I stop my flexbox from stretching?

Fortunately, the solution is simple. You just need to replace your image/flex item's align-self property's default stretch value with another value. Instead of stretch you can use center , which will remove the image stretching, and vertically align your image in the middle of its parent container.

How do I make my Flex Box 100% height?

Getting the child of a flex-item to fill height 100%Set position: relative; on the parent of the child. Set position: absolute; on the child. You can then set width/height as required (100% in my sample).

Can you set height of flexbox?

By default, a flex container has the following width and height assigned. width: auto; height: auto; But you can set a fixed height and width to the flex container.

How do I make my flex element not shrink?

default flex-shrink: 1; If there's not enough space available in the container's main axis, the element will shrink by a factor of 1, and will wrap its content. flex-shrink: 0; The element will not shrink: it will retain the width it needs, and not wrap its content.


1 Answers

align-content default value is stretch. Change that to align-content: start; and it should be okay.

.content-wrapper{
  display: flex;
  flex: 1;
}

div#container {
    padding: 20px;
    background: #F1F1F1;
    display: flex;
    flex-flow: column wrap;
    align-content: start;
    height: 540px;
    border: 1px solid red;
    box-sizing: border-box;
}
.header{width:100%; height:40px; background:#ccc;}
.content {
    width:150px;
    background:#ddd;
    padding:10px;
    margin-left: 10px;
}
<div id="container">
<div class="header">
HEADER
</div>
<div class="content-wrapper">
    <div class="content">
         <h1>Title 1</h1>

        <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,...
        </div>
    </div>
    <div class="content">
         <h1>Title 2</h1>
        <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,...
        </div>
    </div>
    </div>
</div>
like image 161
Kushtrim Avatar answered Sep 22 '22 23:09

Kushtrim