CSS Flexbox for Responsive Design

Introduction:

Welcome to the exciting world of responsive design with CSS Flexbox. As we dive into this innovative layout model, we are stepping into an area of front-end development that is essential for Web3 applications.

Flexbox offers a more efficient way to lay out, align, and distribute space among items in a container, even when their size is unknown or dynamic.

Remember, watching the lesson video will offer a deeper understanding of this topic.

Learning Objectives:

By the end of this lesson, you will be able to:

  1. Understand the fundamentals of Flexbox and how it differs from traditional layout techniques.
  2. Know how to use Flexbox properties to create responsive layouts.
  3. Be equipped to apply Flexbox to the Token Minter dApp for enhanced responsiveness.

Lesson Video:


What Is CSS Flexbox?

Flexbox is a one-dimensional layout method for laying out items in rows or columns.

Items flex to fill additional space and shrink to fit into smaller spaces.

Implementing Flexbox:

Applying Flexbox to your projects can significantly improve the user experience by making your layouts more adaptable to different screen sizes.

Let’s apply Flexbox to our Token Minter dApp, enhancing its responsiveness and ensuring it looks great on any device.

Example Codes for Practical Application:

Expanding the Capstone Project

Expanding on our Token Minter dApp, let’s enhance its features using what you learned from this lesson.

Existing Capstone Project Code from Previous Lesson:

CSS Code:

/* External CSS */

/* IMPORTS */

/* Importing a custom Google font */
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');



/* BODY */

/* Basic styling for the body */
body {
    font-family: 'Roboto', sans-serif; /* Applying the imported font family to the body */
    background-color: #f4f4f4;
    background: linear-gradient(to right, #630404, #e040fb, #6a027c); /* Darker brown to lighter purple gradient background */
}


/* NAVIGATION */

/* Styling navigation menu using child combinator */
nav > ul > li {
    display: inline;
    margin-right: 20px;
    text-align: center; /* Aligns text to the center */
}

/* Styling the navigation menu with a hover effect */
nav a:hover {
    background-color: #630404; /* Light background on hover */
    color: #f3e40d; /* Dark text on hover */
}

/* Styling links using attribute selector */
a[href^="#"] {
    text-decoration: none;
    color: white;
}



/* HEADINGS */

/* Styling for the header using element selector */
header {
    background: #333; /* Dark background for the header */
    color: white; /* Light text for contrast */
    padding: 10px;
    text-align: center;
}

/* Styling headings for emphasis */
h1, h2, h3 {
    font-weight: 700; /* Makes the font bold */
}

/* Styling paragraph using adjacent sibling combinator */
h2 + p {
    color: #cfbdbd;
}      



/* MAIN */

/* Styling for the main content area */
main {
    margin: 20px;
    border: 2px solid #333;
    padding: 10px;
    display: grid;
    grid-template-columns: 2fr 3fr; /* One column for the aside, three for the main content */
    gap: 20px; /* Space between grid items */
}


/* ARTICLE */

/* Styling for the article */
article {
    margin-bottom: 15px;
    padding: 10px;
    border: 1px solid grey;
}
/* Applying Box Model to the paragraph */
article p {
    margin-bottom: 20px;
    border: 1px solid grey;
    padding: 15px;
}



/* IDs */

/* Targeting specific section with ID selector */
#mint {
    background-color: #eaeaea;
    padding: 15px;
    margin-top: 10px;
}

/* Styling for the form inside the mint section */
#mint form {
    padding: 10px;
    border: 1px solid #ddd;
    margin-top: 10px;
}



/* PARAGRAPH */

/* Adjusting paragraph line spacing for readability */
p {
    line-height: 3.5; /* Sets the space between lines */
}

HTML Code:

<!DOCTYPE html> <!-- Defines document type and HTML version -->
<html lang="en"> <!-- Sets the language of the page -->

<head>
    <meta charset="UTF-8"> <!-- Sets character encoding for the document -->
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Ensures proper rendering on mobile devices -->
    <title>Token Minter dApp</title> <!-- Title of the webpage -->
    <link rel="stylesheet" href="/globalstyles.css"> <!-- Link to external CSS file -->
</head>

<body>
    <header>
        <!-- Site header with navigation -->
        <h1>Welcome to our Token Minter dApp</h1>
        <nav>
            <!-- Navigation links -->
            <ul>
                <li><a href="#home">Home</a></li>
                <li><a href="#mint">Mint Tokens</a></li>
                <li><a href="#about">About</a></li>
            </ul>
        </nav>
    </header>
    
    <main>
        <!-- Main content section -->
        <article id="home">
            <!-- Introduction article -->
            <h2>Introduction to Token Minting</h2>
            <p>This dApp allows users to mint their own tokens on the blockchain.</p>
        </article>
        
        <section id="mint">
            <!-- Token minting form section -->
            <h2>Mint Your Tokens</h2>
            <form action="#">
                <!-- Form for token minting -->
                <input type="email" placeholder="Enter your email">
                <input type="number" placeholder="Token amount">
                <button type="submit">Mint</button>
            </form>
        </section>
        
        <aside>
            <!-- Sidebar with additional information -->
            <h3>Learn More:</h3>
            <p>Explore more about blockchain technology.</p>
        </aside>
    </main>
    
    <footer>
        <!-- Footer with contact information -->
        <p>Contact us at <a href="mailto:hello@dProgrammingUniversity.com">hello@dProgrammingUniversity.com</a></p>
    </footer>
    
</body>
</html>

Enhance the Capstone Project with New Features from this Lesson:

CSS Code:

/* External CSS File: styles.css */

/* Flex container for the header to space out elements */
header {
    display: flex;
    justify-content: space-between; /* Distributes space between items */
    align-items: center; /* Aligns items vertically */
    padding: 0 10px;
}

/* Flex item styling for responsive nav items */
nav ul li {
    padding: 10px;
    flex: 1; /* Allows each item to grow */
}

/* Flex container for main content area */
main {
    display: flex;
    flex-direction: column; /* Stacks items vertically */
    gap: 10px; /* Creates space between flex items */
}

/* Flex item adjustments for articles within main */
article {
    flex: 1; /* Allows the article to grow and fill the space */
}

/* Flex container for the form inside mint section */
#mint form {
    display: flex;
    flex-direction: column; /* Stacks form items vertically */
    gap: 10px; /* Creates space between form items */
}

/* Styling for input and button elements */
#mint input,
#mint button {
    padding: 10px;
    margin-bottom: 10px;
    border: 1px solid #ccc;
    flex: 1; /* Allows inputs and button to grow */
}

AFTER:

Your code may look something like the following after applying the above adjustment from this lesson:

CSS Code:

/* External CSS */

/* IMPORTS */

/* Importing a custom Google font */
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');



/* BODY */

/* Basic styling for the body */
body {
    font-family: 'Roboto', sans-serif; /* Applying the imported font family to the body */
    background-color: #f4f4f4;
    background: linear-gradient(to right, #630404, #e040fb, #6a027c); /* Darker brown to lighter purple gradient background */
}


/* NAVIGATION */

/* Styling navigation menu using child combinator */
nav > ul > li {
    display: inline;
    margin-right: 20px;
    text-align: center; /* Aligns text to the center */
    /* Flex item styling for responsive nav items */
    padding: 10px;
    flex: 1; /* Allows each item to grow */
}

/* Styling the navigation menu with a hover effect */
nav a:hover {
    background-color: #630404; /* Light background on hover */
    color: #f3e40d; /* Dark text on hover */
}



/* HYPERLINKS */

/* Styling links using attribute selector */
a[href^="#"] {
    text-decoration: none;
    color: white;
}



/* HEADINGS */

/* Styling for the header using element selector */
header {
    background: #333; 
    color: white; 
    padding: 10px;
    text-align: center;
    /* Flex container for the header to space out elements */
    display: flex;
    justify-content: space-between; /* Distributes space between items */
    align-items: center; /* Aligns items vertically */
    padding: 0 10px;
}

/* Styling headings for emphasis */
h1, h2, h3 {
    font-weight: 700; /* Makes the font bold */
}

/* Styling paragraph using adjacent sibling combinator */
h2 + p {
    color: #cfbdbd;
}      



/* MAIN */

/* Styling for the main content area */
/* main {
    margin: 20px;
    border: 2px solid #333;
    padding: 10px;
    display: grid;
    grid-template-columns: 2fr 3fr; One column for the aside, three for the main content 
    gap: 20px; Space between grid items 
} */

/* Flex container for main content area */
main {
    display: flex;
    flex-direction: row; /* Stacks items vertically */
    gap: 10px; /* Creates space between flex items */
}

/* ARTICLE */

/* Styling for the article */
article {
    margin-bottom: 15px;
    padding: 10px;
    border: 1px solid grey;
    /* Flex item adjustments for articles within main */
    flex: 1; /* Allows the article to grow and fill the space */
}
/* Applying Box Model to the paragraph */
article p {
    margin-bottom: 20px;
    border: 1px solid grey;
    padding: 15px;
}



/* IDs */

/* Targeting specific section with ID selector */
#mint {
    background-color: #eaeaea;
    padding: 15px;
    margin-top: 10px;
}

