FCC---CSS Flexbox: Align Elements Using the justify-content Property
sometimes the flex items within a flex container do not fill all the space in the container. it is common to want to tell css how to align and space out the flex items a certain way. fortunately, the justify-content
property has several options to do this. but first, there is some important terminology to understand before reviewing those options.
here is a useful image showing a row to illustrate the concepts below.
recall that setting a flex container as a row places the flex items side-by-side from left-to-right. a flex container set as a column places the flex items in a vertical stack from top-to-bottom. for each, the direction the flex items are arranged is called the main axis. for a row, this is a horizontal line that cuts through each item. and for a column, the main axis is a vertical line through the items.
there are several options for how to space the flex items along the line that is the main axis. one of the most commonly used is justify-content: center;
, which aligns all the flex items to the center inside the flex container. others options include:
-
flex-start
: aligns items to the start of the flex container. for a row, this pushes the items to the left of the container. for a column, this pushes the items to the top of the container. this is the default alignment if nojustify-content
is specified. -
flex-end
: aligns items to the end of the flex container. for a row, this pushes the items to the right of the container. for a column, this pushes the items to the bottom of the container. -
space-between
: aligns items to the center of the main axis, with extra space placed between the items. the first and last items are pushed to the very edge of the flex container. for example, in a row the first item is against the left side of the container, the last item is against the right side of the container, then the remaining space is distributed evenly among the other items. -
space-around
: similar tospace-between
but the first and last items are not locked to the edges of the container, the space is distributed around all the items with a half space on either end of the flex container. -
space-evenly
: distributes space evenly between the flex items with a full space at either end of the flex container
an example helps show this property in action. add the css property justify-content
to the #box-container
element, and give it a value of center.
bonus
try the other options for the justify-content
property in the code editor to see their differences. but note that a value of center is the only one that will pass this challenge.
换上上面提到的几个属性,都可以操作,体会实际效果~
from:
to:
下一篇: 东莞五一去哪玩 东莞五一好去处