When the width is reduced to a certain point, the display will change to inline-block, preserving the layout structure

My goal is to maintain a two-column layout for my container boxes, even when the browser width is minimized to 600px in my fiddle. The issue arises with the CSS rule display: inline-block causing the boxes to stack into a single column.

Important point: I am using the jQuery plugin Mixitup.

Here is an example of the plugin's code (not mine) which you can view here: http://codepen.io/jzhang172/pen/EVGNqj


What I have attempted so far: I tried adjusting the CSS rule using @media all (max-width:600px) but had no luck changing the value of display: "value". I suspect a JavaScript solution might be needed, as my expertise lies more in CSS rather than Javascript.

Jsfiddle link:http://jsfiddle.net/jzhang172/886mbLbq/

.box{
    width:200px;
    height:200px;
    background:black;
    display:inline-block;
    
}

@media all and (max-width:600px)
    {
     #container .box{
      display:block;   
     }
        
    }
<div id="container">
    
    
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    
</div>

Answer №1

One way to keep elements in no less than 2 columns is by setting a min width on the container.

For example, in Sample 1: The extra 4 pixels in the 404px width account for the extra margin of inline elements. More information on this can be found here: https://css-tricks.com/fighting-the-space-between-inline-block-elements/

In Sample 2: Instead of using the inline-block hack, you can use floats. Additionally, the box-sizing: border-box; style ensures that any temporary borders added for visibility stay within the box's specified dimensions.

A third method would be using Flex, although there is no sample provided for that in this instance.

Below is an example of implementing inline-block:

#container {
    min-width: 404px;   
}

.box{
    width:200px;
    height:200px;
    background:black;
    display:inline-block;        
}
<div id="container">
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>    
</div>

Alternatively, utilizing the float property:

#container, .box {
    box-sizing: border-box; 
}

#container {
    min-width: 400px;   
}

.box {
    width:200px;
    height:200px;
    background:black;
    float:left;
    border: 1px solid #ccc;
}
<div id="container">
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>    
</div>

Answer №2

If you're looking to change up your layout, consider using floats instead of inline-block. This approach allows you to target specific elements like the 1st, 3rd, or 5th and start a new "line" with a clear. Take a look at this example CSS snippet:

.container{
width:250px;
height:250px;
background-color:#333;
margin-right: 6px; /*adjust for desired spacing*/
margin-bottom: 6px;
float: left;

}

@media all and (max-width:800px)
{
 .container:nth-child(3n+1) {
  clear: both; 
   background-color:blue; /*temporary color for visualization*/ 
 }

}

https://jsfiddle.net/abc123/

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

Here is a way to retrieve the name of a ref object stored in an array using Vue.js 3 and Typescript

I have a Form, with various fields that I want to get the value of using v-model and assign them to ref objects. In order to populate my FormData object with this data, I require both the name and the value of the ref objects. Unfortunately, I am struggli ...

The functionality of the bootstrap Dropdown multiple select feature is experiencing issues with the Onchange

Creating a Bootstrap Multiple Select Drop Down with dynamically retrieved options from a Database: <select size="3" name="p" id="p" class="dis_tab" multiple> <?php echo "<option>". $row['abc'] ."</option>"; //Fetching option ...

Guide on retrieving a nested JSON array to extract a comprehensive list of values from every parameter within every object

