Ensure that both columns are of equal height

I attempted to create two columns with the same height, but it did not work as expected.

Here is my HTML code:

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="utf-8" />
   <title>@ViewBag.Title</title>
   <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
   <meta name="viewport" content="width=device-width" />
   @Styles.Render("~/Content/css")
   @Scripts.Render("~/bundles/modernizr")
   @Scripts.Render("~/bundles/jquery")

   <script type="text/javascript">
        jQuery(document).ready(function () {
        var divone = jQuery(".main-content").height();
        var divtwo = jQuery(".sidebar").height();
        var maxdiv = Math.max(divone, divtwo);
        jQuery(".main-content").height(maxdiv);
        jQuery(".sidebar").height(maxdiv);
    });
 </script>
</head>
<body>
@RenderBody()
<header>
    <h1>Document Heading</h1>
    <div class="nav">
        <ul class="menu">
            <li>@Html.ActionLink("Home", "Index", "Home")</li>
            <li>@Html.ActionLink("About", "About", "Home")</li>
            <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
            <li>@Html.ActionLink("Sermon", "Sermon", "Home")</li>
        </ul>
    </div>
</header>

<div class="main-content">

    <h2>Column 1</h2>
    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit...</p>
</div>
<div class="sidebar">
    <h2>Column 2</h2>
    <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit...</p>
    <ul>
        <li><a href="#">Link 1</a></li>
        <li><a href="#">Link 2</a></li>
    </ul>
</div>
<footer>FOOTER</footer>
</body>
</html>

This setup represents an ASP.NET MVC 4 _Layout.cshtml. The relevant CSS portion is provided below:

body {
font-size: .85em;
font-family: "Segoe UI" , Verdana, Helvetica, Sans-Serif;
margin: 0;
padding: 0;
background-color: #CCCCFF;
}


 .main-content
{
float: left;
width: 60%;
margin-left: 5%;
}

.sidebar
{
float: right;
width: 35%;
background-color: #00FFFF;
}

footer
{
margin: 0px;
clear: both;
background-color: #FF6600;
font-size: .8em;
text-align: center;
height: 50px;
position: absolute;
bottom: 0;
width: 100%;
padding-top: 20px;
}

Despite using jQuery to make the two columns equal in height, they still display different heights. Any idea why this might be happening?

Update: Check out this fiddle.

Answer №1

Your div appears to have a consistent height. To test this further, try adding a background color to the .main-content class.

.main-content{
    float: left;
    width: 60%;
    margin-left: 5%;
    background-color: #006400; /*testing*/
}

Answer №2

When faced with this task, my go-to solution involves utilizing jQuery.

var firstColumnHeight = $(firstColumn).innerHeight();
var secondColumnHeight = $(secondColumn).innerHeight();
if (firstColumnHeight > secondColumnHeight){
  tallestHeight = firstColumn;
}else{
    tallestHeight = secondColumn;
}
$(firstColumn).css('height',tallestHeight);
$(secondColumn).css('height',tallestHeight);

Answer №3

I successfully solved the issue on my own.

var mainContentHeight = jQuery(".main-content").height();
var sidebarHeight = jQuery(".sidebar").height();
var windowHeight = $(window).height() - $('header').height() - $('footer').height();
jQuery(".main-content").height(windowHeight);
jQuery(".sidebar").height(windowHeight);

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

Implement a feature in JSP that allows users to dynamically add or remove fields before submitting the data to the database

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http- ...

Is there a way for me to increment the value of 'sessionStorage.fgattempt' whenever either 'fgMade()' or 'threeMade()' are called?

Currently, I am developing a basketball game stat tracker and need to update the field goal attempts every time I make a field goal or three-pointer. Additionally, I am looking for ways to optimize the javascript code provided. The main requirement is to ...

Why is the bootstrap modal not showing up when editing the data?

I am currently working on a Blazor application with .NET Core 7. I am facing an issue where the Bootstrap modal does not display as active when clicking the edit button. When trying to edit data using the Bootstrap modal, the modal does not display as act ...

How can I verify an element's attribute while employing Event Delegation?

Is there a method to determine if a particular element has been activated using Event Delegation, based on its attribute, class, or ID? <ul> <li><button>Make the first paragraph appear</button></li> <li><butto ...

Styling group headers within an unordered list using pure CSS - possible?

