Attempting to align two div blocks side by side to occupy the entire page

http://jsfiddle.net/UeLMA/1/

Here's the HTML code snippet:

<div id="left" style="background: red; display: inline-block; float: left">
    aaaa<br />
    aaaaa
</div>
<div id="right" style="background: yellow; float: left"></div>

My goal is to make #right fill the remaining space that #left isn't using on the screen. I have attempted to achieve this using jQuery:

$('#left').height($(window).height());
$('#right').height($(window).height());
$('#right').width($(window).width() - $('#left').outerWidth());

However, I am encountering an issue as it doesn't seem to be working as intended. Feel free to check out the jsfiddle for reference: http://jsfiddle.net/UeLMA/1/

Do you have any suggestions or solutions?

Answer №1

Updated version: http://jsfiddle.net/UeLMA/4/

Include the following code snippet in your CSS to reset:

* {
margin: 0;
padding: 0;
}

Answer №2

The issue lies in the sizing of the two divs on your page. The second div is not fitting next to the first one because it does not take up the entire window width. To fix this, adjust the width of the second div using the following code:

$('#right').parent().width() - $('#left').outerWidth()
.

You can see the updated version in action on this fiddle:

http://jsfiddle.net/UeLMA/9/

To ensure accurate sizing, I am using the .parent() selector instead of relying solely on the window dimensions. This way, we account for any margins or padding that may be present on the containing body element.

Answer №3

Forget about jQuery, you can achieve this using just a few CSS tricks.

  1. If you want an element to have 100% height, make sure all its parent elements also have their height set to 100%.

    CSS:

    html, body, #left, #right { height: 100%; margin:0; }

  2. To make elements fill the entire page width, float them both to the left and give them a width of 50% each (or any ratio that adds up to 100%)

    CSS:

    #left, #right { float: left; width: 50%; }

Check out the live example here: http://jsfiddle.net/UeLMA/2/

Answer №4

Don't forget to take into consideration the padding and margin around the body.

http://jsfiddle.net/UeLMA/6/

CSS:

#left { background: red; display: inline-block; float: left }
#right { background: yellow; float: left; }
body {padding: 0; margin: 0;}

Make sure to switch from using outerWidth() to width():

$('#left').width()

Answer №5

Avoid relying on JQuery for this issue. The key issue in your script is that the default setting for div elements is to take up 100% width, causing them to stack vertically. Simply define specific widths for each left and right div, assign a height of 100% to both, and eliminate unnecessary JQuery code.

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 an innovative solution to automatically close the navbar component in tailwindcss on

I've been attempting to create a unique tailwind navbar component for React, but I've encountered issues when trying to make it close on scroll for mobile view. I found the initial code on this website. After that, I tried implementing the code ...

.htacces Disables Styling on Page

I've been trying to understand the functionality of .htaccess RewriteRule. Here is a URL example where I have experimented with .htaccess: http://example.com/test/home This used to be: http://example.com/test/index.php?p=1 My goal is to redirect ...

Can the hasClass() function be applied to querySelectorAll() in JavaScript?

As a beginner in javascript and jquery, I recently attempted to manipulate classes using the following code snippet: if ($(".head").hasClass("collapsed")) { $(".fas").addClass("fa-chevron-down"); $(".fas&qu ...

Strikeout list on click of a mouse

Is there a way to apply strikethrough formatting to text with just a mouse click? The CSS for lists is beyond the form field margin. I've tried multiple methods without success. No matter how many times I change the code, I can't seem to get it r ...

What's the best way to add animation to the send icon while hovering over the button?

<div class="text-center"> <button [disabled]="btnStatus" class="btn btn-secondary buttonSend" type="submit"> <div [hidden]="btnStatus"> Send Message&nbsp;&nbs ...

Can you provide me with a comprehensive list of all the colors available in Twitter Bootstrap 3?

I've integrated the twitter bootstrap sass 3.0.3.0 gem, which is supposed to be the latest version of twitter bootstrap 3.0.3. I'm trying to customize the colors using the 'customize' section of the LESS variables, but I can't seem ...

Trigger jQuery to play audio files

Something seems off with this code. The audio file isn't playing when the webpage loads. $(document).ready(function() { var audioElement = document.createElement('audio'); audioElement.setAttribute('src','content/aud ...

Generate text in reStructuredText with columns

I'm attempting to style a reStructuredText segment in the following layout: type1 : this type4 : this type7 : this type2 : this type5 : this type8 : this type3 : this type6 : this type9 : this I want to fill the page with tex ...

Wiki experiencing issues with NodeJS HttpGet functionality

Goal Retrieve the HTML content of a Wiki Page. Introduction In an attempt to fetch the HTML of a Wiki page () for data parsing purposes, I am utilizing NodeJS and its HTTP Request methods. Code Snippet Below is the simple code snippet that accesses th ...

the functionality of jquery/ajax is not functioning properly

It seems like I am having some trouble with this code. Despite copying and pasting it correctly, the output is going to html-echo.php instead of displaying in htmlExampleTarget Can someone please point out what mistake I might be making here? Thank you, ...

Is the [] value always populated in the second DropDownList?

After populating two DropDownList in my View, I encountered an issue with the values not updating as expected: <tr> <td class="info_label">City</td> <td>@Html.DropDownListFor(m=>m.User.City,Model.City,"-- Select ...

How can you utilize the Array submission syntax within HTML coding?

I currently have numerous input fields within a form, and some of them are structured like this: <input name="agents[]" type="file" /> Additionally, imagine there is a plus button next to this field as shown below: <img src="plus.jpg" id="some_ ...

Passing a JSON object to a different page using jQuery

I'm currently facing a challenge with sending JSON data (demographics) to a new page within the same directory when a user clicks on a marker that I've placed on a Google map on my website. I am using the jquery-ui-map plugin, and while the marke ...

How can I immediately disable a button upon clicking "cancel" on a modal that contains TextFields in React Version 18?

New to React development. Looking for a way to reset a button to its initial disabled state when canceling out of a modal. The scenario is that upon clicking a button, a dialog will appear with an "ADJUST" button that is initially disabled until text is ...

What is the best method for submitting information using AJAX and jQuery?

Here is the HTML code in question: <script src='https://code.jquery.com/jquery-1.12.4.min.js'></script> <p>1</p> <!-- this content gets loaded with <?php echo $item->id; ?> --> <a id='delBtn1' ...

Creating a custom video to use as the favicon for my website

Imagine this: With the help of this plugin, you can have a video playing as your site's favicon using the following code snippet: var favicon=new Favico(); var video=document.getElementById('videoId'); favicon.video(video); //stop favicon.v ...

Having problems with querying a REST service due to JSONP limitations

I'm encountering issues with JQuery's JSONP functionality: http://jsfiddle.net/emepyc/tuCvT/ $.ajax({ url : url, dataType : 'jsonp', contentType : "application/json", jsonpCallback : 'cback', ...

Troubleshooting Issue with Nested ng-include in AngularJS: Difficulty arises when the parent element with the ng-include attribute is dynamically added using the ng-

Click here to see a demo plunker that will help you understand my issue. On my main page, I have a table. Each table row is followed by a hidden empty row. When the first row is clicked, I use a directive to inject HTML into the empty row below it. Main ...

Retrieve the rendered component color variables exclusively from the global color variable file

color_variables.css: [data-theme='default'] { --avatar_bg: #000; --avatar_text: #fff; --avatar_border: red; --button_bg: blue; --button_text: #fff; --button_border: darkblue; --modal_widget_bg: orange; --footer_bg: yellow; --foo ...

Creating new accounts with SQL in HTML code

I am working on a query to input any information entered into a form into the database. I have written some code, but I believe there is an issue with the query itself as I am unsure how to include variables in a SQL query. If anyone could assist me with t ...