Log in

View Full Version : Centre Aligning a Div



Richard Cousins
10-31-2012, 11:22 AM
Im currently making a website for the design studio I work for and its not far from completion. The one problem I am having is centreing the links at the very top. All these links are enclosed within one main DIV so all I want to do is align this div so it sits right in the middle of the users screen. Now it seem to do this horizontally but it wont do it vertically.

Could anyone please look at the code and help point out where I am going wrong.

The sit is currently hosted here: www.stormdfx.com/stormdfx


The DIV I want to centre is called storm-home



Any help would be much appriciated.

Thanks,

Rich

Richard Cousins
10-31-2012, 04:48 PM
anyone able to help me? :(

Beverleyh
10-31-2012, 06:55 PM
I can't check from iPhone but maybe its because you've not set the left and right margins to "auto"?

See this for ref: http://www.w3schools.com/css/css_align.asp

Richard Cousins
11-01-2012, 08:56 AM
Ive added margin-left and margin-right auto and it hasn't made it float in the centre. I've got top set to 50%.

Not sure what the problem is.

Richard Cousins
11-01-2012, 08:57 AM
Centre aligning horizontally isnt the problem really its getting it to float in the centre vertically.

bernie1227
11-01-2012, 10:28 AM
css vertical align? (http://www.w3schools.com/cssref/pr_pos_vertical-align.asp)

Richard Cousins
11-01-2012, 10:36 AM
Where do i place that in the code, im not sure?

Richard Cousins
11-01-2012, 10:39 AM
I've tried it in a few locations and it doesn't seem to be working :S

Richard Cousins
11-01-2012, 10:42 AM
this is the code for the part i want to centre:


<div id="content">
<article id="storm-home">
<div><a href="#storm-dfx" class="home-create"> </a></div>
<div id="text-box1"><img src="img/text-block1.png" alt="text" width="940" height="25" /></div>
<div><a href="#service" class="home-create1"> </a></div>
<div id="text-box2"><img src="img/text-block2.png" alt="text" width="940" height="19" /></div>
<div><a href="#finger-pulse" class="home-create2"> </a></div>
<div id="text-box33"><img src="img/text-block3.png" alt="text" width="940" height="30" /></div>
<div><a href="#award-winning" class="home-create3"> </a></div>
<div id="text-box4"><img src="img/text-block4.png" alt="text" width="940" height="16" /></div>
<div><a href="#repack-service" class="home-create4"> </a></div>
<div id="text-box5"><img src="img/text-block5.png" alt="text" width="940" height="28" /></div>
<div><a href="#drop-line" class="home-create5"> </a></div>
<div>
<div id="text-box6"><img src="img/text-block6.png" alt="text" width="146" height="47" /></div>
<div><a href="#your-brand" class="home-create6"> </a></div>
</div>



This is the CSS for the storm-home id:


#content article { width: 940px; height: 550px;}
#storm-home,
#storm-dfx,
#service,
#finger-pulse,
#award-winning,
#repack-service,
#drop-line,
#your-brand{padding-top: 10px;}
#storm-home {position: absolute; top: 50%; margin-left:auto; margin-right:auto; vertical-align:text-top;}
#storm-dfx {position: absolute; top: 1090px;}
#service {position: absolute; top: 2180px;}
#content h1 {margin: 0 0 25px 0; font-size: 60px; font-family: arial; font-weight: normal; line-height: 65px;}
#finger-pulse {position: absolute; top: 3270px;}
#award-winning {position: absolute; top: 4360px;}
#repack-service {position: absolute; top: 5450px;}
#drop-line {position: absolute; top: 6540px;}
#your-brand {position: absolute; top: 7630px;}

Richard Cousins
11-01-2012, 10:57 AM
OK Ive tried adding this css to a new div and then wrapping that div around the content and it doesnt work. Now if I open a statr a new html document with nothing in it it seem works. So Im guessing some cod I already have isnt allowing it to align vertically in the centre though I dont know which bit of code :-(


#div-new{
position: absolute;
background: #000;
top: 50%;
left: 0;
width: 100%;
}

Beverleyh
11-01-2012, 12:27 PM
Maybe one of these methods will work? http://blog.themeforest.net/tutorials/vertical-centering-with-css/

Richard Cousins
11-05-2012, 09:28 AM
I cant seem to make these work. Maybe I'm doing it wrong. Has anyone been able to look at my code?

Beverleyh
11-05-2012, 07:22 PM
I'm not sure but I think the issue is with you having all the content in one long page of chained divs.

Normally when you vertically align body content with CSS you can set the central div to a top position of 50% (of the visible screen body) but then offset it with a negative top margin that is half the height of the central div, thus pulling the central div back up to a seemingly central position on any viewing screen.

This I don't think will work here because the body contains all if your pages in one long chain of divs.

I think maybe you'll need to take a different approach, possibly with JavaScript to identify the height of the viewport (the viewing browser height) and setting that dynamically to be the height of the containing div. From there I imagine that you could then give the link container a top position of 50% with the margin-top negative offset that is half the height of the link container.

I haven't explored any further but hopefully one of these routes will start you off http://www.google.com/m?q=jQuery%20get%20viewport%20height%20to%20vertically%20align%20div

Richard Cousins
11-07-2012, 03:22 PM
Thank you Bereleyh. I'm looking into it now.

One thing I noticed that I might be doing wrong is I was trying to make a div that was already within a div go into the centre of the page. Now I am trying to centre all of the content. For my site which is a single page everything is contained within #wrapper. So what I have done is this:

#wrapper {position: absolute; width:940px; height:8800px; margin:0 auto; left:50%; top:10%; margin-left:-470px;}

Now if I add margin-top:-4400px all it does it lift all off the content up and off of the screen. The top: ""% works but I cant centre it perfectly. Any ideas which doing it this way it wrong?

Beverleyh
11-07-2012, 04:48 PM
You're getting into a right pickle, aren't you ;)

OK looking at the code from here: http://stackoverflow.com/questions/6564752/set-div-height-dynamically-based-on-viewport-height let's borrow the jist of that function and do this...

At the bottom of your page, insert the red code into your script tags;


<script type="text/javascript">
function vp_height() {
var height = $(window).height();
$("#storm-home").css('height', height+'px');
}
$(document).ready(function() {
vp_height();
$(window).bind('resize', vp_height);
$.piroBox_ext({
piro_speed :700,
bg_alpha : 0.5,
piro_scroll : true,
piro_drag :false,
piro_nav_pos: 'bottom'
});
});
</script>

Now, in your HTML higher up the page, wrap another div inside #storm-home;


<article id="storm-home">
<div id="centerme" style="height:600px; position:relative; top:50%; margin-top:-300px">
<div><a href="#storm-dfx" class="home-create"> </a></div>
<div id="text-box1"><img src="http://www.stormdfx.com/stormdfx/img/text-block1.png" alt="text" width="940" height="25" /></div>
<div><a href="#service" class="home-create1"> </a></div>
<div id="text-box2"><img src="http://www.stormdfx.com/stormdfx/img/text-block2.png" alt="text" width="940" height="19" /></div>
<div><a href="#finger-pulse" class="home-create2"> </a></div>
<div id="text-box33"><img src="http://www.stormdfx.com/stormdfx/img/text-block3.png" alt="text" width="940" height="30" /></div>
<div><a href="#award-winning" class="home-create3"> </a></div>
<div id="text-box4"><img src="http://www.stormdfx.com/stormdfx/img/text-block4.png" alt="text" width="940" height="16" /></div>
<div><a href="#repack-service" class="home-create4"> </a></div>
<div id="text-box5"><img src="http://www.stormdfx.com/stormdfx/img/text-block5.png" alt="text" width="940" height="28" /></div>
<div><a href="#drop-line" class="home-create5"> </a></div>
<div>
<div id="text-box6"><img src="http://www.stormdfx.com/stormdfx/img/text-block6.png" alt="text" width="146" height="47" /></div>
<div><a href="#your-brand" class="home-create6"> </a></div>
</div>
<nav class="next-prev" style="margin-top:90px">
<hr /><a class="next storm-dfx" href="#storm-dfx">Next</a>
</nav>
</div>
I called it "centerme" and estimated the overall height of your existing menu at being 600px to allow me to set the -300px negative offset.

Notice how I also plucked out your <nav class="next-prev"> + inner <hr> and put them inside the "storm-home" div otherwise it will go floating off into the middle of the screen. I also just added a top margin of 90px to bring it back to roundabout the same position as it previously was, but you can mess with exact pixels to suit.

If, all goes well, you'll get this: http://jemthingsandstuff.co.uk/testing/center-div-in-viewport.html

Is this something you can work with?

BTW - I used to live in Alfreton (well, Riddings)

Beverleyh
11-07-2012, 05:00 PM
ps - I just changed my example to
<div id="centerme" style="height:600px; position:relative; top:50%; margin-top:-320px">instead of -300px, as I think it looks a bit more central that way.

Beverleyh
11-08-2012, 01:35 PM
Just out of curiosity, did Stormdfx used to be called Lynx? - theres a picture with Lynx on in your site.

If I was, I went for an interview there about 8 years ago - ha, ha

Richard Cousins
11-13-2012, 12:47 PM
Hi Beverleyh.

Thank you for the examples. Ive been holiday so today is the first chance Ive got to implement it. It looks awesome :-)

