Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8320281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:36:19+00:00 2026-06-08T22:36:19+00:00

I would say that I’m great with HTML and CSS, it’s when I start

  • 0

I would say that I’m great with HTML and CSS, it’s when I start to touch JavaScript that I struggle; I can understand some of it but I couldn’t write it for my life! I’m trying to learn though, any ways; I’ve put together this jQuery script with the goal to vertically center a div between a relative positioned top div, and an absolute positioned bottom div. It’s not working out for me, lol.

Because I’m trying to learn how to work with jQuery and create my own scripts, I’d like to try and get this working. However, if there is a better way to accomplish my goal, I would greatly appreciate your input in that way as well!

<script type="text/javascript">
$(document).ready(function() {
$(window).ready(function(){
vertical_height()
});
$(window).resize(function(){
vertical_height()
});
function vertical_height(){
var doc_height = $(document).height();
var head_height = $(".headcontent").height();
var mid_height = $(".midcontent").height();
var foot_height = $(".footer").height();
var pos_height = Math.round(head_height+foot_height);
var neg_height = Math.round((head_height-foot_height)/2);
var fin_height = Math.round(doc_height-(pos_height-neg_height));
$('.midcontent').css({
"marginTop","-"+fin_height+"px",
"marginBottom","-"+fin_height+"px"
}
}
});
</script>

.headcontent is the top div.

.midcontent is the middle div that I’d like to center.

.footer is the bottom div.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:36:20+00:00Added an answer on June 8, 2026 at 10:36 pm

    Here’s your code, tidied into something that will run :

    $(function() {
        function vertical_height() {
            var doc_height = $(document).height();
            var head_height = $(".headcontent").height();
            //var mid_height = $(".midcontent").height();//not used
            var foot_height = $(".footer").height();
            var pos_height = head_height + foot_height;
            var neg_height = (head_height - foot_height) / 2;
            var fin_height = doc_height - (pos_height - neg_height);
            $('.midcontent').css({
                "marginTop": "-" + fin_height + "px",
                "marginBottom": "-" + fin_height + "px"
            });
        }
        $(window).resize(vertical_height).trigger('resize');
    });
    

    Now you can concentrate on getting the algorithm right.

    As it stands, combining and simplifying expressions, I get :

    var fin_height = doc_height - head_height*3/2 - foot_height*3/2;
    

    which doesn’t look right to me but I could be wrong.

    EDIT

    To avoid overlap, try this version :

    var $$ = {}; //jQuery object cache.
    $$.w = $(window);
    $$.h = $(".headcontent");
    $$.m = $(".midcontent");
    $$.f = $(".footer");
    function vertical_height() {
        var w = $$.w.height(),
            h = $$.h.outerHeight(true),
            m = $$.m.outerHeight(),
            f = $$.f.outerHeight(true),
            fin_height = Math.max(0, (w - h - m - f) / 2);
        $$.m.css('marginTop', Math.floor(fin_height)).css('marginBottom', Math.ceil(fin_height));
        $$.h.text(fin_height);
    };
    $(window).on('resize', vertical_height).trigger('resize');
    

    DEMO

    Notes

    • position:absolute removed from the footer’s CSS
    • jQuery objects now cached in $$ to give the resize handler less work to do.
    • Calculation now based on window height not document height.
    • The three divs now measured with .outerHeight(false) to include vertical padding and borders.
    • Overlap is prevented by Math.max(0, ...), which ensures fin-height can’t go negative.
    • The statement $$.h.text(fin_height); is for debugging and can be removed.

    EDIT 2

    The following code will “debounce” the resize event.

    Replace :

    $(window).on('resize', vertical_height).trigger('resize');
    

    with :

    var do_resize;
    $(window).on('resize', function(){
      clearTimeout(do_resize);
      do_resize = setTimeout(vertical_height, 100);
    }).trigger('resize');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Instinctively, I would say that this is impossible, as NHibernate needs to know some
How would I say that if a UITextField has @- in it, do something.
First I would like to say that I am new to this site, never
First off I would like to say that I think that my question may
First off I would like to say that this is my first post and
Lets say that I would like to construct a list (L2) by appending elements
Would it be correct to say that whenever casting is used, the resulting object
Let's say that accounts in my SAAS are of the type Account(models.Model) . Would
Let's say that I am 7 commits ahead of the origin/master repo. I would
Ok, let's say that I have a button and I would like to enable/disable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.