Last active
August 29, 2015 13:55
-
-
Save qmmr/8772255 to your computer and use it in GitHub Desktop.
Flexbox - justify-content & align-items (horizontal & vertical)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%header | |
%h1 Flexbox - main axis alignment | |
%article | |
%section.flex-container | |
.box.flex1 | |
%h3 One | |
.box.flex1 | |
%h3 Two | |
.box.flex2 | |
%h3 Three | |
.box.flex1 | |
%h3 Four | |
%section.flex-container.column | |
.box.flex1 | |
%h3 One | |
.box.flex1 | |
%h3 Two | |
.box.flex2 | |
%h3 Three | |
.box.flex1 | |
%h3 Four | |
%h1 Flexbox - cross axis alignment | |
%section.flex-container.cross-axis | |
.box.flex1 | |
%h3 One | |
.box.flex1.self | |
%h3 Two | |
.box.flex2 | |
%h3 Three | |
.box.flex1 | |
%h3 Four | |
%h1 Flexbox - margins don't collapse vertically | |
%section.flex-container.vertical | |
.box.flex1 | |
%h3 One | |
.box.flex1 | |
%h3 Two | |
.box.flex2 | |
%h3 Three | |
.box.flex1 | |
%h3 Four | |
%h1 Flexbox - trick with margin: auto | |
%section.flex-container.trick | |
.box.flex1 | |
%h3 Column One | |
.box.flex1 | |
%h3 Column Two | |
.box.flex2 | |
%h3 Column Three | |
%strong Fat | |
.box.flex1 | |
%h3 Sidebar |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ---- | |
// Sass (v3.3.0.rc.2) | |
// Compass (v1.0.0.alpha.17) | |
// ---- | |
@import "compass"; | |
@import url(http://fonts.googleapis.com/css?family=Audiowide); | |
// Documentation about Flex | |
// http://www.w3.org/TR/css3-flexbox/ | |
$colors: (chartreuse, royalblue, plum, teal, lightsteelblue, sienna, maroon, lime, cornflowerblue, darkviolet, dodgerblue); | |
@for $n from 1 through length($colors) { | |
.box:nth-child(#{$n}) { | |
background-color: nth($colors, $n); | |
} | |
} | |
body { | |
width: 80%; | |
margin: 0 auto; | |
padding: 2%; | |
line-height: 1.6; | |
} | |
.flex-container { | |
display: -webkit-flex; | |
display: -ms-flexbox; | |
display: flex; | |
margin: 1em 0; | |
border: 1px solid #333; | |
// flex-start, flex-end, space-between, space-around | |
@include justify-content(space-between); | |
// flex-flow is a shorthand for flex-direction & flex-wrap | |
&.row { | |
@include flex-flow(row); | |
} | |
&.column { | |
@include flex-flow(column); | |
height: 300px; | |
} | |
&.vertical { | |
@include flex-flow(column); | |
height: 400px; | |
& .box { | |
width: auto; | |
margin: 20px; | |
} | |
} | |
&.cross-axis { | |
height: 200px; | |
// default stretch | |
// flex-start, flex-end, stretch, baseline | |
@include align-items(stretch); | |
} | |
// by setting margin: auto on last child and 20px on all other | |
// we fill all the space with margin before the last element | |
&.trick { | |
height: 200px; | |
@include justify-content(flex-start); | |
.box { | |
margin: 20px; | |
&:nth-last-child(1) { | |
margin-left: auto; | |
} | |
} | |
} | |
} | |
.box { | |
font-family: 'Audiowide', cursive; | |
font-size: 1.8em; | |
line-height: 1; | |
margin: 0 5px; | |
padding: 10px 20px; | |
text-align: center; | |
} | |
.flex1 { | |
width: 100px; | |
.column & { | |
width: 100%; | |
} | |
} | |
.flex2 { | |
width: 200px; | |
.column & { | |
width: 100%; | |
} | |
.cross-axis & { | |
margin-top: 10px; | |
height: 100px; | |
} | |
} | |
.self { | |
@include align-self(flex-start); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.box:nth-child(1) { | |
background-color: chartreuse; | |
} | |
.box:nth-child(2) { | |
background-color: royalblue; | |
} | |
.box:nth-child(3) { | |
background-color: plum; | |
} | |
.box:nth-child(4) { | |
background-color: teal; | |
} | |
.box:nth-child(5) { | |
background-color: lightsteelblue; | |
} | |
.box:nth-child(6) { | |
background-color: sienna; | |
} | |
.box:nth-child(7) { | |
background-color: maroon; | |
} | |
.box:nth-child(8) { | |
background-color: lime; | |
} | |
.box:nth-child(9) { | |
background-color: cornflowerblue; | |
} | |
.box:nth-child(10) { | |
background-color: darkviolet; | |
} | |
.box:nth-child(11) { | |
background-color: dodgerblue; | |
} | |
body { | |
width: 80%; | |
margin: 0 auto; | |
padding: 2%; | |
line-height: 1.6; | |
} | |
.flex-container { | |
display: -webkit-flex; | |
display: -ms-flexbox; | |
display: flex; | |
margin: 1em 0; | |
border: 1px solid #333; | |
-webkit-justify-content: space-between; | |
justify-content: space-between; | |
} | |
.flex-container.row { | |
-webkit-flex-flow: row; | |
flex-flow: row; | |
} | |
.flex-container.column { | |
-webkit-flex-flow: column; | |
flex-flow: column; | |
height: 300px; | |
} | |
.flex-container.vertical { | |
-webkit-flex-flow: column; | |
flex-flow: column; | |
height: 400px; | |
} | |
.flex-container.vertical .box { | |
width: auto; | |
margin: 20px; | |
} | |
.flex-container.cross-axis { | |
height: 200px; | |
-webkit-align-items: stretch; | |
align-items: stretch; | |
} | |
.flex-container.trick { | |
height: 200px; | |
-webkit-justify-content: flex-start; | |
justify-content: flex-start; | |
} | |
.flex-container.trick .box { | |
margin: 20px; | |
} | |
.flex-container.trick .box:nth-last-child(1) { | |
margin-left: auto; | |
} | |
.box { | |
text-align: center; | |
} | |
.flex1 { | |
width: 100px; | |
} | |
.column .flex1 { | |
width: 100%; | |
} | |
.flex2 { | |
width: 200px; | |
} | |
.column .flex2 { | |
width: 100%; | |
} | |
.cross-axis .flex2 { | |
margin-top: 10px; | |
height: 100px; | |
} | |
.self { | |
-webkit-align-self: flex-start; | |
align-self: flex-start; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<header> | |
<h1>Flexbox - main axis alignment</h1> | |
</header> | |
<article> | |
<section class='flex-container'> | |
<div class='box flex1'> | |
<h3>One</h3> | |
</div> | |
<div class='box flex1'> | |
<h3>Two</h3> | |
</div> | |
<div class='box flex2'> | |
<h3>Three</h3> | |
</div> | |
<div class='box flex1'> | |
<h3>Four</h3> | |
</div> | |
</section> | |
<section class='flex-container column'> | |
<div class='box flex1'> | |
<h3>One</h3> | |
</div> | |
<div class='box flex1'> | |
<h3>Two</h3> | |
</div> | |
<div class='box flex2'> | |
<h3>Three</h3> | |
</div> | |
<div class='box flex1'> | |
<h3>Four</h3> | |
</div> | |
</section> | |
<h1>Flexbox - cross axis alignment</h1> | |
<section class='flex-container cross-axis'> | |
<div class='box flex1'> | |
<h3>One</h3> | |
</div> | |
<div class='box flex1 self'> | |
<h3>Two</h3> | |
</div> | |
<div class='box flex2'> | |
<h3>Three</h3> | |
</div> | |
<div class='box flex1'> | |
<h3>Four</h3> | |
</div> | |
</section> | |
<h1>Flexbox - margins don't collapse vertically</h1> | |
<section class='flex-container vertical'> | |
<div class='box flex1'> | |
<h3>One</h3> | |
</div> | |
<div class='box flex1'> | |
<h3>Two</h3> | |
</div> | |
<div class='box flex2'> | |
<h3>Three</h3> | |
</div> | |
<div class='box flex1'> | |
<h3>Four</h3> | |
</div> | |
</section> | |
<h1>Flexbox - trick with margin: auto</h1> | |
<section class='flex-container trick'> | |
<div class='box flex1'> | |
<h3>Column One</h3> | |
</div> | |
<div class='box flex1'> | |
<h3>Column Two</h3> | |
</div> | |
<div class='box flex2'> | |
<h3>Column Three</h3> | |
<strong>Fat</strong> | |
</div> | |
<div class='box flex1'> | |
<h3>Sidebar</h3> | |
</div> | |
</section> | |
</article> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment