Flexbox design that adapts responsively to display 3 items per row, shifting to 2 items and finally

I have a segment with six elements.

For desktop, I would like to display 3 items per row; for medium devices, 2 items per row; and for mobile devices, only 1 item per row.

<div class="flex-container">
  <div>1</div>
  <div>2</div>
  <div>3</div>  
  <div>4</div>
  <div>5</div>
  <div>6</div>
</div>  

Here is the CSS:

.flex-container{
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    width: 100%;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    justify-content: center;
}

@media only screen and (max-width: 768px) and (min-width: 320px){
    .flex-container {
        display: flex;
        justify-content: center;
        flex-basis: 100%;
    }
}

Currently, it displays 3 items on desktop and 1 item on mobile. However, I am missing the 2 items per row on medium devices. What adjustments should I make in my code to achieve this layout?

Answer №1

To create the layout, you can adjust the flex-basis property within media queries.

Here's how it works:

.flex-container {
  display: flex;
  flex-wrap: wrap;
}

.flex-container > div {
  flex: 1 0 26%;
}

@media ( max-width: 768px) {
  .flex-container > div {
    flex-basis: 34%;
  }
}

@media ( max-width: 320px) {
  .flex-container > div {
    flex-basis: 51%;
  }
}

/* extra styles for demonstration */
.flex-container > div {
  height: 50px;
  background-color: lightgreen;
  border: 2px solid white;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 1.2em;
}
<div class="flex-container">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
  <div>6</div>
</div>

Check out the jsFiddle demo here

The first flex rule sets flex-grow to 1, allowing flex items to take up all available space.

With this setup, the width of each item specified by flex-basis no longer needs to follow conventional percentages like 33.33% or 50%. These values usually require manual adjustments, such as using calc(), to account for margins.

In this scenario, utilizing flex properties enables flex-grow to fill up any remaining space, requiring flex-basis only to determine when wrapping occurs.

For instance, setting flex-basis: 26% allows three items per row before a wrap is triggered, leaving ample room for margins. The flexibility of flex-grow handles any leftover space efficiently.

Answer №2

You're on the right track with your approach to media queries.. Just be cautious of the 'e' in .flex-conteiner, it should actually be .flex-container

From there, implement media queries and flex-basis for different screen sizes, like so:

@media only screen and (max-width: 768px) and (min-width: 320px){
    .flex-container {
        display: flex;
        justify-content: center;
        flex-basis: 100%;
    }
}

@media only screen and (max-width: 1200px) and (min-width: 768px){
    .flex-container {
        display: flex;
        justify-content: center;
        flex-basis: 50%;
    }
}

For larger screens, consider using a flex basis of 33%.

Answer №3

To adjust the flex-basis within the <div>, you can make changes directly. For a visual example, check out this link: https://example.com/example-link

Answer №4

Yes, the previous responses are accurate. I recommend checking out my answer on a similar Stack Overflow thread: How to create a fluid child div relative to its parent.

Furthermore, it appears that there is an error in your media query and class selection; I suggest correcting it.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Google Chrome applying its unique style compositions

I recently started transitioning a website from HTML to Wordpress, and encountered an unexpected challenge with Google Chrome applying unfamiliar styles. Specifically, the issue lies with the positioning of the background image bg.gif. While Internet Explo ...

What is the best way to eliminate empty spaces from the container?

click here check out this image too Just getting the hang of Bootstrap. The first picture looks good, with the image as the background. But on the second picture, there are margins and a background color showing up. How can I get rid of those margins? Her ...

Creating a personalized login function in LaravelWould you like to learn how to

Currently, I am in the process of creating an application that caters to two distinct user groups: sellers and buyers. Each group has their own unique set of pages and forms for registration. I have successfully implemented the registration logic for both ...

Mastering the art of utilizing the LESS preprocessor with React Create-React-APP

