Flexbox Revisited: The New Syntax For Flexible Boxes | Van SEO Design |
Flexbox Revisited: The New Syntax For Flexible Boxes Posted: 26 Nov 2012 05:30 AM PST Last summer I wrote about the css flexible box model. At the time I mentioned how the spec was changing and offered syntax for both the old and “new” specs. Those “new” specs didn’t last long. A newer spec has taken it’s place and I wanted to run through flex layout model again to have more accurate information here. The current syntax I’ll be using in this post works only in Chrome at the moment and requires the -webkit vendor prefix even though I won’t be showing it here. I created a simple demo or rather me experimenting with a few properties. It’s nothing special, but you can get a feeling for the code involved. Remember though, it’s Chrome only at the moment. What Are the Benefits of Flexible Boxes?If support is still on the patchy side, then why should we care about flexible boxes? That word flexible should be a big clue. Unlike other types of boxes we can display, this one flexes. It changes as our layout changes, as the browser changes. Flexible boxes allow for automatic resizing of elements within a parent element that’s been resized. Has the word responsive come to mind yet? Flexible boxes help solve the problem of wanting a different source order when moving from the layout of phone sizes to the layout of widescreen displays. That alone would make them worthwhile in my opinion. Along the way they make vertical centering elements easier and they even give us a solution for equal height columns. They’ll generally make our lives easier and anyone who’s aware of them is probably jumping at the bit for browsers to support them to the point where we can use them in production. Flex Layout Box ModelFlex layout, joins the 4 previously introduced layout modes, block (documents), inline (text), table (tabular data), positioned (explicitly positioned elements). Flex is intended for the layout of complex applications and webpages. To understand how this all works take a look at the image above of 2 flex items inside a row flex container. There are 4 ideas we should take away from the image
. Flex Containers and Flex ItemsYou create a flex container by setting the display property on an element.
The above generates a block level and inline level flexbox respectively. Since this is a different layout model, flex containers aren’t exactly like block and inline boxes.
Flex containers hold 0 or more flex items that can also contain other elements. If a flex item is empty, holding only whitespace, it’s treated as though display: none were set on it. Ordering and OrientationWe have 4 properties to help us order and orient flex items.The first 3 are applied to the flex container and the last will get applied to the flex items. flex-direction — sets the direction of the main axis. There are 4 possible values
Rows set the main axis parallel to the inline axis and column sets it parallel to the block axis. By default the direction will be from start to end unless -reverse is used. flex-wrap — determines whether the flex container is a single or multiple line container. Here we have 3 values
Using nowrap sets the container as a single line container, while either of the wrap values sets it as multiline. The wrap value means lines flow from cross start to cross end, while the reverse is true with wrap-reverse as you probably guessed. When nowrap is set the flex items will remain on a single line even if that means overflowing the flex container. flex-flow — this 3rd property is actually a shorthand for the 2 above set in the following order: <flex-direction> | <flex-wrap> order — is set on the flex items and it takes an integer value. As you’d expect, it sets the order in which the flex items display. This single property solves the html source order problem. FlexibilityThere are 3 properties to determine how a a growing or shrinking flex container distributes free space to its flex items. These are set on the flex items.
flex-grow and flex-shrink take a number as a value, while flex-basis takes the width of a box or auto. The above properties work relative to one another. For example if you have 3 flex items with flex-grow and flex-shrink values of 1, 1, and 2 respectively, the last item will grow/shrink twice as much as the other two which will grow/shrink the same amount as the container expands and contracts. AlignmentFinally we have a few properties to align flex items and flex lines inside the flex container. To align flex items along the main axis you use the justify-content property (on the flex container), which comes with the following values
In the cross axis direction you use align-items and align-self.
Both properties can have any of 5 different values. The first 3, flex-start, flex-end, and flex-center are similar to the above. The 2 different values are:
The align-self property can also have a value of auto, which means use the default align-items value. Flex lines can be aligned within the flex container when there is extra space in the cross direction. The align-content property is used to do this with values similar to justify content. The one difference is the additional stretch value which stretches the lines from start to end. SummaryHopefully this new syntax will become the standard and hopefully browsers will adjust to adopt the new syntax sooner rather than later. The flexible box model offers us a lot of new options and helps solve quite a few problems we’re having when it comes to responsive design. If you’ve played around with flex boxes before the new syntax shouldn’t be too hard to work with. In many ways it maps to the old syntax. Once again I created a demo you can check out to see the code in action and how it ties together. Working with flexbox isn’t as complicated as I might have made it seem in this post, though I’d be lying if I said I could get everything to work as I expected. Have you played around with the news syntax? One of the older syntaxes? Just looking forward to the day we can use this in practice? The post Flexbox Revisited: The New Syntax For Flexible Boxes appeared first on Vanseo Design. |
You are subscribed to email updates from Vanseo Design » Blog To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google Inc., 20 West Kinzie, Chicago IL USA 60610 |