Results 1 to 6 of 6

Thread: Centering DIVs - help needed!!

  1. #1
    Join Date
    Dec 2008

    Smile Centering DIVs - help needed!!

    Hello! I am currently designing a website. It's the first time I've done anything like this for a good few years and the first time I've used CSS which is very easy thus far until I've tried to centrally align some <div> blocks! I've already looked all over the internet, read about 40 existing threads and tried every suggestion I've seen but with no luck. Also many of the suggestions deal with old browsers; I have Firefox 3 and IE 7 although obviously I would like my website to work in all browsers!!

    Basically, I want my whole website to be horizontally centred. As you can see below I am at the very start of the design process and simply want my header block and my navigation menu block to be centrally aligned. I tried putting margin-left: auto and margin-right: auto tags for each of these two blocks but that did nothing and they stayed firmly rooted to the left of the screen.

    I now have added these to <body> and put its width as 800px even though I want the grey background to fill all visible space, not just 800px wide. My current code then is as follows:

    Here is the HTML code:

    <title>Home Page</title>
    <link rel="stylesheet" href="style.css">
    <div id="header"></div>
    <div id="navigation"></div>
    Here is the CSS code:

    body 	{ background: grey;
               width: 800px;
    	   margin-left: auto;
               margin-right: auto }
    #header {position: absolute;
    	top: 10px;
    	width: 800px;
    	height: 100px;
    	background-image: url(header.jpg) }
    #navigation {	position: absolute;
    		top: 112px;
    		width: 700px;
    		height: 20px;
    		background-image: url(nav.jpg) }
    Now when viewed in Firefox 3, the #header is centrally aligned but the #navigation is not. Its left hand side is directly in line with the left hand side of the header but as it is only 700px and not 800px this means it is not truly centered, just directly below the header with a 100px gap on the right hand side. How can I correct this?!

    Even worse is when I open this page on IE7 it doesn't do anything at all - everything is just bang on the left side of the page. I've tried the text-align trick but that did nothing in Firefox and completely messed the page up in IE adding in a horizontal scroll bar for some reason!!

    Aaarghhhh!!! Any advice gladly welcomed!!

    Last edited by rookie156; 12-20-2008 at 07:27 AM.

  2. #2
    Join Date
    Jan 2004
    chertsey, a small town s.w. of london, england.
    Hi there rookie156,

    and a warm welcome to these forums.

    In 'Quirks Mode', IE does not recognize margin:auto.

    To put IE into 'Standards Compliance Mode' use a full dtd such as...
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"


  3. #3
    Join Date
    Dec 2008
    Hi coothead, thanks for the welcome and thanks for your help!

    I pasted that dtd (something else I'm entirely new to!) at the very top of my html document and it's definitely solved part of my problem. Using the CSS in my original post above, the header is now centrally aligned in both IE and Firefox but the Navigation box is not in either - it's directly below the Header but not centrally aligned.

    I have included a photo to make it easier to see what I'm probably not explaining very well!

    Any ideas how I overcome this?


  4. #4
    Join Date
    Jan 2004
    chertsey, a small town s.w. of london, england.
    Hi there rookie156,

    it is rarely necessary to use position:absolute for layout purposes and it's use can often cause problems.

    So I would suggest that you try it like this...
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <style type="text/css">
    body 	{ 
    #container {
    #header {
    #navigation {
        margin:2px auto;
    <div id="container">
    <div id="header"></div>
    <div id="navigation"></div>

  5. #5
    Join Date
    Dec 2008
    Coothead - thanks so much that worked perfectly!

    Will avoid the 'absolute' tag then, I didn't realise you could specify exactly where you wanted something without it.

    I'm sure I will be back on here soon, especially when I have to start putting divs in divs.

    Thanks again for your help!

  6. #6
    Join Date
    Jan 2004
    chertsey, a small town s.w. of london, england.
    No problem, you're very welcome.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.12496 seconds
  • Memory Usage 2,884KB
  • Queries Executed 13 (?)
More Information
Template Usage (33):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (3)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (6)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (6)postbit
  • (6)postbit_onlinestatus
  • (6)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates