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

Internet Explorer automatically moves the cursor to the beginning of a textarea when it gains

When trying to insert "- " into an empty textarea, I am facing issues with Internet Explorer. While Firefox and Chrome work perfectly fine by inserting the text as expected, IE causes it to jump to the beginning of the textarea after insertion. Therefore, ...

Why is it that this JavaScript isn't working as intended in the popup form?

</br> s_foot"> * use ajax.jquery as control event. like $("#save").click(function(){.....}); <script type="text/javascript>" var wp; var position; var pid; var product_name; var production_date; In this script, I am attempting to retri ...

Is there a method or API available for interacting with an <object> that is currently displaying a video?

Yay, I figured it out! I've been using video.js to play videos on a website that needs to work offline too. Unfortunately, using flash is not an option due to compatibility issues. So, I decided to write my own fallback solution. For Internet Explor ...

Struggling to align content vertically within an article and in need of some guidance

Still struggling with vertical alignment of content within an article. I've tried using tables and the vertical-align property but can't seem to make it work. Any suggestions for centering the content responsively on the right-hand side? The lef ...

The v-for directive is displaying my list in a single row with multiple columns instead of in a single column with multiple rows

Can someone please assist in rendering my list as shown below: A B C The current rendering looks like this: 1: A 2: B 3: C Below is the code snippet: To-Do List: <input type="text" class = "todo" placeholder = "Next Item" v-on:keyup.enter="add ...

When an HTML element extends beyond the screen's right boundary, it becomes truncated

My website utilizes a table to showcase some data, but there's an issue when viewed on smaller screens. The table gets cut off and not all the content is visible. Even after scrolling completely to the right, the rightmost field remains barely visible ...

Error in Firefox: "Anticipated media feature name, but discovered 'hover'"

I am trying to style elements differently based on whether the client browser supports hovering or not. I came across media features as a solution and implemented it in the following way: /* Supports hovering */ @media (hover: hover) { ... } /* Does not ...

When I adjust the size of my browser, the elements on my HTML page shift around

I'm facing an issue where all my elements move when I resize my browser, and I cannot figure out why. How can I ensure that they stay in their respective positions even when the browser is resized? Is there a CSS solution for this? Thank you! body ...

Angular Fragmentary Perspective

I have a lengthy form in angular that I am currently working on. I'm wondering if there is a way to divide it into multiple views and then link each of them back to the main view. <div class="container"> <div class="row" id="part1"> ...

How can I use AngularJS to show a JSON value in an HTML input without any modifications?

$scope.categories = [ { "advertiser_id": "2", "tier_id": 1, "tier_name": "1", "base_cpm_price": "", "retarget_cpm": "", "gender": "", "location": "", "ageblock1": "", "ageblock2": "", "ageblock3": ...

Group in group, glitch in outdated browser

Facing an issue with IE6 while writing HTML/CSS code. I am looking to create dynamic divs using classes: Here is a simple example (not real-project code): .top {width: 50px;} .top.selected {background: #f00;} .mid {width: 114px;} .mid.selected {backgrou ...

Adjust the size of the text within the div element as needed

Here is my code on jsFiddle where I am dynamically changing the text font to fit in the div. Everything is working perfectly fine. Currently, the text is within a span element, but I would like to remove the span and have the same functionality. However, w ...

Experiencing excessive CPU usage while utilizing a progress bar in Angular

Whenever I try to load a page with 2 progress bars, my CPU usage goes through the roof... I decided to investigate and found that once I removed them, the site's speed improved significantly. Here's a code snippet of one of the progress bars: ...

Create a JavaScript button that redirects to a different page within a React application

I am creating a div element using a for-loop and I want to link each div to the "/campaign" page with its respective id. When a div is clicked, I want it to navigate to the "/campaign/id" page and pass the id to the Campaign component. class Home extends ...

Can the current website navigation be integrated into the blog script?

People might see me as a spoil sport for not using Wordpress, but I prefer a flatfile blog system for my small site. Currently, I am using the ozjournals-3.2 blog system. I need assistance in installing a header and site navigation on top of the blog page ...

Discovering how to adjust the "border-spacing" in a React Material-UI Table to ensure each row is nicely separated

I am looking to create Material-UI Collapsi Table Rows with separate styling. Check out the link for more information: https://material-ui.com/components/tables/#CollapsibleTable.tsx const theme = createMuiTheme({ overrides: { MuiTable: { root ...

Utilizing jQuery to incorporate a radio input function in a POST request

When the Index.php page button is pressed, data is sent to the Resultx.php script page, which then responds with an asynchronous call back on the same Index.php page. index.php <script> $(document).ready(function() { $("#input_form").subm ...

Erase a set of characters with the press of the backspace key (JavaScript)

Within my textarea, I have lines that start with a number and a period: <textarea autoFocus id="text-area"wrap="hard" defaultValue ={this.state.textAreaVal} onKeyUp={this._editTextArea}/> For example: Line 1 Line 2 Line 3 I've created a ...

What is the best way to close a popup window?

Is there a way to close my popup? function hidePopup(){ document.getElementById("popup-1").classList.remove("active"); } @import url('https://fonts.googleapis.com/css2?family=Source+Sans+Pro&display=swap'); .popup .overlay{ positio ...

How can I position one DIV relative to another DIV?

Can a DIV be positioned relative to another DIV? My guess is that it involves nesting it inside the reference DIV and utilizing position: relative. However, I'm struggling with how to achieve this without impacting the content of the reference DIV. An ...