CodeGym /Java Course /Frontend SELF EN /Flexbox and Responsiveness

Flexbox and Responsiveness

Frontend SELF EN
Level 26 , Lesson 4
Available

10.1 Responsive Columns

Flexbox is meant for one-dimensional compositions, meaning it's perfect for arranging elements in a row or column. Let’s go over the basics of Flexbox and techniques for creating complex and responsive layouts.

Flex Container and Flex Items

  • Flex Container: the element to which the display: flex property is applied. This container manages the layout of all child elements (flex items).
  • Flex Items: the child elements of the flex container.

1. Creating Responsive Columns

Example of creating responsive columns using flex-wrap, flex-grow, flex-shrink, and flex-basis:

CSS
    
      .flex-container {
        display: flex;
        flex-wrap: wrap;
        gap: 10px;
      }

      .flex-item {
        flex: 1 1 200px;
        background-color: #3498db;
        color: white;
        padding: 20px;
        text-align: center;
      }
    
  
HTML
    
      <!DOCTYPE html>
      <html lang="en">
        <head>
          <meta charset="UTF-8">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <title>Responsive Columns with Flexbox</title>
          <link rel="stylesheet" href="styles.css">
        </head>
        <body>
          <div class="flex-container">
            <div class="flex-item">Column 1</div>
            <div class="flex-item">Column 2</div>
            <div class="flex-item">Column 3</div>
          </div>
        </body>
      </html>
    
  

Code Explanation:

  • .flex-container: defines the container as a flex container with items that can wrap to the next line if there's not enough space (flex-wrap: wrap).
  • .flex-item: items are equal in size and can adjust to the available space (flex: 1 1 200px).

10.2 Responsive Image Gallery

Let's create a responsive image gallery that changes the number of columns based on screen size:

HTML
    
      <!DOCTYPE html>
      <html lang="en">
        <head>
          <meta charset="UTF-8">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <title>Responsive Flexbox Gallery</title>
          <style>
            .gallery {
              display: flex;
              flex-wrap: wrap;
              gap: 10px;
            }

            .gallery img {
              flex: 1 1 calc(33.333% - 10px);
              max-width: calc(33.333% - 10px);
              height: auto;
            }

            @media (max-width: 768px) {
              .gallery img {
                flex: 1 1 calc(50% - 10px);
                max-width: calc(50% - 10px);
              }
            }

            @media (max-width: 480px) {
              .gallery img {
                flex: 1 1 100%;
                max-width: 100%;
              }
            }
          </style>
        </head>
        <body>
          <div class="gallery">
            <img data-max-width="256" data-id="ebb06cf3-0e04-45bd-a33e-31fe096fd323" src="https://cdn.javarush.com/images/article/ebb06cf3-0e04-45bd-a33e-31fe096fd323/256.jpeg" alt="image 1">
            <img data-max-width="256" data-id="ddbb4085-7a58-44e0-88fe-a2368b777222" src="https://cdn.javarush.com/images/article/ddbb4085-7a58-44e0-88fe-a2368b777222/256.jpeg" alt="image 2">
            <img data-max-width="256" data-id="71be962a-10e9-4351-8c27-8846c6ad2e00" src="https://cdn.javarush.com/images/article/71be962a-10e9-4351-8c27-8846c6ad2e00/256.jpeg" alt="image 3">
            <img data-max-width="256" data-id="8afd6be8-0f8e-42e9-a64b-8549f12862f7" src="https://cdn.javarush.com/images/article/8afd6be8-0f8e-42e9-a64b-8549f12862f7/256.jpeg" alt="image 4">
            <img data-max-width="256" data-id="77241dcb-9b7f-471e-934e-9f7f2a42d369" src="https://cdn.javarush.com/images/article/77241dcb-9b7f-471e-934e-9f7f2a42d369/256.jpeg" alt="image 5">
          </div>
        </body>
      </html>
    
  
HTML
    
      <!DOCTYPE html>
      <html lang="en">
        <head>
          <meta charset="UTF-8">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <title>Responsive Flexbox Gallery</title>
          <style>
            .gallery {
              display: flex;
              flex-wrap: wrap;
              gap: 10px;
            }

            .gallery img {
              flex: 1 1 calc(33.333% - 10px);
              max-width: calc(33.333% - 10px);
              height: auto;
            }

            @media (max-width: 768px) {
              .gallery img {
                flex: 1 1 calc(50% - 10px);
                max-width: calc(50% - 10px);
              }
            }

            @media (max-width: 480px) {
              .gallery img {
                flex: 1 1 100%;
                max-width: 100%;
              }
            }
          </style>
        </head>
        <body>
          <div class="gallery">
            <img src="image1.jpg" alt="Image 1">
            <img src="image2.jpg" alt="Image 2">
            <img src="image3.jpg" alt="Image 3">
            <img src="image4.jpg" alt="Image 4">
            <img src="image5.jpg" alt="Image 5">
          </div>
        </body>
      </html>
    
  

Explanation:

  • On large screens, images take up a third of the container's width.
  • On screens up to 768 pixels wide, images take up half of the container's width.
  • On screens up to 480 pixels wide, images take up the full width of the container.

10.3 Complex Responsive Layout with Flexbox

Let's create a complex layout using Flexbox that includes a header, sidebar, main content, and footer that adapts to different screen sizes:

HTML
    
      <!DOCTYPE html>
      <html lang="en">
        <head>
          <meta charset="UTF-8">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <title>Complex Flexbox Layout</title>
          <style>
            body {
              display: flex;
              flex-direction: column;
              min-height: 100vh;
              margin: 0;
            }

            header, footer {
              background-color: #333;
              color: white;
              text-align: center;
              padding: 20px;
            }

            main {
              flex: 1;
              display: flex;
              flex-direction: column;
            }

            .content {
              flex: 1;
              padding: 20px;
              background-color: #f4f4f4;
            }

            aside {
              background-color: #ccc;
              padding: 20px;
            }

            @media (min-width: 768px) {
              main {
                flex-direction: row;
              }

              .content {
                flex: 3;
              }

              aside {
                flex: 1;
              }
            }
          </style>
        </head>
        <body>
          <header>Header</header>
          <main>
            <aside>Sidebar</aside>
            <div class="content">Main Content</div>
          </main>
          <footer>Footer</footer>
        </body>
      </html>
    
  

Explanation:

  • On small screens, the layout consists of a header, main content, sidebar, and footer arranged vertically.
  • On screens 768 pixels wide and up, the main content and sidebar are arranged horizontally.
1
Task
Frontend SELF EN, level 26, lesson 4
Locked
Responsive Columns
Responsive Columns
1
Task
Frontend SELF EN, level 26, lesson 4
Locked
Responsive Gallery
Responsive Gallery
1
Опрос
Responsive Graphics,  26 уровень,  4 лекция
недоступен
Responsive Graphics
Responsive Graphics
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION