A horizontal navigation bar, or navbar for short, is a common design pattern that you'll notice at the top of many websites. Navigation links are displayed as a horizontal bar. In this guided practice, you'll explore some foundational flexbox concepts by positioning and sizing a navbar for the solar system!
- Each person in the group should clone this repository down to your local machine. This will not be submitted.
- Open the cloned folder with VS Code.
- Live serve
index.htmlto see the starting point for this guided practice. - Choose one person in your group to be the Driver initially. Everyone else will be a Navigator. These roles will swap after each section.
- As a team, read each question out loud and reach a consensus on the answer before moving to the next question.
- Every person in the team should follow along and type the answers on their own computers.
Note
This guided practice comes with some pre-written HTML and CSS to allow you to focus on flexbox. In particular, borders have been added to the elements to help visualize the effects of different flex properties.
Flex-direction determines the main axis of the flexbox. The cross axis runs perpendicular to the main axis.
- Style
navto be a flexbox by settingdisplaytoflex. - Set
flex-directiontocolumn, which will set the main axis to be vertical. Is the cross axis now horizontal or vertical? - How does the start and end of the main axis change if
flex-directionis set tocolumn-reverse? - Remove the
flex-directiondeclaration. What is the default value offlex-directionif it is not set? - Set the
gapto1ch. On which axis doesgapadd spacing?
Flex items can be repositioned along an axis. Justify will move items along the main axis, while align will move items along the cross axis. Items refers to the items themselves, while content refers to the spacing between the items.
Warning
In flexbox layouts, the property justify-items is ignored. See MDN for more information.
- According to the definitions above, what would the property
justify-contentchange? - What would the property
align-itemschange? - Style
navso that its children are evenly distributed along the main axis. Mercury should be flush with the left edge, and Neptune should be flush with the right edge. The children should also be centered along the cross axis.
Before continuing to the next section, select another person to be the Driver.
One major benefit to using flexbox is that the size of a flex item is flexible along the main axis. A flexbox container will keep track of the free space and distribute it according to the needs of its flex items. This means that a flex item can grow or shrink depending on the space available.
- Set the
flex-growproperty of#earthto1. What happens? - Set the
flex-growproperty of#marsto1. What happens? - Set the
flex-growproperty of#jupiterto2. What happens?
Currently, the total grow factor is
- What fraction of the free space do Earth and Mars get?
- What fraction of the free space does Jupiter get?
- Set the
flex-growproperty of#saturnto3. What fraction of the free space do Earth, Mars, and Jupiter now get? - To adjust the size of flex items, is the
flex-growproperty changed for the flexbox container or the flex items? - How does
gapaffect a flexbox's free space?
Note
flex-shrink works in very much the same way as flex-grow. Extra free space is distributed according to the grow factor of each item. If there is not enough space, then each item shrinks according to their shrink factor.
Before continuing to the next section, select another person to be the Driver.
A flexbox container can only rearrange its direct children. Flexbox containers can be nested in order to rearrange further descendants.
- In the HTML, wrap the links for the first four planets in a
sectionwith two classes:innerandplanets. Wrap the links for the last four planets in asectionwith two classes:outerandplanets. What happens? - Why do the previously-set grow factors no longer apply?
- Set the grow factors of the two sections so that the
outersection is twice as big as theinnersection. - Write a rule to flex all elements with the class
planets. This will cause the grow factors to be applied because each planet link is once again a direct child of a flex container. Why are the planet size ratios different from before? - What fraction of the inner planets' free space do Earth and Mars now take?
- What fraction of the outer planets' free space do Jupiter and Saturn now take?
At this point, our nav is a flexbox that rearranges the sections, each of which is also a flexbox that rearranges the individual links for each planet.
Before continuing to the next section, select another person to be the Driver.
Another common use case of flexbox is to center an element within its parent. This used to be much more difficult before flexbox, but now it's relatively straightforward!
- Look at the provided HTML file. What are the direct children of
body? - Flex the
bodyas acolumnand set the grow factor ofmainto1. What do you think this will change? Make this prediction before writing the CSS. - Did anything change? How much free space is available in
body? - Set the
heightofbodyto100vh. What happens now? - What is the relationship between a flexbox container's size, main axis, and free space?
- Put it all together! Style
mainso that the planet emoji is centered within its borders.



