Designing a dynamic 2x2 layout to maximize screen space

I can't seem to figure out how to tackle a seemingly easy problem. I need to create a 2x2 box layout that fills the whole screen and then switches to a single column at a certain width.

The tricky part for me isn't making it break down, but rather creating a fluid 2x2 grid.

I tried using this CSS:

.outer-div{
  display: table;
  width: 100%;
  height: 100%;
}
.inner-div{
  display: table-row;
  width: 100%;
  height: 100%;
}
.inner-block{
  display: table-cell;
  width: 50%;
  height: 100%;
}

It kind of works, but doesn't feel as fluid as I'd like. Is there a better way to do this? Any suggestions are welcome!

(see attached image for visual aid)

https://i.sstatic.net/nH4ML.jpg

EDIT I realized my solution was actually working fine. The issue is that I have another div with absolute positioning and a fixed height that I'm not sure how to accommodate in the layout. Check out this example on Fiddle: https://jsfiddle.net/q2j940r1/1/

Answer №1

To ensure proper display, make sure to set the height of all container elements to height:100%, including the html and body tags. Additionally, allocate 50% width and height for each individual item.

Check out this JsFiddle Example

html, body {
    height: 100%;
}
body {
    margin: 0;
}
.container {
    font-size: 0; /*fix inline gaps*/
    height: 100%;
}
.item {
    font-size: 16px; /*reset font size*/
    display: inline-block;
    vertical-align: top;
    width: 50%;
    height: 50%;
    position: relative;
    overflow: auto; /*for scrollbar*/
}
.content {
    position: absolute;
    left: 0; right: 0; top: 0; bottom: 0; /*stretch and fill*/
}
.item:nth-child(1) { background: aqua; }
.item:nth-child(2) { background: lime; }
.item:nth-child(3) { background: gold; }
.item:nth-child(4) { background: teal; }
<div class="container">
    <div class="item">
        <div class="content">A - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
    </div>
    <div class="item">B</div>
    <div class="item">C</div>
    <div class="item">D</div>
</div>

Answer №2

When it comes to creating a 2x2 table with multiple div elements, the implementation is key.

For a seamless solution in any position and environment, consider using a TABLE as the container in a 2x2 layout with:

  • absolute positioning
  • 100% width/height
  • no margin or border

Simplifying your instructions to the browser leads to smoother rendering - that's the advice I would offer for this scenario.

Answer №3

Simple Grid Layout Example:

If you're looking for a demonstration, feel free to check out the following link: http://jsfiddle.net/9pzvf6nb/

Below is the code snippet for creating a basic 2x2 grid layout:

.col-1 {
    background: blue;
}

.col-2 {
    background: gray;
}

.col-3 {
    background: red;
}

.col-4 {
    background: green;
}

html, body, .container {
    height: 100%;
}

.container {
    position: relative;
    height: 100%;
    width: 100%;
}
.container div {
    height: 50%;
    width: 50%;
    float: left;
    position: relative;
}
<div class="container">
    <div class="col-1">
    </div>
    <div class="col-2">
    </div>
    <div class="col-3">
    </div>
    <div class="col-4">
    </div>
</div>

To create more complex grid layouts, consider using frameworks like Bootstrap or Foundation.

Bootstrap: http://getbootstrap.com

Foundation CSS:

These libraries offer pre-built 12 grid systems that make designing intricate layouts much easier and faster.

Answer №4

If you're looking for a simple solution, one approach is to utilize a combination of Javascript and jQuery to adjust the heights of the div elements dynamically.

Feel free to test out the code snippet below or visit https://jsfiddle.net/rce4csfn/ to see how the resizing adapts based on the window dimensions.

function setHeight(){
    var windowHeight = $( window ).height();
    $('.box').height(windowHeight / 2);
}

setHeight();

$( window ).resize(function(){
    setHeight();
});
*{margin:0px;}

.box{
    width:50%;
    height:100px;
    background-color:blue;
    box-sizing:border-box;
    float:left;
}

#one{background:#82FFFB;}
#two{background:#FFE382;}
#three{background:#E1FF83;}
#four{background:#FEBD87;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box" id="one"></div>
<div class="box" id="two"></div>
<div class="box" id="three"></div>
<div class="box" id="four"></div>

Answer №5

One way to break the blocks in a single line at certain resolutions is by utilizing media queries.
Feel free to check out this fiddle for an example:
jsfiddle.net/s1gfLabf

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

Is there a way to verify if a particular element on a webpage has been altered?

Currently, I'm in a bit of a pickle trying to secure a spot in a Zoom class. If I fail to secure a spot, I'll owe a driving school $300. It's a long story, but not really crucial. My main goal is to receive a notification when the updated da ...

