22

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?

9
  • Also, if anyone with 1500+ reputation could create a css-flex tag, that would be great. Jan 5, 2014 at 5:18
  • 2
    The tag already existed, it's flexbox
    – vals
    Jan 5, 2014 at 18:27
  • @vals Thanks, it was crowded by all the Adobe/Macromedia Flex stuff. Jan 6, 2014 at 0:10
  • Why are you using Flexbox at all here? Calendars are tabular data.
    – cimmanon
    Jan 6, 2014 at 13:53
  • @cimmanon Mostly because I hate tables. And, depending on what you call data, my calendar won't contain any. Jan 6, 2014 at 18:08

1 Answer 1

26

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

1
  • 1
    Thanks for this - for automatically stretching the width, the key piece was setting the width of each flex child to 100%.
    – jbyrd
    Nov 4, 2015 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.