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

"Utilize AJAX to submit the value of the text box input from a jQuery slider when the Submit Button

Whenever I adjust the sliders, the value is shown in an input textbox. However, when I move the slider and check the values echoed from the textboxes on another PHP page, they are always displaying as 0. Even after clicking the submit button, it still echo ...

Tips for deciding on the appropriate CSS for an Angular 6 navbar component

I am currently working on an angular 6 application where users are assigned different roles that require distinct styling. Role 1 uses Stylesheet 1, while Role 2 uses Stylesheet 2. The Navbar component is a crucial part of the overall layout structure of ...

Show the res.send() method from express.js without replacing the existing html content

Currently, I am in the process of developing a calculator application using express.js. The app needs to handle get requests for an HTML file and post requests that capture user input and provide the response accurately. My challenge lies in showcasing the ...

What could be the reason why my POST endpoint isn't able to receive this AJAX request?

I am currently working on a JavaScript function that is supposed to send JSON data to my escreve POST REST method. $(document).ready(function() { $("#idform").on('submit', function(e) { e.preventDefault(); alert($("#idform"). ...

Is it possible to transfer data from javascript to php through ajax?

I am attempting to extract the data speedMbps from my JavaScript code using Ajax to send the data to my PHP script, but unfortunately, I am not receiving any output. My experience with Ajax is limited to implementing auto-completion feature. <script sr ...

Error message: Fancybox unable to open PDF file - ReferenceError: href is not defined

I came across some really interesting code on a website detailing how to open a PDF within a Fancybox iframe. However, I am trying to implement this into a CSS Menu that I generated using PHP. When I try to run the code, I receive an error message in the G ...

Transmit HTML form information through a POST request to an ASP.NET web service

Having an HTML page with a contact form is great, but I'm facing the challenge of sending the data using AJAX. I attempted to send the data to a webservice without converting the structure to JSON format, but unfortunately, I did not succeed. Is ther ...

two elements with the inline-block property not properly displaying inline and behaving like block elements

Having a CSS issue - attempting to display two code blocks with numbers on the sides next to each other, then stack them after a certain screen size. However, there seems to be an error occurring when the screen size is smaller. I'm open to making an ...

Enhance your scheduling capabilities with FullCalendar2's customizable minTime and maxTime settings for each

Can each weekday have different minTime and maxTime in fullcalendar (the hours on the left side)? For example: Monday: 08:00 - 18:00 Tuesday: 08:00 - 18:00 Wednesday: 10:00 - 19:00 Thursday: 13:00 - 20:00 ... The default setting in fullcalendar appl ...

Encountering an issue stating "The element is not visible at the moment, therefore cannot be interacted with" while trying to access a text box within a window opened via an ajax call

Currently, I am working on a Selenium script using Java to automate a specific process. However, I have encountered an issue where clicking on a dropdown causes a hidden popup to appear through an ajax call. Unfortunately, when attempting to interact with ...

Enclosing values in JavaScript literals

I apologize for the inconvenience. I am unsure why it is not functioning properly. If I input <button type="button" onclick="document.getElementById("demo").innerHTML = Date()">click</button> the above code does not work. However, if I u ...

Focusing on a specific div element

I need help selecting the initial div block that contains the word "Target". <div class="box-content"> <div> <div>Target</div> <div>xxxx</div> <div>xxxx</div> <div>xxxx</div> ...

Generate an image of a "button" with dynamic hover text

I am trying to create a clickable image with dynamically changing text overlaid on it. I have the PHP code that retrieves the necessary information for the text, but I am struggling to create the button as I am new to this type of task. Here is an example ...

Altering the positioning of the spinning wheel feature in jQuery

I've successfully implemented a spinning wheel feature that appears when clicking a link in jQuery UI Tabs. However, the issue is that the wheel appears next to the link instead of at the top of the div container within the tabs. The jQuery code snip ...

Learn how to retrieve the value of an associated field at a specific index by utilizing a combo box in JavaScript when receiving a JSON response

Hey there, I'm currently working on a phone-gap app where I need to fetch data from a WCF service that returns JSON responses. Specifically, I want to display the DesignName in a combo box and pass the associated designId. Any thoughts on how I can ac ...

Guide to saving a Python docx file on the client side with the help of Ajax

I am currently using Python 3.6 with Django, and my code snippet is shown below: from docx import Document document = Document() document.add_heading('My docx', 0) document.save('myFile.docx') return HttpResponse(document, content_typ ...

Activating the mousewheel effect exclusively on specific div sections, rather than the entire page

After researching mousewheel events in jQuery, I realize that my lack of knowledge may be hindering my ability to find useful answers. I am looking to create a mousewheel effect that is only triggered within a specific div called #scroller. Currently, I am ...

Achieving uniform alignment of multiple field labels in Bootstrap and Simple Form

Here is the current layout of my form: https://i.sstatic.net/2vZjl.jpg You may notice that the labels are not properly aligned. I would like them to appear like this: https://i.sstatic.net/gm39D.jpg Below is the code for all the input fields: <%= si ...

Is there a way to eliminate duplicates from an array in JavaScript by utilizing a set and placing each element in an li tag? Here is the code

const numbers = [" 4", "9", "16"," 4", "9", "16"," 4", "9", "16"] <ul> {(<li> {[...new Set(numbers)]} </li>)} </ul> const numbers = [" 4", "9", "16"," 4", "9", "16"," ...

How to align several lines of text in a table cell

I've been trying to center multiple lines of text in a table cell using the table method, but no matter how many online guides and SO posts I follow, I just can't seem to get it right. What I'm aiming for is to have the text perfectly cente ...