Three Divisions Positioned at the Top, Middle, and Bottom

I need to place 3 divs at the top, middle, and bottom positions.

To achieve this, I have used jQuery to calculate the viewport height and apply a percentage of it to the top and bottom divs. However, resizing the page causes issues as the middle div overlaps either the top or bottom div. It is important for me to maintain a margin of around 10px for the middle div, as shown in the picture provided.

Below is the JavaScript code I am currently using:

$(document).ready(function() {

    function thirty_pc() {
        var height = $(window).innerHeight();
        var thirtypc = (40 * height) / 100;
        thirtypc = parseInt(thirtypc) + 'px';
        $(".botline, .topline").css('height', thirtypc);
    }

    $(document).ready(function() {
        thirty_pc();
        $(window).bind('resize', thirty_pc);
    });

});

I would like to know if there is a way to achieve the same result using CSS or jQuery more effectively.

I appreciate any assistance you can provide. Thank you.

Answer №1

DEMO

Give flexbox a try for your layout. It's a pure CSS solution that works seamlessly on all modern browsers. Check out this flexbox guide to get started.

HTML Code

<div id="mainFlex">
    <div id="box1">❶</div>
    <div id="box2">❷</div>
    <div id="box3">❸</div>
</div>

CSS Code

    div {
    border: 3px solid red;
    margin: 10px;
}
#mainFlex {
    display: -webkit-box;
    display: box;
    -webkit-box-orient: vertical;
    box-orient: vertical;
    -webkit-box-pack: center;
    box-pack: center;
    -webkit-box-direction: normal;
    box-direction: normal;
    -webkit-box-align: stretch;
    ox-align: stretch;
    border: 1px dashed blue;
}
#box1 {
    -webkit-box-ordinal-group: 1;
    box-ordinal-group: 1;
    -webkit-box-flex: 0;
    box-flex: 0;
    height: auto;
    min-height: 100px;
}
#box2 {
    -webkit-box-ordinal-group: 2;
    box-ordinal-group: 2;
    -webkit-box-flex: 0;
    box-flex: 0;
    height: 100px;
}
#box3 {
    -webkit-box-ordinal-group: 3;
    box-ordinal-group: 3;
    -webkit-box-flex: 0;
    box-flex: 0;
    height: auto;
    min-height: 100px;
}

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

"Alert box displaying error message is not appearing on the screen

In order to display an error message using a tooltip, I have hidden the tooltip by setting the style of a span with an id of demo to display:none. Then I use JavaScript's getElementById method to retrieve the value of the input field with an id of use ...

The mouse scurries away once the div height has been adjusted

How can I make the height of #header change when hovering over #hoverme, and then revert back to its original height when the mouse leaves #hoverme? If anyone knows a solution, please check out my jsfiddle as it's not working as I intended. Here is ...

Using jQuery to fetch and display content only when the user hovers over

Looking to optimize page loading speed by minimizing the loading time of social icons such as Facebook Like and Twitter follow buttons. Considering displaying a static image of the like buttons initially, with the actual buttons appearing on mouse hover. ...

"Adding properties to objects in a map: a step-by-step guide

During my attempt to add a property within a map loop, I noticed that the update appeared to occur on a duplicate rather than the original object. MY_ARRAY.map(function(d){ d.size = DO_SOMETHING }); ...

Ruby on Rails: clearing the asset pipeline cache by hand

While Rails usually clears the asset pipeline cache automatically when files are modified, I am facing a unique situation. I am providing my application as an HTML response to an Ajax request, cross-domain using rack-cors to bypass CORS. The issue arises ...

I'm looking for a way to incorporate JavaScript code within an iframe tag. Specifically, I'm attempting to embed code into a Wix website using the "Embed HTML

Trying to execute the code below on a Wix website using "Embed HTML", but IFRAME is blocking scripts. Seeking help to embed custom HTML with JavaScript on platforms like Wix.com or Wordpress.com, as the embedded code is not functioning due to IFRAME restri ...

When the web browser page is zoomed out (Ctrl -), elements do not appear to float

Can you test resizing the window to the minimum (CTRL-)? Here's the link: http://jsfiddle.net/Zty9k/13/ This is the HTML code: <ul> <li><img src="http://i.imgur.com/PBuDAFH.gif"></li> <li><img src="http://i.i ...

Consecutive pair of JavaScript date picker functions

My issue involves setting up a java script calendar date picker. Here are my input fields and related java scripts: <input type="text" class="text date" maxlength="12" name="customerServiceAccountForm:fromDateInput" id="customerServiceAccountForm:from ...

The error message "Uncaught ReferenceError: $ is not defined" in Laravel 7.x signifies that

In Laravel 7.x, I have been experimenting with JQuery. I inserted the following code at the end of the body tag in welcome.blade.php to test it out: <script type="text/javascript"> $(document).ready(function () { alert('JQu ...

The functionality of anchor links created through ajax is not operating correctly

Issue: I am encountering a problem where I have a table filled via Ajax, containing local anchors. When an anchor is clicked, it should make a section visible and automatically scroll to it. This functionality works when manually filling the table, but not ...

Unable to transmit FormData from a JSP page to a Servlet through jquery-AJAX

Having trouble sending POST data from a JSP file to a servlet using jQuery-AJAX. The servlet is receiving null values in the post request. Although the console on the JSP page displays the correct data, and Firebug in Mozilla confirms that the data is bei ...

Using HTML5 to display the {{data}} extracted from a JSON file right in the center of the text

Can someone help me with a question about working with html and angular5? <span [innerHTML]="'client.acceptance.explanation'| translate"></span> <span><b>{{data}}</b></span> I have text stored in a json file ...

Updated the application to Angular 6 but encountered errors when attempting to run npm run build --prod. However, running the command npm run build --env=prod was executed without any issues

Running the command npm run build -- --prod results in the following error messages: 'PropertyName1' is private and can only be accessed within the 'AppComponent' class 'PropertyName2' does not exist in type 'AppCompo ...

Is there a way for me to choose multiple checkboxes simultaneously?

I have a dynamically created HTML table with checkboxes added for each row. $.each(data, function(id, value) { rows.push('<tr><td><input type="checkbox" autocomplete="off" class="chkbox" name="chkbox" value="'+value.site+' ...

Exploring anchor elements within a div using Selenium in Python to extract URLs

Hey there, I'm currently attempting to iterate through anchor elements within a div and retrieve the links of these elements. <div class="List"> <a class="selected" href="link">text 1</a> <a ...

Mobile devices not displaying background images correctly

My Wordpress website has a section with the code below, but the image is not adjusting properly on mobile phones. Can you provide some advice? .one_half_bg { width: 50%; float: left; background-position: 50% 50% !important; backgroun ...

Achieving Content Centering in React Material UI: A Guide to Aligning to the Middle of the Page

Currently, the button is displaying in the top left corner. How can I move the button to the center of the page? import {Button} from '@mui/material'; function App() { return ( <Button variant="contained">Hello World</ ...

Implementing an unordered list in an inline format, even when it is collapsed with Bootstrap

Recently, I've been delving into Bootstrap and experimenting with coding a basic website. Below is the code for the navbar: <nav class="navbar navbar-default navbar-fixed-top" role="navigation"> <div class="container"> <div class ...

A step-by-step guide on showcasing freshly submitted input data through Ajax

I'm having trouble with this issue! Once I submit the new input, it doesn't show up in the div. Below is my JavaScript code: $('#addVariable').validate({ rules: { variable: {required:true}} , submitHandler: functio ...

Utilizing CSS to place an image on top of the upcoming <div> container

My goal is to create a layout similar to the one displayed in the linked image. https://i.stack.imgur.com/7DSE9.jpg Currently, I am using Bootstrap 3 and my HTML markup looks like this: <body class="gray-bg"> <section class="white-bg"> <d ...