How to create a full-screen 2x2 grid layout using divs in HTML

I am attempting to create a grid layout of 4 divs arranged in a 2x2 configuration. I would like to have a 1 pixel border between these divs, similar to the following structure:

1|2
-+-
3|4

Each div should have equal size and collectively fill the entire screen regardless of resolution. Initially, I tried to create 2 rows with 2 column divs each, using the floating left property. While the rows work perfectly, adding the border between the divs causes a scroll bar to appear. It seems like the border is not being included in the 50% width. How can I resolve this issue?

Here is the code I have been working on:

CSS

 html, body 
            {
                margin: 0;
                padding: 0;
                width: 100%;
                min-height: 100%;
            }

            .row
            {
                Width: 100%;
                Height: 50%;
             }

            .border
            {
                border-bottom: 1px solid black;
            }

HTML

<div class="row border" style="background-color: red;">

    </div>
    <div class="row" style="background-color: blue">

    </div>

I also attempted to create a demo of the code in a fiddle: DEMO but the height: 100% on body and/or html does not seem to work as expected.

Answer №1

Would you like something similar to this design? I created it completely from scratch...

View Demo

My approach involves positioning 4 div elements side by side, each occupying 50% of the width. I utilized the box-sizing property to ensure that borders do not affect the alignment of the div elements. These div elements are both 50% wide and 50% high.

<div></div>
<div></div>
<div></div>
<div></div>

* {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

html, body {
    height: 100%;
    width: 100%;
}

div {
    width: 50%;
    height: 50%;
    float: left;
}

div:nth-of-type(1) {
    background: #ccc;
}

div:nth-of-type(2) {
    background: #bbb;
    border-left: 1px solid #f00;
}

div:nth-of-type(3) {
    background: #aaa;
    border-top: 1px solid #f00;
}

div:nth-of-type(4) {
    background: #ddd;
    border-top: 1px solid #f00;
    border-left: 1px solid #f00;
}

Answer №2

Have you heard of the useful CSS property called box-sizing? By setting it to border-box, you can ensure that the widths of borders and padding are included in the width of an element. This means you can freely add padding to your div elements without worrying about them becoming too wide.

Interestingly, you don't necessarily need to enclose your rows in separate div elements. Simply specify that a div should be 50% wide and float them left, allowing two to neatly fit side by side.

Here's an example of how you can achieve this:

<div class="box">Box 1</div>
<div class="box">Box 2</div>
<div class="box">Box 3</div>
<div class="box">Box 4</div>

Additionally, by using CSS, you can create a border layout for your boxes. You can experiment with styling the first and second child boxes to create a visually appealing layout.

If you're looking for a more flexible approach, consider using a background color for the body element to simulate borders between the boxes. With a bit of CSS magic, you can adjust the number of boxes in your grid without hassle. Check out this example to see it in action.

Answer №3

Take a look at this demonstration:

height is utilized in place of min-height

http://jsfiddle.net/N6JuV/7/

It creates a 2X2 grid as per your requirement.

Ensure that the sum of your width % and margin % equals 100%. You can also use decimals for smaller margins.

For example, check out http://jsfiddle.net/N6JuV/8/

Answer №4

Check out my solution below:

<div class="grid-container">
   <div class="grid-row">
       <div class="grid-cell first"></div>
       <div class="grid-cell first"></div>
   </div>
   <div class="grid-row">
       <div class="grid-cell first"></div>
       <div class="grid-cell first"></div>
   </div>
</div>

Here is the CSS code:

.grid-container{
   display:table;
   table-layout:fixed;
   height:100%;
   width:100%; // Assuming body and html are set to 100% height and width
}
.grid-row{
   display:table-row;
}
.grid-cell{
   display:table-cell;
}
.first{
   width:50%;
}
.second{
   width:50%;
}

View the demo http://jsbin.com/otufir/2/edit

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

Ways to make an image wander aimlessly across a webpage while also spinning as it moves (with the help of jQuery or CSS)

I am working on making a div randomly move around a page. This is a related question Now, I have replaced the div with a picture like an "arrow" and I want it to randomly move while also pointing in the right direction by changing its rotation angle as i ...

Is there a way to resolve issues with window.open() on Heroku platform?

I've been working on hosting a webpage on Heroku using Node.js. Instead of using the php method, I opted to go with Node and it's been going smoothly so far. However, I've run into a small issue. Here's what my Procfile looks like: web ...

Delete the "img" and "a" elements from the content within the specified node

Is it possible to only extract text from my HTML content? var sb = new StringBuilder(); doc.LoadHtml(inputHTml); foreach (var node in Doc.DocumentNode.ChildNodes) { if (node.Name == "strong" || node.Name == "#text" || node.Name == "br" || no ...

What methods can be used to pause a jQuery function when hovering and resume it when the mouse leaves?

I'm currently working on a jQuery function that operates on the mousemove event. The goal is to have two images - one main image and one shadow image, with the shadow image reflecting movement based on mouse position. While everything is functioning p ...

The error message states: "change is not defined"

I am encountering an issue where I have 4 input boxes triggering a function on keyup using the onkeyup event. Strangely, I keep receiving an error stating that "change" (the function's name) is not defined. Despite correctly defining the function, I c ...

What is the term for the visual display of a product through a photo gallery or slideshow?

Can someone help me identify what the item I'm looking for? You can click on the link to see an example of the product: sephora product example What is the technical term for the section that contains product pictures and slides? Are there any librar ...

instructions on how to eliminate slideUp using jquery

I am trying to eliminate the slide-up function from my code $( document ).ready(function() { $("#tabBar ul.buttons > li > a").on("click", function(e){ //if submenu is hidden, does not have active class if(!$(this).hasClass("activ ...

Troubleshooting: Unable to preview Facebook Page plugin

When I visit the Facebook developer site to get the code for a Facebook page plugin, I use this link. The preview feature works perfectly with pages like "facebook.com/Nike", but when I try it with my own page, "facebook.com/BargainHideout", it doesn&apos ...

What is the best way to determine which id has been clicked using jQuery?

Can someone help me figure out how to determine which button has been clicked using jQuery? Here is the HTML code I am working with: <div class="row"> <div class="col-md-6"> <div class="well " id="option_a"> </div& ...

Customizing the appearance and dimensions of JWPlayer Audio Player with percentage-based styling

I'm currently attempting to set up an audio embed using JWPlayer, following the instructions provided in this particular article. The article suggests setting it up with the following code: <div id="myElement">Loading the player...</div> ...

The background image fails to load in ReactJS application

Usually, I have no trouble finding solutions to my problems, but this time I'm really stuck - and I apologize in advance if my question seems silly or trivial. I am currently working on a simple React app where I need to set a background image for th ...

Tips for shifting objects from a horizontal row to a designated vertical column when switching to a smaller display

I am currently utilizing bootstrap to enhance my website layout for mobile view. The main challenge I'm facing involves transitioning items from a row to a column in a specific order. In this case, I have two items in a row and I would like the first ...

How can I retrieve the $index value of an ng-repeat element within a uib-dropdown?

I am currently working on implementing an ng-repeat loop that includes a dropdown menu for each element. I want the dropdown menu to contain functions that operate on the specific element, requiring access to the index of that element. Below is the code sn ...

Discovering a deeply nested div or class using Beautiful Soup

Recently, I came across this URL: All I want to do is extract the zestimate and include it in a list. The specific class where it's located is: class="Text-c11n-8-65-2__sc-aiai24-0 eUxMDw". I attempted to target it at a higher level in th ...

How come my picture is clinging to the bottom of the container?

I am facing an issue where my "vertical rule" is sticking to the bottom of the container when placed to the right of the image. <div> <div style="display:inline-block; width:210px;"> <img src="res/img/PlayBtn.png" style="top:- ...

Display the input in the text box before making a selection from the dropdown menu

Latest Technologies: $(document).ready(function(){ $('#userID').change(function(){ $('#username').val($('#userID option:selected').data('username')); }); }); Coding in HTML: <select class="form- ...

What is the technique for creating multiple circles using CSS?

Creating multiple circles in CSS and linking them to my HTML file has posed a challenge. The code I have so far is: .circle{ height: 50px; width: 50px; background-color: red; } However, attempting to make another circle using the same 'circle' c ...

Using JavaScript to manipulate HTML content that has been dynamically injected using AJAX

I am in the process of developing a portfolio website where I want to fetch partial HTML files using an AJAX request. Here is the basic structure of the HTML: <div id="portfolio"> // Content will be replaced here </div> <a ...

Managing post and session data during PHP form submission

I've encountered an issue with my code structure. What I aim to achieve is: To validate if post values exist, then execute the query and show the results. If post values are not present, check for a session value. If session is available, manipulate ...

Bootstrap grid: Preventing horizontal scrolling when all columns cannot fit in a single row

Why is it that when the column does not fit in one row, instead of showing a horizontal scrollbar, it goes to the bottom? <div class="container-fluid main-container" style="overflow-x: auto; width:600px"> <div class="row mx-5"> < ...