A JSON file with various data points is available: { "success": true, "dataPoints": [{ "count_id": 4, "avg_temperature": 2817, "startTime": "00:00:00", "endTime": "00:19:59.999" }, ... I am trying to extract all the values of & ...

Tips for concealing the "infoFiltered" feature in the jQuery datatable plugin

Within the datatable plugin, there is an option called "infoFiltered" which displays the total number of rows in the datatable across all pages. However, I am looking to hide this information. Is there a way to achieve that? ...

There is a problem with my module where multiple files that require it are overriding its variables

Currently, I am working on developing a mongo connection pool factory that is capable of checking if a connection to mongo already exists. If a connection exists, it will return that connection. However, if there is no existing connection, it will create a ...

Exploring the art of div transitions and animations using React and Tailwind CSS

I successfully implemented the sidebar to appear upon clicking the hamburger icon with a transition. However, I am now facing the challenge of triggering the transition when the close button is clicked instead. I have attempted using conditional operations ...

Encountering redundant links during the scraping process

I'm currently working on extracting "a" tags with the class name "featured" from a specific website . Although the code I've written seems to be error-free, it unfortunately duplicates the links. How can I resolve this issue and avoid the duplica ...

Unable to determine the length of an array in Vue.js due to property being undefined

Within my vue.js data structure, I have the following: data() { return { formData: new Form({ files:[], Count:5, .. } I am attempting to retrieve the length using the following code : <div class="image-input& ...

What method is the most effective for extracting the first line of a file in Node.js quickly?

If you are faced with multiple lengthy text files and your goal is to extract data solely from the first line of each file (without delving into the rest), how would you recommend achieving this efficiently using Node.js? Appreciate any suggestions! ...

How to Revalidate a Next.js Dynamic Route Manually Using an API Route?

In my application built with Next.js, I have a dynamic page that displays resources at the route /resource/[id]. When a user edits a resource, such as #5, I want to refresh the cached page at /resource/5. I've created an API route in my /pages direct ...

Is there a way to refresh a different webpage within the current session?

In the world of Asp.Net 4, there is a form waiting to be filled with numeric data by the customer. However, this task can sometimes prove tricky as customers may struggle to calculate and input the total figure for each of the four fields. An innovative s ...

Bot on Discord engaging in a gaming session

I recently developed a Discord bot with the status of ""playing a game"" and here is the code that I implemented: bot.on('ready', () => { console.log('Client is online!'); bot.user.setActivity('osu!'); Th ...

Is it possible to ensure uniformity in the appearance of all images with MVC Image Handling?

I'm currently developing an MVC 4 application that allows users to upload images. These uploaded images are then displayed in a different view. Users can upload images up to 10MB in size, which are then resized to 800 x ? pixels using the WebImage (Sy ...

Enhance your user interface by customizing the expand icon in the React material

Currently, I am utilizing Material-table with a focus on Tree-data. You can find detailed information about this feature here: https://material-table.com/#/docs/features/tree-data. While attempting to implement the provided example, I am encountering diff ...

ReactJS Components failing to load on initial site visit, only appearing after refreshing for the second time

var img; var dateFormat = require('dateformat'); var count; let arrayIMG = [] var storage = firebase.storage(); var storeRef = storage.ref('images/') const config = { ... }; if (!firebase.apps. ...

Struggles with implementing CSS Grid's dynamic column heights and expanding rows

Linked partially to Removing empty space in CSS Grid, with specific adjustments I am aiming for. Essentially, I want my rows and columns to expand and contract based on the content present. In this CodePen example, you can observe that the content of the ...

Generating a dataframe with cells that have a combination of regular and italicized text using the sjPlot package functions

I've been trying to set up a basic data table where the genus name in the "Coral_taxon" column is italicized, but the "spp." part following it remains lowercase. I thought of using the expression() function for each row in "Coral_taxon," but so far, I ...

Display the date string in Material-UI TableCell格式

I have a TableCell component in Material-UI that displays dates in the format 2019-03-25T19:09:21Z: <TableCell align="left">{item.created_at}</TableCell> I want to change this to a more user-friendly format showing only the date as either 25/ ...

Image failed to load

Issue Encountered in Browser Console: https://static.food2fork.com/pastaallavodkaa870.jpg.jpg 404 While attempting to display the image on the browser, I am uncertain if the problem lies within my code or with food2fork. Code from index.js: // Alway ...

Disabling the background shadow effect in Angular Material's Accordion

I successfully disabled the background shadow on the Angular Material Accordion in this demonstration by incorporating the following CSS rule: .mat-expansion-panel:not([class*='mat-elevation-z']) { box-shadow: none !important; /* box-shadow: ...