Many thanks.

We used to be called DesignFX but now we are called StormDFX. Im hoping to have the site fully complete and live by the end of the week :-)

Beverleyh
11-13-2012, 01:17 PM
Good stuff. Hope it works out as planned.

Richard Cousins
11-13-2012, 03:22 PM
Ive copied the cod but it doesn't seem to be centring the element :S

Beverleyh
11-13-2012, 03:25 PM
In the http://www.stormdfx.com/stormdfx/ page?

I cant see your changes in the source code - have you uploaded them?

Richard Cousins
11-13-2012, 03:29 PM
Ive copied the code from the live page and I get the same error. The element ends up lower down. Does the page need to be uploaded to work properly?

Richard Cousins
11-13-2012, 03:32 PM
Yeah that sorted it :-)

Richard Cousins
11-13-2012, 05:32 PM
Hi Beverleyh,

It seems to work in Firefox but when I view the site in Chrome, Opera and IE it goes to the bottom of the page. Please see http://www.stormdfx.com/stormdfx/

I don't know if its viewing a cached version but it doesn't seem to work :(

EDIT:

Also now when I view your example the element is dropping down to the bottom of the screen and not floating in the centre. I'm not sure what's going on.

Beverleyh
11-13-2012, 10:07 PM
Hmmmm, you must have changed something in your CSS file because my sample page (which is linked to the CSS/js files on your server) now no longer behaves as it did.

When I posted the other day, it was centered in all major browsers, but now it isnt.

Retrace your steps and if you can re-upload backups to get it back to how it was. Then make small changes til you find what caused the glitch.

Richard Cousins
11-14-2012, 09:11 AM
Fixed it :-)

for some reason these 2 lines of code were missing


#branding {width: 100%; background: #fff;}
#branding h1 {width: 940px; padding: 10px 15px; margin: 0 auto; text-transform: uppercase; font-size: 18px; font-weight: bold;}