Creating a flexible grid layout with DIVs that share equal width dimensions

Struggling with my HTML and CSS code, I need a way to structure nested DIV blocks into a responsive grid where each block has the same width. Despite my efforts, nothing seems to work as expected. Currently, the nested DIVs are stacked vertically one on top of the other. My goal is to make these blocks wrap when the browser window is resized, ensuring equal width for each DIV. While my code may not be visually appealing, it served its purpose in creating the current layout.

I attempted using flex and flex-wrap properties to form a grid but encountered display issues with the nested DIV blocks.

<!DOCTYPE html>
<html>
<head>
<style>
.border_box_main {
    width: 100%;
    height: auto;
    border: 15px solid Green;
    box-sizing: border-box;
    background-color: Aquamarine;
    margin: auto;
    text-align: center;
}

.list_1 {
    width: 30%;
    margin-right: 10px;
    margin-left: 10px;
    margin-top: 10px;
    margin-bottom: 10px;
    padding: 15px;
    background-color: Grey;
    display: inline-block;
}

.list_subtext {
    text-align: left;
    margin-left: 10px;
    margin-top: 10px;
    margin-bottom: 10px;
    padding: 15px;
    font-size: 10px;
}

hr {
    color: solid black;
    width: 75%;
    text-align: center;
}

.list_2 {
    width: 85%;
    margin-right: 10px;
    margin-left: 10px;
    margin-top: 10px;
    margin-bottom: 10px;
    padding: 15px;
    background-color: White;
    display: inline-block;
}

.div_list_1 {
    display: list-item;
    list-style-type: disc;
    list-style-position: outside;
    text-align: left;
}
</style>
<title>Test</title>
</head>
<body>
<div class="border_box_main">

<hr><br>

<div style="text-align: center;">
    <div class="list_1">
        <div style="text-align: center;">
            <div class="list_2">
                <div class="div_list_1">
                    Test 1 2 3 4.
                    <div class="list_subtext">(Test2)</div>
                </div>
            </div>
        </div>
    </div>
</div>

<div style="text-align: center;">
    <div class="list_1">
        <div style="text-align: center;">
            <div class="list_2">
                <div class="div_list_1">
                Test 1 2 3 4.
                <div class="list_subtext">(Test2)</div>
                </div>
            </div>
        </div>
    </div>
</div>

<hr><br>

</div>
</body>
</html>

Currently, my nested DIV blocks appear in a vertical list with consistent width. However, I want them to flow horizontally while maintaining equal width and centered position on the screen. Additionally, I need them to adapt responsively to changes in the browser window size.

UPDATE: View my JSFiddle link here https://jsfiddle.net/g42nxk0p/. Notice that under small window sizes, my HTML doesn't render correctly—the borders fail to shrink accordingly. I aim to address this issue as well.

Answer №2

Finally, I managed to make it work on desktop devices! Special thanks to Khoi Le for the grid layout suggestions. It took some tweaking of both his code and mine, but I eventually got everything up and running smoothly. Now, all that's left is to implement the mobile-friendly code without breaking the layout. The only downside was having to add a static pixel width to the DIVs as a temporary fix. Here's an example of the code that's currently working:

<!DOCTYPE html>
<html>
<head>
<style>
.border_box_main {
    width: 100%;
    height: auto;
    border: 15px solid Green;
    box-sizing: border-box;
    background-color: Aquamarine;
    margin: auto;
    text-align: center;
    overflow: hidden;
    word-wrap: break-word;
}

.list_1 {
    width: 320px;
    padding: 15px;
    display: inline-block;
    overflow: hidden;
    word-wrap: break-word;
}

.list_subtext {
    text-align: left;
    margin-left: 10px;
    margin-top: 10px;
    margin-bottom: 10px;
    padding: 15px;
    font-size: 10px;
    overflow: hidden;
    word-wrap: break-word;
}

hr {
    color: solid black;
    width: 75%;
    text-align: center;
}

.list_2 {
    margin: 5px;
    padding: 15px;
    background-color: White;
    display: inline-block;
    border: 20px solid grey;
    width: 200px;
    overflow: hidden;
    word-wrap: break-word;
}

.div_list_1 {
    display: list-item;
    list-style-type: disc;
    list-style-position: outside;
    text-align: left;
    overflow: hidden;
    word-wrap: break-word;
}

.container {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;

    width: 100%;
    justify-content: center;
    overflow: hidden;
    word-wrap: break-word;
}

.item {
    width: 350px;
    margin: 20px;
    padding: 5px;
    min-height: 100px;    
    text-align: center;
    overflow: auto;
    word-wrap: normal;
}
</style>
<title>Test...</title>
</head>
<body>
<div class="border_box_main">
<br><br>
<hr>
<br>

<div class="container">
...

The complete JSFiddle can be found here : https://jsfiddle.net/jdbtwo/ch3tfu2q/

All the top sections of the DIVs are now nicely aligned, and the responsive grid adjusts well with different browser window sizes.

jdb2

UPDATE: In order to make my code functional on mobile devices, I had to incorporate "min-width: x;" for every "width: x;". Unfortunately, in the browsers on my iPhone, the listing appears shifted to the left. I'm still investigating the cause of this issue.

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

