The issue of multiple columns not displaying correctly when set to a height of 100

I am struggling with a seemingly simple task. I want to create two columns on my page, each with a width of 50% and a height of 100% so they completely fill the screen side by side. However, no matter what I try, they do not align properly and end up floating with each other.

I attempted to use jQuery to address this issue, but when the window is resized, the columns no longer fill the entire page. I have searched for solutions online, but nothing seems to offer a straightforward answer for creating multiple columns like this. Many websites seem to achieve this layout effortlessly, so there must be a way to do it efficiently using pure CSS or more effective jQuery techniques.

http://jsfiddle.net/nFeh8/1/

<div class="container">
    <div class="on"> 
        <button type="button" class="btn-on">
            on
        </button>
    </div>

        <div class="off">
        <button type="button" class="btn-off">
            off
        </button>
    </div>

</div>

.

html {
    height: 100%;
}

container {
    width: 100%;
    height: 100%;
}

.on {
    width: 50%;
   height: 100%; 
    background-color: #FEE;
    float: left; 
}

.off {
    width: 50%;
   height: 100%;
    background-color: #666;
    float: left;       
}

.btn-off {
    position: relative;
}

.

$(document).ready(function() {
    var height = $(window).height();
    $('.on').css({'height':height + 'px'});
    $('.off').css({'height':height + 'px'});
});

Answer №1

Utilizing jQuery allows you to invoke the same functions when the document is ready and when the window is resized:

var adjustBlocks = function () {
    var height = $(window).height();
    $('.on').css({'height':height + 'px'});
    $('.off').css({'height':height + 'px'});
};

$(document).ready(adjustBlocks);
$(window).resize(adjustBlocks);

See a demonstration: http://jsfiddle.net/effone/nFeh8/6/

Does % value relate to height? I have some uncertainties about that ...

Answer №2

To achieve this, I recommend using div elements styled as a table. This is a simple and easy-to-understand method. Here is an example code:

CSS:

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

.div-table {
    display:table;
    width:100%;
    height:100%;
}

.div-table-row {
    display:table-row;
}

.div-table-cell {
    display:table-cell;
    width:50%;
    height:100%;
}

/* styles to differentiate columns */

.div-table-cell:nth-child(1) {
    background-color:red;
}

.div-table-cell:nth-child(2) {
    background-color:green;
}

HTML:

<div class="div-table">
    <div class="div-table-row">
        <div class="div-table-cell"></div>
        <div class="div-table-cell"></div>
    </div>
</div>

See the JSFiddle link for demonstration: http://jsfiddle.net/T9BsG/1/

Main advantages are no need for JavaScript (which helps keep the site lightweight) and avoiding float:left; which can sometimes cause unexpected layout issues.

Answer №3

Not sure if this solution will be compatible with your needs, but here's a suggestion...

.on {
    width: 50%;
    height: 100%; 
    position: absolute;
    bottom: 0;
    background-color: #FEE;
}

.off {
    width: 50%;
    height: 100%;
    position: absolute;
    bottom: 0;
    left: 50%;
    background-color: #666;      
}

You may not even require the container for implementing this.

Answer №4

To ensure that the container on the right is positioned correctly in both classes, the code below can be used. The container should have a position of absolute so that it will be placed exactly where specified on the page:

.on {
    position: absolute;
    width: 50%;
    height: 100%;
}

.off {
    position: absolute;
    left: 50%;
    height: 100%;
}

Feel free to add any other attributes as needed.

Answer №5

While I may not have a wealth of experience in programming, I recently encountered a similar issue where I needed to divide the width of a wrapping container into 4 blocks. Strangely enough, I faced the same problem as you did - the fourth block was stubbornly sliding down below the first block. After much trial and error, I discovered that by changing the width from 25% to 24% in my CSS, the blocks lined up perfectly. It turns out that it was the margin causing the misalignment, so I factored in 1% for both the margin and padding. Hopefully this insight proves helpful to you as well.

Answer №6

There is no need to rely on jquery or tables (or table display types) for this particular task. There are two key steps:

Firstly, make sure to assign a height to the body element. Essentially, everything between your designated element and the window (or desired container) should have a defined height.

html, body { height: 100%; }

Secondly, be sure to target the div with the class "container", not an element named container. Remember to include a period before the class name:

.container {
    width: 100%;
    height: 100%;
}

View the implementation in this Fiddle: http://jsfiddle.net/nFeh8/7/

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

Incorporate personalized feedback into a datatable with server-side processing