/* Styling for the form inside the mint section */
#mint form {
    padding: 10px;
    border: 1px solid #ddd;
    margin-top: 10px;
}

/* Flex container for the form inside mint section */
#mint form {
    display: flex;
    flex-direction: column; /* Stacks form items vertically */
    gap: 10px; /* Creates space between form items */
}

/* Styling for input and button elements */
#mint input,
#mint button {
    padding: 10px;
    margin-bottom: 10px;
    border: 1px solid #ccc;
    flex: 1; /* Allows inputs and button to grow */
}



/* PARAGRAPH */

/* Adjusting paragraph line spacing for readability */
p {
    line-height: 3.5; /* Sets the space between lines */
}



HTML Code:

<!DOCTYPE html> <!-- Defines document type and HTML version -->
<html lang="en"> <!-- Sets the language of the page -->

<head>
    <meta charset="UTF-8"> <!-- Sets character encoding for the document -->
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Ensures proper rendering on mobile devices -->
    <title>Token Minter dApp</title> <!-- Title of the webpage -->
    <link rel="stylesheet" href="/globalstyles.css"> <!-- Link to external CSS file -->
</head>

<body>
    <header>
        <!-- Site header with navigation -->
        <h1>Token Minter dApp</h1>
        <nav>
            <!-- Navigation links -->
            <ul>
                <li><a href="#home">Home</a></li>
                <li><a href="#mint">Mint Tokens</a></li>
                <li><a href="#about">About</a></li>
            </ul>
        </nav>
    </header>
    
    <main>
        <!-- Main content section -->
        <article id="home">
            <!-- Introduction article -->
            <h2>Introduction to Token Minting</h2>
            <p>This dApp allows users to mint their own tokens on the blockchain.</p>
        </article>
        
        <section id="mint">
            <!-- Token minting form section -->
            <h2>Mint Your Tokens</h2>
            <form action="#">
                <!-- Form for token minting -->
                <input type="email" placeholder="Enter your email">
                <input type="number" placeholder="Token amount">
                <button type="submit">Mint</button>
            </form>
        </section>
        
        <aside>
            <!-- Sidebar with additional information -->
            <h3>Learn More:</h3>
            <p>Explore more about blockchain technology.</p>
        </aside>
    </main>
    
    <footer>
        <!-- Footer with contact information -->
        <p>Contact us at <a href="mailto:hello@dProgrammingUniversity.com">hello@dProgrammingUniversity.com</a></p>
    </footer>
    
</body>
</html>

Conclusion:

You have now gained the knowledge to implement Flexbox in your projects, an indispensable skill in modern web development.

The responsive and flexible layouts you can create will significantly improve the user experience of the dApps you develop.

Resources & References:

To deepen your understanding of this lesson, you can explore the following resources:

  1. MDN Web Docs – CSS Flexbox reference
  2. W3Schools – CSS Flexbox Tutorial
  3. CSS Tricks – A Complete Guide to Flexbox

These resources provide comprehensive guides and examples that will be invaluable as you learn and apply them in your journey of Web3 frontend development.


Support:

If you need help with this lesson, questions, suggestions and improvement. The best way to get help is to use the comment below:
1. First check existing comments if your questions have been answered before posting.
2. If no existing related question with an answer, then you can post a new one (kindly avoid duplicating previously asked and answered questions).

NOTE: This is the most recommended way of getting support directly from the instructor of this course. Posting on other platforms like Discord may get support from other community members and not from the course instructor directly.

Images|Videos|Links: To support your question with a screenshot, Github repo link or other media, kindly upload it somewhere online like Drive/Dropbox or Github (for codes) first, then post the link to it in your comment. Please do not upload media/codes directly, it may fail or cause your comment to be deleted.

Please ONLY post questions that are specific to this lesson not the entire course to ensure others can find it helpful. Each lesson allows comments relating to it ONLY. Off-lesson questions will be deleted even if they relate to this course as a whole but as long as they are not specific to this lesson, they are not allowed.

2 Comments
Collapse Comments

The questions in this CSS Flexbox for Responsive Design are TypeScript’s questions. There is a mix-up. Kindly check it @Solomon Foskaay.

Solomon Foskaay (Administrator) February 16, 2024 at 9:34 am

I have removed it. Thanks for calling my attention to it.

Leave a Comment

By using this website you agree to accept our Privacy Policy and Terms & Conditions