Refresh the content of a random div every few seconds

I have a div that displays one of 10 files at random on each pageload. I want this to refresh every 8 seconds, showing a different file from the 10 each time. I've looked at similar questions using jQuery's .load method as a solution, but it doe ...

Transforming a string into the date input type layout

I am working with an HTML input type of "datetime-local" that I save in a Datetime field within a MS SQL Server database. Upon loading the edit page, I aim to populate this HTML date field with the value retrieved from the database. Upon examining the va ...

Preserving distance between absolute elements

I'm facing an issue with my menu animation using jQuery. In order to achieve the desired effect, I am forced to use position: absolute. My challenge is maintaining consistent spacing between each text string, especially since they vary in width. Unfor ...

Contrasting the addition of an onClick listener through Jquery versus utilizing an HTML onclick attribute

I found this interesting piece of code that I want to share with you all: <!DOCTYPE html> <html> <head> <script type="text/javascript" src="jquery-1.12.4.js"></script> <title> HelloWorld JQuery </t ...

"Discovering a button press using the Gamepad API: A Step-by-Step Guide

I'm currently building a web page that can detect button presses on an Xbox controller and display a boolean value based on the pressed button. Right now, I have successfully managed to detect when a controller is connected and show it as a string. Ho ...

Inserting a PHP variable ($username) into a form field

<form method="POST"> <h1>Contact Us</h1> <span><h2 class="required">*Required</h2></span> <div class="input-group"> <label class="sr-only">First Name</label> <input class="text" ...

How can I locate a div element using multiple class names?

<div class="value test" /> I am trying to locate this specific element on the web page which has two classes assigned to it. Since className does not accept space-separated values, I am exploring alternative ways to achieve this. Any suggestions? ...

Themes in Next.js with a splash of color

Is there a way to implement theming with color picking for elements? Check out the example here. I've explored using CSS variables and changing the document classname, but I'm uncertain if this is the best approach. @tailwind base; @tailwind com ...

Dynamic styles object for React components with inline styles

I have a styles object let styles = { step_div:{ height:'150px', } } I'm trying to display multiple div elements with different colors using React class Layout extends React.Component{ constructor(props) { super(props); ...

Ways to mandate adjusting an option in <select> tag

Using the code $('select').change(function(){alert('changed');});, I am able to trigger an alert every time the option is changed. However, I also need to trigger the alert when the default option, which is the first one in the list, is ...

Tips for extracting JSON data from multiple websites using Python

Having recently delved into Python, I am currently engaged in a project that involves extracting data from numerous websites containing JSON information. While I have successfully scraped data from one website, I am uncertain about how to scrape multiple s ...

Having Multiple File Inputs [type=file] in one webpage

Is it possible to have two separate inputs for uploading images, each setting a different background image in a div? The second file input is: <input type='file' id='getval' name="logo" /> I want this file to be set as the back ...

JavaScript undefined error occurred during runtime

Greetings! I come from a background in VB6 and VB.Net, and I am currently delving into Bootstrap/MVC with C#. Please pardon my beginner queries as I journey through this new territory. I have searched on SO and watched YouTube tutorials, but I am still fin ...

"Utilizing Bootstrap's Table Features within the Moodle Platform

Hey there! I'm currently in the process of updating a client's Moodle website. Our goal is to create responsive tables on the homepage, but I've encountered some challenges. Due to conflicts with other Moodle libraries, I wasn't able to ...

Having issues with Drupal's lightframe and lightbox functionalities not functioning properly

I recently came across a Drupal 6 project that I have little knowledge about. My goal is to incorporate an Iframe into a lightbox, which seems simple but I'm encountering some obstacles. Despite researching online and attempting to implement the code ...

Exporting canvas as JSON and importing JSON back into canvas

Is there a way to prompt file explorer to open and allow me to select a save location for the JSON file of my canvas when I click the save button? Additionally, how can I load the canvas with the JSON file using the load button? Any guidance on getting s ...

Ways to position a single item in the middle of a multi-column layout

I need help with aligning dynamic images in a 2-column layout. What's the best way to center a single image or collapse the columns when there's only one image? Currently, the single image is always placed in the left column. Is there a CSS-only ...

How can you use jQuery to take the input value and assign it as a class to the label surrounding it?

I need a simple solution: how can I retrieve the input value and apply it as a class to the same input? For example, I want to extract the value "gray" and add class="gray" to the surrounding label in order to style the label text that says "Gray". This i ...

Troubleshooting issue arising from transferring code from CodePen to a text editor

I've attempted to recreate a Codepen example in my HTML files on my computer, but it's not displaying correctly. While the static layout resembles what appears in Codepen, the background with the moving squares isn't functioning as expected ...