Trying to implement server-side processing for a datatable loaded from an AJAX response using this specific example The server response being received is as follows: {"msg":null,"code":null,"status":null,"result":[{"aNumber":"3224193861","bNumber":"32159 ...

Assign a "margin-bottom" value of "0" to all elements in the final row of a responsive layout

I'm currently working on a Bootstrap responsive template design where I have multiple items arranged in rows. The number of items per row and the number of rows vary depending on the screen resolution. <div class="row"> <div class="col-xs- ...

Issues encountered while utilizing the jQuery function $.ajax()

I'm encountering issues with utilizing ajax in conjunction with jQuery... Here is the script I am using: $(document).ready(function() { $('rel[close]').click(function() { $(this.parent).close(); }); $('a[rel=trac ...

Adding a background image in javascript using data from a MySQL database

My current tech stack includes CodeIgniter, vanilla JavaScript, AJAX, CSS, and MySQL. I am trying to figure out how to set the background of an image that is stored in a MySQL database. While the following code is error-free and working perfectly, my cha ...

Sending data with a post request using Ajax in ASP.NET MVC 3

Hey everyone, I'm working on an ajax login call. I'm serializing the form data and sending it to the server to get a redirect URL link. However, I'm running into an issue where my URL after the post ends up looking like http://localhost:508 ...

Easily move a group of HTML elements at the same time with a simple

Exploring HTML5 Drag and Drop with Multiple Elements When it comes to dragging and dropping multiple elements in HTML5, there seems to be a limitation with the default draggable attribute. This attribute allows only one element to be dragged at a time, ma ...

The left-floated image extends beyond the boundaries of the div

I have a situation where text and an image are combined but the cat image goes outside of the parent div. Is there a solution to this issue? View the code here <div style="border:1px solid #CCCCCC"> <img style="float: left;" src="http://plac ...

How can we assign priority to the child element for detection by the "mouseover" event in jQuery?

Can you help me figure out how to make the "mouseover" event prioritize detecting the child element over its parent? This is the jQuery code I've been using: <script> $(function() { $("li").mouseover(function(event){ $('#log&a ...

In Internet Explorer, when utilizing a table-cell with a variable height, the position should be set to absolute for

I'm struggling with getting a uniform height in my horizontal layout using display: table-cell. While it looks great in Chrome, I can't seem to get Internet Explorer to display it the same way. Check out this link for reference This is how I wa ...

How can I ensure the Jquery datepicker functions correctly?

I've been attempting to create a jsp page with some Jquery functionalities. Unfortunately, despite my best efforts, I am unable to make it work. I have downloaded jquery1.7.1 and jquery-ui1.8.17 (non-mini), renamed them to jquery171.js and jquery-ui. ...

Antonio Mele's latest book on Django3 and Ajax Button Development

After clicking the like button, it shows a count of 2099 instead of 1. However, after refreshing the page, it displays the correct count of 1. The same issue occurs when unliking. The count is accurate only after a refresh, otherwise, it randomly shows eit ...

Error message displaying "Invalid ID attribute in HTML 5 input"

On my website, I have a page with multiple items, each containing an HTML5 input field. Whenever a user changes the value of the input, it triggers an AJAX call to the server. The server then responds with JSON data, including an array of items that have ...

Move the scroll event binding from the parent element to the child element

I'm working on an HTML page with the following structure: <div class="parent"> <div class="child1"> <div class="child2"> </div> </div> </div> Currently, I have a scr ...

Is there a function for displaying the message "User is currently typing"?

I've been working on a chat system in PHP/jQuery, where the message 'User is typing a message...' appears at the bottom. Despite trying numerous methods, I have yet to succeed. My chat system utilizes PHP, MySQL, Ajax, and jQuery. While I&a ...

The art of combining CSS3 gradients with background images

I've created a function that changes the parent element's background gradient when a checkbox's status is toggled. Check out the Lorem link to see it in action! Click here to view. However, I've encountered an issue with applying this ...

Utilize jQuery to export HTML or JSON data to an Excel spreadsheet

I am looking for a way to export json or html data to an excel sheet using only html and jquery, without involving any server code. I have found some fiddles that allow me to download the excel sheet successfully, but unfortunately, none of them work in In ...

Encountering Empty Output When Trying to Save HTML Form Data to Database

I have recently started learning PHP and web development, and I am attempting to build an HTML form that will submit data to MYSQL. After submitting the form and checking phpMyAdmin, it shows that a row has been submitted, but the row is empty. Previously ...

Clicking on the anchor at the bottom of the page will smoothly navigate you to the top of the page

I added an anchor at the bottom of the page. I wrapped a group of buttons with the link so that when clicked, they trigger the assigned JavaScript and scroll to the bottom of the page. However, the buttons currently execute the JavaScript but then take you ...

Tips for utilizing the form.checkValidity() method in HTML:

While delving into the source code of a website utilizing MVC architecture, I encountered some difficulties comprehending it fully. Here is a snippet of the view's code: function submitForm (action) { var forms = document.getElementById('form& ...

Centered title in side menu for Ionic 3

I recently utilized the Ionic CLI to create an Ionic project. The version I am working with is Ionic 3. Currently, I am facing a challenge in centering the title image. Any assistance on this matter would be greatly appreciated. <ion-menu [content]=" ...