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 3691282
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:07:48+00:00 2026-05-19T04:07:48+00:00

On this page , I would like to horizontally center the main #container div

  • 0

On this page, I would like to horizontally center the main #container div relative to the page. Normally I would achieve this by adding a CSS rule,

#container { margin: 0 auto }

However, the layout of this page (which I did not write), uses absolute positioning for #container and most of its child elements, so this property has no effect.

Is there any way I can achieve this horizontal centering without rewriting the layout to use static positioning? I’ve no problem with using JavaScript/JQuery if that’s the only way to achieving my objective.

  • 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-05-19T04:07:49+00:00Added an answer on May 19, 2026 at 4:07 am

    Same as @rquinn answer but this will adjust to any width. Check this demo

    http://jsfiddle.net/Starx/V7xrF/1/

    HTML:

    <div id="container"></div>
    

    CSS:

    * { margin:0;padding:0; }
    #container {
        width:50px;
        position:absolute;
        height:400px;
        display:block;
        background: #ccc;
    }
    

    Javascript

    function setMargins() {
        width = $(window).width();
        containerWidth = $("#container").width();  
        leftMargin = (width-containerWidth)/2;    
        $("#container").css("marginLeft", leftMargin);    
    }
    
    $(document).ready(function() {
        setMargins();
        $(window).resize(function() {
            setMargins();    
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like the navigation links on this page to each appear on their
I would like to know how to use CSS positioning and floats to achieve
Does anybody know why the same code from this page http://emacsformacosx.com/ would not render
Possible Duplicate: How to Vertically Align a Table in CSS I would like to
I would like to create a footer which is relative to the content (so
I am using JQuery UI and would like to position my dialog horizontally centered
This is a followup to my question here . I would like to understand
My website is setup with the content structure like this <body> <div id=header></div> <div
I ask this as I would like a global toolbar in my app (similar
My Case: I have original Page Control app from Apple. I would like to

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.