HTML CSS
Advanced
30 mins
Teacher/Student led
+75 XP
What you need:
Chromebook/Laptop/PC or iPad/Tablet

Introduction to Flexbox

Learn the basics of the Flexbox layout model and create a navigation bar using Flexbox properties.
Learning Goals Learning Outcomes Teacher Notes

Live Class Feed

This is a live feed of the latest activity by your students on this lesson. It will update in real-time as they work on the lesson.
Load previous activity

    1 - Understanding Flexbox

    Flexbox is a powerful layout model in CSS that makes it easy to arrange elements on a web page. It helps you control the alignment, direction, and sizing of elements within a container. Flexbox is especially useful for creating responsive designs that adapt to different screen sizes and orientations. In simpler terms, Flexbox helps you organize and position elements on your web page in a flexible and efficient way.

    In this lesson, we will learn the basics of Flexbox and use it to create a simple navigation bar. By the end of this lesson, you'll have a better understanding of how Flexbox works and how to use it in your own projects.


    2 - Create a Flex Container

    In this step, we will create add a <div> element that will act as our Flex container. Inside the container, add four <a> elements that will serve as navigation links. Your code should look like this:

    <div id="navbar">
      <a href="#">Home</a>
      <a href="#">About</a>
      <a href="#">Services</a>
      <a href="#">Contact</a>
    </div>

    3 - Add Basic CSS

    In this step, we will add some basic CSS to style our navigation bar. Add the following CSS code:

    #navbar {
      background-color: #333;
      padding: 10px;
    }
    
    #navbar a {
      color: white;
      text-decoration: none;
      padding: 8px 16px;
    }
    
    #navbar a:hover {
      background-color: #ddd;
      color: black;
    }

    This code sets the background color of the navbar to a dark gray (#333) and adds 10 pixels of padding around the navbar. The navigation links are styled with a white color, no text decoration (removing the default underline), and 8 pixels of padding on the top and bottom, and 16 pixels on the left and right. When a user hovers over a navigation link, the background color changes to a light gray (#ddd) and the text color changes to black, creating a visual effect that indicates the link is interactive.

    Try playing around with the values in the CSS and running the code to see the effects.

    4 - Make the Container a Flex Container

    In this step, we will make the <div> element a Flex container by setting its display property to flex. This will arrange all the direct children of the Flex container as Flex items, following the Flexbox layout rules.

    Add the following CSS code:

    #navbar {
      background-color: #333;
      padding: 10px;
      display: flex; /*add this*/
      flex-direction: column; /*add this*/
    }

    By default, Flex items are placed in a row. You can change the direction using the flex-direction property.

    You can also use row, column, row-reverse, or column-reverse as values for the flex-direction property.

    Try playing around with different values for flex-direction and running the code to see the effects.

    5 - Align Flex Items

    In this step, we will align the navigation links using the justify-content property. This property helps to align and distribute Flex items within the container. Change your CSS code to the following:

    #navbar {
      background-color: #333;
      padding: 10px;
      display: flex;
      flex-direction: row; /*change this*/
      justify-content: flex-end; /*add this*/
    }

    This code evenly distributes the space between the Flex items, creating a balanced navigation bar. You can also use other values like flex-start, flex-end, center, or space-around.

    Try playing around with different values for justify-content and running the code to see the effects.

    Unlock the Full Learning Experience

    Get ready to embark on an incredible learning journey! Get access to this lesson and hundreds more in our Digital Skills Curriculum.

    Copyright Notice
    This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more