Is it possible to implement the LESS preprocessor in my create-react-app project? In my React code, I have the following: ReactDOM.render( <form> <input type="email" data-info="Enter Email" pattern="/\S+@\S+\.\S+/ ...

What is the best way to ensure a stable background image using CSS?

I have successfully created my navigation section and now I am working on the main body of the website. However, when I try to add a background image, it ends up appearing on the navbar as well. Can someone please assist me with this issue? Below is the HT ...

What is the typical output value that fluctuates?

I only have one input to work with. My goal is to set the input value to "5", then display "3" in the "total" field and "4" in the "total2" field. Additionally, for every increment of +1 to the input value, I want the "total" and "total2" fields to also in ...

Using Kendo UI Mobile, take advantage of a button to trigger the click action of another

Consider the following situation: HTML <a id="mButton" data-role="button" data-click="clickFn">myButton</a> <asp:ImageButton runat="server" ID="aspButton" style="display: none"></asp:ImageButton> SCRIPT function clickFn(e) { ...

What are the acceptable ID attribute values to use with JQuery selectors?

Can you tell me what the acceptable values are for the ID attribute in HTML elements when utilizing JQuery selectors? Is JQuery conforming to HTML4 or HTML5 rules regarding the ID attribute? ...

Incorporate JavaScript code into an Angular UI-Router view

I can't seem to find an answer to this question anywhere, so I'm hoping someone here can help me out. In my AngularJS app, I am using ui-router to load multiple views into the same <ui-view> element. I am trying to implement Jquery UI&apos ...

The jQuery ajax request will only display the data in the HTML once

Hey there! I am facing an issue where, when I click on my button to make an ajax request, it works perfectly and displays the data on my select item. However, on clicking the button again, the data is fetched correctly but the select item shows the data t ...

Make the table width 100%, but we don't want the central cell to be stretched

My table contains a central image cell that spans two rows: <table width="100%" style="text-align:center"> <tr> <td>Cell 1</td> <td>Cell 2</td> <td rowspan="2"><img src="http://www.skrenta.com/ima ...

What is the method to alter the color of an SVG source within an image tag?

I am currently working on a component that involves changing the color of an SVG icon from black to white when a color prop is given. export class CategoryIconComponent extends React.Component { static displayName = 'CategoryIcon'; state = ...

Functionality of JavaScript limited within a form

Here is some HTML code I am working with: <div class = "login"> <form> <input type="text" id="username" name="username" placeholder="Username" style="text-align:center"> <br> <br> <input type="pa ...

Displaying the Polymer Paper spinner when the page is still loading

How can I make the polymer paper spinner display while the body tag is set to unresolved? I'm experiencing a brief blank screen before the page finishes loading. I'm feeling lost and unsure of how to begin. Check out the Polymer Project documen ...

Combining a Vuetify 2 toolbar and a navigation drawer, topped with an additional toolbar for added functionality

I am working towards achieving a layout similar to this: https://i.stack.imgur.com/M0rGl.jpg However, I currently have this setup: https://i.stack.imgur.com/VOGxJ.jpg The issue I am facing is that I am unable to position the first toolbar above the navi ...

Challenges with KendoUI integration

In one of my web pages, I have a combination box and a checkbox. To enhance the functionality, I decided to utilize the KendoUI library. The combo box allows users to choose between two options - 0% and 100%. If the checkbox is selected, the combo box shou ...

Implementing jQuery to enable the selection of multiple options depending on user input

Assume that when the people input value is 1, then the options for values 1 and 2 will be selectable. However, if the people value is greater than 1, then all other options will become selectable. Is it feasible to enable multiple select options based on ...

Troubleshooting CSS Animation Failure in Internet Explorer 11

My mouse over / mouse out animation works perfectly in Firefox and Chrome, but it's not functioning in IE. Can anyone suggest why this might be happening when it was working fine before? var actual = 1; var over = 0; var over2 = 0; function scrol ...

Tips for creating the ultimate footer section on your webpage

I'm trying to position the footer div (id="Footer") 10px from the bottom of the screen. Regardless of whether the page content fills the entire height of the screen or creates a scroll area, I want the div to always be at the very bottom with a 10px m ...

Is it true that outerHeight(true) excludes margins?

As stated in the JQuery documentation, the outerHeight(true) function should return the complete height of an element, including padding, border, and margins. However, I encountered a scenario where this functionality seemed to be flawed. You can find a co ...