Resetting the width of a CSS-styled div to its default maximum width

I have a set of automatically generated div containers that contain label and data information. For example: <div class="display-label"> @Html.DisplayNameFor(model => model.BusinessPhone) </div> <div class="display-field"> @H ...

Only one ID is recognized by jQuery

I have a group of three checkboxes set up like this: <input id="image_tagging" class="1" type="checkbox" value="1" checked="checked" name="data[image_tagging]"> Now, I've created an AJAX function (which is functioning correctly), but it seems ...

Does anyone know of a CSS/JavaScript framework that can be used to replicate the look and functionality of the iOS home

I'm wondering if there is a CSS/JavaScript framework that replicates the layout of an iOS home screen. I want to create a kiosk website with a grid layout similar to Android/iOS where apps can be displayed as icons. ...

The onblur event is triggering prior to the value being updated

There are two input fields within a <form> element. I am trying to retrieve the value of one input field (dpFin) once it has been changed. The issue is that when I attempt to get the new value inside the event using var endDt = document.getElementByI ...

How can I make one background image extend beyond the grid when there are two background images?

I am currently working on a website design using the 960 grid system and a responsive framework called 'skeleton'. Everything is running smoothly, but I have encountered a problem with the header banner. Since it is only 960px wide, there are whi ...

Create dynamic animations using AngularJS to transition between different states within an ng-repeat loop

Here's a simplified explanation of my current dilemma: I have an array containing a list of items that are being displayed in an Angular view using ng-repeat, like... <li ng-repeat="item in items"> <div class="bar" ng-style="{'width ...

Sometimes, in extremely rare instances, external stylesheets may fail to download or be properly applied

There have been very rare occurrences where major websites like Amazon and Facebook do not load a CSS file or apply the rules correctly, resulting in pages looking incomplete: I was recently asked to provide an internal explanation after receiving a compl ...

Tips for designing the microphone appearance on the Google Chrome speech input interface

Google Chrome features an input control for speech recognition. If you want to know how to use it, check out this link. I'm looking to enlarge the microphone icon and possibly use a customized image for it. Increasing the width and height of the inp ...

Is there a way to transform BASE64 encoded HTML into a GIF format using ColdFusion?

I am getting a BASE64 encoded message from a WebService. This message is an interpretation of an HTML page and I can use pre-existing ColdFusion functions to convert and showcase it. However, my requirement now is to generate a GIF image of the HTML conten ...

Utilize TypeScript File array within the image tag in HTML with Angular 2

I am in the process of developing a web application that allows users to upload CSV data and images, which are then displayed on the application. However, I have encountered an issue where I am unable to display the imported images. The images are imported ...

The scrollbar located within a table cell is malfunctioning and unresponsive

In a container with a fixed width of 500px, I have a table that contains a cell with potentially long text. To ensure the long text appears on a single line, I set the white-space: nowrap property. However, this causes the table to adjust its size to accom ...

Is it possible to prevent the text from appearing in the text box when a button is

I have successfully implemented a feature where clicking on the button (click on me) in HTML displays a textbox along with another button (show) on the screen. The text written in the textbox is visible when the show button is clicked. However, after the i ...

Content Security Policy directive violation: Chrome extension policy error occured due to refusal to execute inline event handler

I've been working on a chrome extension to perform a simple task, but I've hit a roadblock with one line of HTML code that's causing issues with setting the correct permissions. Despite my efforts, I'm stuck on what exactly needs to be ...

Adjust the class of the iframe element when clicked

I am attempting to change the class of an iframe (soundcloud embedded track) when it is clicked, in order to apply different properties. However, my code doesn't seem to be working as expected. Here is what I have: Here is a snippet from my index.htm ...

What causes RangeError: Maximum call stack size exceeded when Element UI event handlers are triggered?

I'm currently working on setting up a form and validating it with Element UI. Despite closely following the documentation, I am encountering an issue where clicking or typing into the input boxes triggers a RangeError: Maximum call stack size exceeded ...

Tips for ensuring an image fits perfectly within a MUI grid

I am currently working on an application that involves a collection of cards. My tech stack includes React and MUI v5. One issue I've been facing is with the grid layout, specifically in trying to keep the image size consistent within the grid item. ...

Tips on enlarging a webpage without showing the scroll bar on the main component

My goal is to create a webpage that resembles a window application. However, I've encountered an issue where zooming in on the page results in a scroll bar appearing on the main page. The parameters for the main page (the parent) are set as follows: w ...

What can I do to adjust the Navigation Item and Dropdown placement?

I've been working with Bootstrap dropdowns, but they're not aligning correctly for some reason. I've tried multiple solutions like updating my Bootstrap versions, but none of them seem to be working. Here is the code: <script src=" ...

Validating Data in Laravel with a Single Rule

I just started working with Laravel and our system has multiple forms on one page to enter a single bitcoin wallet in each input field. Currently, users can enter the same wallet information in all inputs, but this is not correct. Is there a way to only al ...

Step-by-step guide for dynamically including dropdown options

Is there a way to dynamically add a dropdown using JavaScript? I currently have a dropdown with numbers that creates another dropdown when selected. However, I want to add the dynamic dropdown through JavaScript. How can I achieve this? Below is the PHP ...