Hey there, I'm looking to spruce up my UL by adding group headers. Here's a sneak peek at the structure I have in mind (keep in mind this is generated dynamically from a database): <ul> <li class='group group-1'> < ...

Avoid using CSS browser prefixes when selecting plugins or addons

I tried searching on Google for a solution, but I couldn't find anything! Is there a trick or library available to avoid repeating CSS extensions for browsers? Instead of using -webkit-, -moz-, and -o- prefixes? -webkit-animation: NAME-YOUR-ANIMATI ...

Tips for restricting the line length in email XSLT

I am currently working on creating a report that needs to be sent via email. However, I have encountered an issue where the lines get cut off if they exceed about 2040 characters in length by email daemons. I have been using XSLT to construct the email rep ...

Display an Asterisk Icon for md-input fields with lengthy labels

Documentation states that md-inputs add an asterisk to the label if it is a required type. However, when input containers have width constraints and long labels, the label gets truncated and the asterisk becomes invisible. From a user experience perspectiv ...

Steps for obtaining a CSV or PDF file as the response to an AJAX request

Currently, my project is built on Django. I am in need of a solution to receive a PDF, CSV, or XLS file as a response after submitting a form via AJAX. While the file is successfully received when I submit the form normally, it does not display the downl ...

Attempted to include a reference to System.Runtime in the GAC, but it was not found

Should I go with ASP.NET 4.5.1 or 4.5.2? Trying to upgrade Nuget package MicrosoftAspNet.Identity.EntityFramework from version 2.2.1 to version 3.0.0-rc1-final An error occurs: Encountered a failure while adding reference. The package 'Microsoft.As ...

execute operations on a separate webpage

Currently, I am facing a challenge while developing an application that allows regular users to modify data that is typically only accessible through an admin panel. My application effectively filters out any sensitive information; however, once the data i ...

Struts2 jQuery tag select fails to fetch data

I've been attempting to utilize the <sj:select> tag to display my list in Struts2 JSP. Within the same section, I have also included a <s:select> tag The data is being populated in the <s:select> tag while no data is showing up in ...

Is there a way to keep my navbar fixed at the top as I scroll down the page? And is it possible to have a picture appear above the navbar when scrolling up to the top?

I am looking to create a design where a picture appears above my navbar, and as I scroll down, I want the picture to stay fixed at the top so that it is always visible, regardless of how much I scroll. However, when scrolling back up, I want the picture to ...

Button press triggers the fadeIn function successfully, but the keypress event does not have the same effect

I'm currently facing an issue with two div elements on my webpage. I want only one of them to be visible at a time, depending on which key is pressed. If the 1 key is pressed, I want 'div1' to fadeIn (if it's not already visible) and fo ...

Notifications for AngularJS tabs

I need help finding a method to incorporate "tab notification" using AngularJS, in order to show that there are important alerts that require attention. For example: (1) (3) TAB_ONE TAB_TWO TAB_THREE Could you provide any recom ...

Hide and reveal images on hover by using multiple images or links on a single webpage

Despite my efforts, I haven't been able to find exactly what I'm looking for. My current setup involves an image/content slider powered by Awkward Showcase. Within each slide, I want to incorporate a mouse-in/mouse-out effect that switches and hi ...

The jQuery Show Hide feature is experiencing issues specifically on Internet Explorer

Everything looks good in Firefox and Chrome, but unfortunately it's malfunctioning in IE. Does anyone have a suggestion for hiding select Dropdown options? I attempted using CSS with display: none but it didn't work. $j("#id option[value=&apos ...

Tips for enabling autocomplete in ASPX using the original page request exclusively

Currently, our team is working on a project involving an ASPX (.NET 2.0) page with a select list that contains over 200 elements. We are in need of autocomplete functionality to transform this select list into a user-friendly text box with auto-suggestion ...

Issue with PHP redirection not functioning as expected upon submission

Thank you for taking the time to offer assistance. I am encountering an issue with a basic HTML contact form that utilizes a PHP file for processing. The form's method is set to post and it directs its action to a PHP file. Upon completion of the PHP ...

The NVD3 tooltip is being obscured by other divs

Attempting to make the NVD3 tooltip appear above all other divs has presented a challenge. With three charts lined up horizontally and tooltips that exceed the boundaries of their divs, adjusting the z-index creates a dilemma. Regardless of which side&apos ...