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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:46:44+00:00 2026-05-25T09:46:44+00:00

I try to center a div on my page. It should use just relative

  • 0

I try to center a div on my page. It should use just relative values, so its size depends just on the window size.
I found a solution and it centers the box, but it seems that the margin isn’t calculated correctly. Instead the size of the body gets bigger then the window. In the following example firebug tells me that the box #container has size 1265×335 on my screen. When inspecting #content its size is 506×134 and 2px boarder on each side, which matches my calculation. But the top margin is 316.

Have I got something wrong?
The margin should be 25% of 335, right?
How can I fix this?

Here’s the HTML:

<body>
  <div id="container">
    <div id="content">
      nothing...
    </div>
  </div>
</body>

And here the CSS:

html {
  height:            100%;
}

body {
  height:            100%;
  margin:            0em 0em 0em 0em;
}

#container {
  height:            100%;
}

#content {
  text-align:        center;
  width:             40%;
  height:            40%;
  margin-top:        25%;
  margin-left:       auto;
  margin-right:      auto;
  margin-bottom:     auto;
  border:            2px solid black;
}
  • 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-25T09:46:45+00:00Added an answer on May 25, 2026 at 9:46 am

    From http://www.w3.org/TR/CSS2/box.html:

    The percentage is calculated with respect to the width of the generated box’s containing block. Note that this is true for ‘margin-top’ and ‘margin-bottom’ as well.

    So you get 25% of 1265 which explains the behavior you are seeing. Unfortunately, I don’t have any solutions to offer.

    Edit: CSS3 makes this statement dependent on whether the containing block is horizontal (you would want it to be vertical). I don’t think that any browsers implement the required block-progression property however (it might have unintended side-effects anyway).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm inserting some data into a div container via ajax. When its just straight
I have this page: http://www.comehike.com/hiking.php <div style=align:center> <div id=fb-root></div> <script src=http://connect.facebook.net/en_US/all.js#appId=217585988283772&amp;xfbml=1></script> <fb:like href= send=true
I found this jQuery code that allows to center a div that doesn't have
Rather than try describe it, I should just show you. The Korean text is
I'm try to change text color and align item in spinner to center of
i try make some parts of page with fixed style. I have similar HTML:
I am building a website in which a center div if pushed out of
I'm trying to copy the docklayout from one page and try to recreate it
I have a very simple holding page I built centering a div, anchor and
Hello everyone iam trying to open a aspx page in a dialog box window

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.