Responsive Flexbox Grid Layout

I'm having some trouble with a Flexbox layout and can't seem to find the right solution.

My goal is to have 3 items per row, but not force them to fill up the entire row if there are less than 3 items.

For example:

1 2 3
4 _ _

On smaller screens, it should look like this:

1 2
3 4

However, what I currently get is:

1 _
2 _
3 _
4 _

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

.flex_container .third {
  width: 33.333%;
  padding-right: 10px;
  margin-bottom: 10px;
}

@media(min-width:981px) {
  .flex_container .third:nth-child(3n) {
    padding-right: 0px;
  }
}

@media(max-width:980px) {
  .flex_container .third {
    width: 50%;
    padding: 0px 5px!important;
  }
}

@media(max-width:768px) {
  .flex_container .third {
    width: 100%;
  }
}
<div class="flex_container">
  <div class="third">1</div>
  <div class="third">2</div>
  <div class="third">3</div>
  <div class="third">4</div>
</div>

Can anyone help me figure out what I'm missing?

Answer №1

If you're looking for a layout that prioritizes mobile responsiveness, consider the following code snippet (This approach is known as mobile-first design and offers various advantages which you can explore further through research):

* {
  box-sizing: border-box;
}
.flex_container {
  display: flex;
  flex-wrap: wrap;
}
.flex_container .third {
  display: inherit;
  flex: 0 1 100%;
  padding:0 10px;
  margin-bottom:10px;
  /* Added to show the boxes when no content */
  min-height: 100px;
  border: 1px solid black;
}

@media(min-width: 768px){
  .flex_container .third{
      flex: 0 1 33%;
  }
}

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

When you hover over the page, the content shifts to reveal a hidden div

Trying to figure out how to show additional content when a photo is hovered over without shifting the rest of the page's content placement? Looking for alternative solutions rather than using margin-top: -22%. Here's a link to the website in que ...

Conceal scroll bar within web browser view

Is there a way to remove the scrollbar on Android Chrome/Chromium by utilizing CSS or Javascript? I attempted: document.documentElement.style.overflow = 'hidden'; Although this solution works for Chrome on Windows, it doesn't have any eff ...

Right-align each item when selecting none

Is there a way to change the style of just one of the elements select or option, without affecting the style of both? I attempted to align the select element to the right, while leaving the option elements aligned to the left. However, this did not work a ...

Ensuring proper input with JavaScript form validation

Currently, I am working on implementing a basic form validation feature, but it is not functioning as intended. The desired behavior is for the field border to change color to green or red based on its validity, while displaying text indicating whether t ...

Tips for utilizing CSS container queries in conjunction with the Material-UI framework

I'm looking to implement CSS container queries with MUI. In my code, I want to change the background color to red if the parent width is less than 300px. Check out the sandbox const StyledItem = styled("div")(({ theme }) => ({ border: ...

What is the most efficient method for populating *interconnected data-dependent* <select> dropdowns in a Rails application?

When users need to choose a car from our system, they are faced with multiple dropdown menus to select the year, make, model, and submodel. The challenge lies in the fact that these options are interdependent, making it necessary to retrieve data through s ...

Tips for showing an inline <span> within <div> elements

Is there a way to showcase inline spans within a div element while ensuring they are displayed equally in size and space, based on the user's screen? And how can I ensure that these spans are positioned behind other div elements? body { width: au ...

Switching up the menu choices does not result in a shift in the active class, despite attempting the workaround outlined in the following link

Currently, I am working on a website which can be accessed at the following link: . You can find a helpful example link below. How to change active class while click to another link in bootstrap use jquery? Now, I will showcase the code that I have writt ...

How can I prevent dataTable resizing?

Is there a solution to prevent datatable resizing? For instance, when the default number of rows per page in the datatable is set to 10 rows. If you input 13 rows into the datatable, it will be divided into 2 pages - with 10 rows on the first page and 3 ro ...

Tips for running a JavaScript statement multiple times in the browser console

When I enter the following JavaScript statement in the browser console: inbreedingInvite('74011','107433',''); and press enter, it sends an invitation to user number 107433 to join group id 74011. If I want to invite another ...

When generating dynamic text boxes, a new area is always allocated for each new set of dynamic text boxes that are created

<html> <head> <script src="jquery-1.10.2.min.js"></script> <script> function AddingTextBoxes() { var NumOfText=$("#NumOfTextBoxes").val(); $('#NewlyCreatedSe ...

How can I correctly focus on 'highlight' events using the tab key on an HTML element?

I'm struggling to figure out the event that is triggered when an element gains focus through tab navigation. I want all buttons in the codepen provided to expand to full size when tabbed over. https://codepen.io/anon/pen/YopBaz Currently, I have achi ...

Jquery CSS malfunctioning on Post's div element

After retrieving a div using jquery's post method on my page, I am encountering an issue when attempting to apply CSS to the divs. Strangely enough, applying CSS with jquery to divs that are already present on the page works perfectly fine. Javascrip ...

Display an image when hovering over a list in HTML, utilizing JS and JQuery

When I hover over the text, I want those images to become visible. However, it seems that only the first image is selected because they all have the same ID. Does anyone know how to solve this issue? This is my first post, so please forgive me if I made ...

Issue with dropdown menu display in Internet Explorer

Having trouble with a CSS dropdown menu displaying incorrectly in IE, while working fine on other browsers. <body> <div id="container"> <header> <div id="hlogo"> <a href="index.html">title ...

PHP API is returning the entire object, rather than just the message

I recently developed a REST API using PHP for data insertion. However, when I try to display the message in my AJAX success response, all I get is a series of objects instead. Here is my PHP code snippet: if(mysqli_query($connection , $ins)){ echo jso ...

Enhance the appearance of your printed pages by incorporating borders with the help of wkhtml

Is there a way to print a square border on each page of a multi-page PDF rendered using wkhtmltopdf, similar to the question asked here: Add borders to each printed page with CSS? I generate an HTML page as a variable and utilize snappy from https://githu ...

I'm struggling to adjust the height of a div based on whether a list item within it contains a nested unordered

I have been working on a horizontal menu that is functioning well for me. However, according to the design requirements, I need to adjust the height of <div id="nav-subMenu"></div> if the main/parent menu li does not have any submenus or ul. Th ...

Troubleshooting Problem with Accordion Size in CSS

I am facing an issue with a dropdown menu that I have created. The dropdown has parent and child rows to display controls, but the width of the Accordion is not stretching as expected despite being set to 100%. Using Chrome and Edge developer tools, I insp ...

Creating a blog "post" model in Ruby on Rails with CSS that can behave in two distinct ways

I am in the process of creating a blog using Ruby on Rails. The posts on my blog are generated from the Posts model, and I want them to exhibit two different behaviors. Currently, I have configured it so that a new post is automatically created every day. ...