Intro to Flexbox



Intro to Flexbox

0 0


flex-talk


On Github arelia / flex-talk

Intro to Flexbox

  • Who here does front end, backend? Anyone who doesn’t develop for the web?
  • This talk is web-specific so feel free to step out if it you don’t find this relevant
  • Questions? Please find me after the CoderSpace talk which is immediately after this one

I’m Arelia

@arelia
Web Developer CareerBuilder
Instructor CoderSpace

What is Flexbox?

  • A CSS layout model
  • allows child elements of a container to be laid out vertically or horizontally
  • allows the child elements to shrink or grow (“flex”) their size based on unused space
  • Should be used for components, not whole page layouts (for which you might use the new grid properties)
See the Resources and Featured Customer areas of hiring.careerbuilder.com. Both use flexbox.
Here I've used mixins from the Bourbon CSS library that add vendor prefixes and older flexbox properties in addition to the modern properties.
.resource-grid {
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
}

@media only screen and (max-width: 40em) {
  .resource-grid {
    flex-wrap: nowrap;
    flex-direction: column;
  }
}
					
Here is the simplified code
  • display flex creates our flex layout
  • flex-wrap: wrap lets our content flow into multiple lines when needed
  • flex-direction: row sets our items in a row, then in our small-screen media query we set our items into a column and do not allow it to wrap
  • This is all set on our parent element
.resource-block {
  flex: 1;
}

@media only screen and (max-width: 64em) {
  .resource-block {
    flex: 1 0 46%;
  }
}

@media only screen and (max-width: 40em) {
  .resource-block {
    flex: 1;
  }
}
					
  • On our child elements, or flex-children, we are using the flex property to dictate the target size for each item and how much they can shrink or grow
  • flex: 1 is a shorthand that means elements have a 1:1 size ratio to each other, they should grow/shrink/target the same amount of space
  • flex: 1 0 46% means the items can grow and absorb any additional space, cannot shrink (0), and should have a target size of at least 46%
Here we're looking at the HTML for the featured customer area which is a little more complex
  • There is a .logo-grid flex parent around the entire area
  • The large box is given about 33% of space and stretches to the height of the small box area
  • the small box area has it's own flex-parent wrapper and the small boxes inside are flex children that maintain a uniform height and width
.logo-grid {
  display: flex;
  flex-wrap: nowrap;
  justify-content: center;
}

@media only screen and (max-width: 40em) {
  .logo-grid {
    flex-direction: column;
  }
}
						
  • Children are the large box and the box that wraps the smaller boxes
.logo-grid__testimonial--large {
  min-width: 30%;
  max-width: 33%;
}

@media only screen and (min-width: 40em) {
  .logo-grid__testimonial--large {
    flex-basis: 31.5%;
  }
}
						
						
.logo-grid__mini-wrap {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-content: space-between;
}

.logo-grid__testimonial--mini {
  width: 45.75%;
  flex-grow: 1;
}

@media only screen and (max-width: 64em) {
  .logo-grid__testimonial--mini {
    width:43%
  }
}

@media only screen and (min-width: 40em) {
  .logo-grid__testimonial--mini {
    flex-basis: 43%;
  }
}

@media only screen and (max-width: 40em) {
  .logo-grid__testimonial--mini {
    width:100%
  }
}
						
					
  • Tried table and bourbon/neat grid but felt like I had to right new declarations to override the built in rules and my code was messy and ultimately it wouldn’t give me equal width/height boxes regardless of the amount of text dont want to use grid because i want to reclaim more of the margin space for my content, in this case i need the space, but I don't want to guess and check for how much margin and box size i need, flexbox can just rearrange margins while I focus on content size

What is it good for?

  • Dynamic content
  • Responsive layouts
  • Uniform height/width across elements
  • (Easy) Vertical centering
  • Changing order
There are ways to achieve these results with traditional CSS but they are hacks. Flexbox gives us a straightforward way to meet these ends. If after listening to this talk you’re still not quite ready to go full force with flexbox check out Flexbox without Flexbox.

Getting Started

Begin with a flex container (the parent element)

  • Much of the flexibility can be achieved with just the properties applied to the parent
  • (At this point it helps to go to the CSS Tricks flexbox article and go down the line of properties)

Getting Started

Understand which is the main axis and cross axis

Demo

https://github.com/arelia/flex-talk/tree/master/example_files

Flex Parent Properties

Display: flex;

Sets you up for flexing

Flex Parent Properties

Flex-direction: row || row-reverse || column || column-reverse;

Arrange items horizontally or vertically, forwards or backwards order

Flex Parent Properties

Flex-wrap: nowrap || wrap || wrap-reverse;

Flex children stay in one line or go to multiple lines

Flex Parent Properties

Flex-flow: <flex-direction> <flex-wrap>;

Combine the direction and wrap properties With just those 3 (or 2 if we use shorthand) we’ve already got a responsive layout that will resize itself to any device

Flex Parent Properties

Justify-content: flex-start || flex-end || center || space-between || space-around;

Dictates how to distribute extra space on the main axis it's easier to see what justify-content does when there's no margin

Flex Parent Properties

Align-content: flex-start || flex-end || center || space-between || space-around || stretch;

Dictates how to distribute extra space on the cross axis it's easier to see with a border and extra height on the parent

Flex Parent Properties

Align-items: flex-start || flex-end || center || baseline || stretch;

Dictates how items on the same row/column should position and grow themselves in relation to each other

Flex Child Properties

Order: <integer>;

Reorder elements differently from the source order

Flex Child Properties

Flex-grow: <non-negative number>;

Allow individual element to take up extra space proportionately Can be set on individual or all child elements

Flex Child Properties

Flex-shrink: <non-negative number>;

Allow individual element to shrink proportionately

Flex Child Properties

Flex-basis: <length>;

Determines target size of an element before extra space is redistributed

Flex Child Properties

Flex: <flex-grow> <flex-shrink> <flex-basis>;

Shorthand for grow/shrink and basis. Recommended to use this to reset other properties

Flex Child Properties

Align-self: auto || flex-start || flex-end || center || baseline || stretch;

Overrides align-items set on the parent element
Supported by all modern browsers
With browser prefixes and including old properties you can acheive graceful degredation without using a polyfil such as flexibility

Resources

CSS Tricks Guide to Flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/Flexbox Froggy: http://flexboxfroggy.com/Guide to Flexbox without Flexbox: https://kyusuf.com/post/almost-complete-guide-to-flexbox-without-flexboxRachel Andrew: https://rachelandrew.co.uk/Jen Simmons: http://jensimmons.com/

Chicago HTML5 Meetup

Tuesday, June 14 @ 6:00pm

Enova

Lunch and Learn – Teaching in the Cloud

Room 621

Thank You

Intro to Flexbox Who here does front end, backend? Anyone who doesn’t develop for the web? This talk is web-specific so feel free to step out if it you don’t find this relevant Questions? Please find me after the CoderSpace talk which is immediately after this one