This web content is a comprehensive guide on creating responsive layouts using CSS Flexbox, detailing its application, benefits, and how it integrates with media queries for adaptive design.
Abstract
The provided web content serves as "Chapter 14: Responsive Layouts with CSS Flexbox" from "A Complete Frontend Developer Textbook for Beginners (2023 Edition)." It introduces the concept of Flexbox as a powerful CSS module for easily aligning elements into rows or columns, making layout creation precise and efficient. The chapter covers the use of HTML tags such as header, main, and footer, and CSS properties like display, flex-direction, flex-wrap, and order. It also explains the @media rule for responsive design, demonstrating how to make layouts adapt to different screen sizes, particularly for mobile devices. The content emphasizes the ease of reordering elements, managing space distribution, and handling overflow with Flexbox, as opposed to traditional layout methods. Interactive elements such as quizzes, coding exercises, and flashcards are provided to reinforce learning and offer practical experience.
Opinions
The author expresses that using Flexbox simplifies the process of creating responsive layouts compared to previous
Chapter 14: Responsive Layouts with CSS Flexbox
A Complete Frontend Developer Textbook for Beginners (2023 Edition)
This allows our layout to take up the entireviewport.
For our header section, our main section, and our footer section, let’s divide them into threerows that always cover the entireviewport even after resizing.
header {
}
main {
}
footer {
}
To do this, each of these sections will have a width of 100vw:
header {
width: 100vw;
}
main {
width: 100vw;
}
footer {
width: 100vw;
}
As for the height, let’s make the header: 20vh
header {
width: 100vw;
height: 20vh;
}
In main, let’s make the height: 65vh
main {
width: 100vw;
height: 65vh;
}
And in footer, let’s make the height: 15vh
footer {
width: 100vw;
height: 15vh;
}
As long as they add up to 100vh, we can be confident that they take up the entireviewport.
height: 20vh + 65vh + 15vh = 100vh
To visualize these sections, let’s give them a border.
For the headerborder, let’s make it 5px, dashed, and green:
Without Flexbox, to do the same thing, we would have to rearrange the HTML code itself — not a best practice because that could adversely affect the semanticmeaning of our HTML code and downstreamdependencies in our CSS code and JavaScript code.
Now, let’s witness how easy it is to refactor the subsections of main to use Flexbox instead!
In programming, to refactor means to restructure our code to improve its readability, efficiency, and/or maintainability. It is a best practice in our industry which we will do constantly throughout this course.
In the next lecture, we will go over oneimportantuse case that makes this feature truly invaluable.
Media Queries
With Flexbox, we now have the ability to easily make our layout responsive.
What does that mean?
Our layout may look great on a tablet for example, but what if it’s on a smartphone where the screen is muchsmaller?
Wouldn’t it be cool when the viewport is below a certain width, our layout for mainautomaticallyconverts from a rowof boxes to a columnof boxes instead?
Using Flexbox, we can now easily make this possible!
To check if the viewportwidth is below a certain width, we can use a special CSS at-rule called @media:
@media ()
Then, inside the parentheses, we set the max-width to 480px for example:
@media (max-width: 480px)
This means that when the viewportwidth is 480px or below, we can tell it to execute whatever CSS statements we want inside the curly braces:
@media (max-width: 480px) {
}
For example, in the main, change the flex-direction to column:
@media (max-width: 480px) {
main {
flex-direction: column;
}
}
Let’s test it out on the UI window!
Make the widthabove480px… And then below480px…
Super cool, right? 😎
Since the flex property is so awesome, let’s study it in a little more detail.
The flex property is actually a shorthand that combines:
The flex-direction comes first, while the flex-wrap comes second.
Per CSS, when two rulesets have identical specificity, priority will be given to the ruleset that appears last. For this reason, we write our media query after the first main ruleset because we want the new main ruleset to replace it.
If we want the boxes that are too large or too many to wrap to the next row or column, we can use flex-wrap.
flex-wrap: wrap;
The flex-direction and flex-wrap can be combined using the flex-flowshorthand.
flex-flow: row wrap;
Using flex-grow, flex-shrink, and flex-basis on the children, we can control how the childrenboxes take up the parent container (total available space).
Flex-basis determines the initial sizeof the child box.
Flex-grow determines how many fractions of the parentcontainerthe child box can use.
Flex-shrink determines how many fractionsof theparent container the child boxshould NOT use.
Flex-grow and flex-shrink use fractionunits to make dividingup the parentcontainer intuitive.
If it’s not obvious how fractions work, the next lesson will explain them in more detail.
These flex properties can be combined using the flexshorthand.
flex: 11 auto;
Again, if these flex properties are still confusing, they will be covered in greater detail in Unit 3.
Media Queries
These flex properties affect the childrenboxes along the main axisonly. In effect, this makes it supereasy to switch the flex-direction whenever necessary.
flex-direction:row <-> column
Consequently, by combining Flexbox with Media Queries, we can easily make the layoutresponsive to anydevice sizes by changing the flex-direction from row to column, and vice versa.
@media (max-width: 480px) {
main {
flex-direction: column;
}
}
Flexbox also lets us decide how to distribute our childrenelements along both the main axis and cross axis, which will be discussed in Unit 3.
Concept Quiz
Take my Programming Concept Quiz to check your understanding! For every correct choice, you will earn SW Coins which you can redeem for coupons towards the purchase of any of my Udemy courses!
Sample Quiz Questions for Lesson 14:
Question 1:
A layout always consists of the following sectionsexcept:
header
main
section
footer
Question 2:
Which display type generates line breaksbefore and after the element?
inline
block
flex
grid
Question 3:
Which display type does NOT generate anyline breaks?
inline
block
flex
grid
Question 4:
Which display type is best for aligning elements along one direction?
inline
block
flex
grid
Question 5:
Which flex property is NOT declared on the parent element?
display
flex-direction
flex-wrap
flex
Question 6:
Which flex property is NOT declared on the children elements?
order
flex-grow
flex-shrink
flex-basis
flex-flow
Question 7:
Which flex value creates a minimum length of 100px?
flex: 1 1 100px;
flex: 0 1 100px;
flex: 1 0 100px;
Question 8:
Which flex value creates a maximum length of 100px?
flex: 1 1 100px;
flex: 1 0 100px;
flex: 0 1 100px;
Question 9:
Flexboxes apply only to the directchildren of a parent element.
Check out my Interactive Coding Exercises to put to practice what you have learned! There, you will also find interactive hints to help you understand each line of code. Likewise, for every correct solution, you will earn SW Coins which you can redeem for coupons towards the purchase of any of my Udemy courses!
Review what you have learned by playing my Syntax Flashcard Game! These flashcards are designed to help you commit to memory all the newcodesyntaxes you learned in this lesson. Likewise, for every correct answer, you will earn SW Coins which you can redeem for coupons towards the purchase of any of my Udemy courses!
With Flexbox, we can now easilyalignelements into rows or columns. But, what if we want to align them into rowsANDcolumns? In the next lesson, we will learn about an even more powerful CSS Module that lets us do just that!
When you use my referral link above 👆 to become a Medium member, all proceeds will be donated towards the construction of the Silicon Wat Campus for children in Ukraine and Cambodia ❤️