Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS3 Flex - Stretch Width and Height

Tags:

html

css

flexbox

I'd like to make a full screen calendar application using the CSS3 Flex model. While I can make the calendar fit quite nicely horizontally, I can't find a way to also let the calendar weeks stretch in equal proportion to consume all the available height.

Here's a jsFiddle to illustrate my dilemma: http://jsfiddle.net/CgXLa/

The weeks are equally distributed until I make them display: flex;. Even if I encompass all of the days into a div element and make that display: flex; instead of each week, I still have the same problem.

How can I use the Flex model to stretch both horizontally and vertically?

like image 797
Micah Henning Avatar asked Jan 05 '14 05:01

Micah Henning


People also ask

How do you make a flex item take 100 height?

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

How do you set the height of a flex item?

Instead of max-height: 250px you should use flex: 0 0 250px and with flex-direction: column it will set height of element to 250px.

Can you set height of flexbox?

The main difference between flex & inline-flex is that the flex container takes the space needed to wrap its child elements not the full width of its parent. But you still can set the height/width of the inline-flex container.


1 Answers

Here is my solution.

I have removed an extra wrapper around weeks. Now the weeks and the th are all direct descendants of calendar

<main id="calendar">
    <section class="th">
        <span>Sunday</span>
        <span>Monday</span>
        <span>Tuesday</span>
        <span>Wednesday</span>
        <span>Thursday</span>
        <span>Friday</span>
        <span>Saturday</span>
    </section>
    <div class="week">
        <div></div>
        <div></div>
        <div></div>
        <div data-date="1"></div>
        <div data-date="2"></div>
        <div data-date="3"></div>
        <div data-date="4"></div>
    </div>
    ....

Now my CSS is

/* Calendar 100% height */
#calendar { height: 100%; 
    display: flex;
    flex-flow: column nowrap;
    align-items: stretch;
}

.th {
    flex: 30px 0 0;
} 
.week {
    flex: 30px 1 0;
    border-bottom: 1px solid #ccc;    
}

The key is to give the th element a definite height (flex-basis: 30px) but no flex-grow, and the week also some height to start with, but flex-grow: 1.

This makes the weeks take all the remaining space from calendar not used by th, evenly between them.

updated fiddle

like image 149
vals Avatar answered Sep 30 '22 14:09

vals