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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:38:28+00:00 2026-05-11T21:38:28+00:00

I have a page that I want centered, with a background and a border

  • 0

I have a page that I want centered, with a background and a border around the entire content.

I made a div and set it with the background color and the border settings I want.

Problem is that it has divs inside it that float and the background does not stretch around the floating divs. The only way I was able to get it to work was by setting position:absolute. Then the border did expand around the floating divs but I was unable to center them using regular html/css.

I found a javascript hack to make it center but it only centers after the page loads and it looks bad.

I am sure there is a way to have the container expand and still have it centered, I just can’t figure it out.

here is a sample html page that shares my problems

<div style="background-color: Red; width: 980px; position: absolute;" id="container">
    <br />
    <br />
    <br />
    <br />
    <div style="width: 400px; background-color: Black; float: left;">
        <br />
        <br />
    </div>
    <div style="width: 400px; background-color: Blue; float: left;">
        <br />
        <br />
    </div>
</div>

and here is the Javascript that makes it work (uses Jquery)

$(function() {
        var winH = $(window).height();
        var winW = $(window).width();
        $("#container").css('left', winW / 2 - $("#container").width() / 2);
    });

There has got to be a better way.

Thanks

  • 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-11T21:38:28+00:00Added an answer on May 11, 2026 at 9:38 pm

    Use auto as left and right margin to center the element.

    Put an element after the floating elements and use clear to put it below them. As it’s not floating, it will affect the size of the outer div.

    <div style="margin: 0 auto; background-color: Red; width: 980px;" id="container">
        <br />
        <br />
        <br />
        <br />
        <div style="width: 400px; background-color: Black; float: left;">
            <br />
            <br />
        </div>
        <div style="width: 400px; background-color: Blue; float: left;">
            <br />
            <br />
        </div>
        <div style="clear: both; height: 0; overflow: hidden;"></div>
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a vertical layout that I want to remain centered on the page.
I have a main page that I want to be a sort of dispatch
I have an aspx page that I want to convert to an HttpHandler ,
I have a form on a non-SSL page that I want to submit as
I am working with php and want to have a page that has a
I have an <li> element on the page that I want to look a
I have a page and I want to insert 'Some text' on that page,
I have a page with two tabs that I want to be able to
I have a page with a number of ListViews that I want users to
I have a page layout for my MOSS '07 site that I want put

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.