Vertically aligning a div inside another div

Associate
Joined
18 Nov 2008
Posts
2,430
Location
Liverpool
Hi there, from googling round this seems like an age old problem best solved by using tables, but in fear of missing something obvious I felt I should post here!

I'm trying to align a div in the exact centre of another div, both vertically and horizontally, I've achieved this horizontally but can't seem to get it to work vertically.

The CSS of the parent div is:
Code:
.contentbox
{
	background-image:url('/images/ContentBoxV1.jpg');
	height:200px;
	width:1024px;
}

While the CSS of the inner div is:
Code:
.innercontentbox
{
	background-image:url('/images/InnerContentBoxV2.jpg');
	height:170px;
	width:300px;
	margin-left:auto;
	margin-right:auto;
}

The live site is www.cypherhosting.co.uk , the relevant element is the not-so-centrally-aligned content box inside the bigger content box under the menu buttons.

Thanks guys!
 
Last edited:
Add margin-top:15px to the .innercontentbox styling.
Only problem is that it won't stay centered if the box grows.

I've been looking for a solution for a very similar problem I was having, but like you, I only found solutions involving table-cells :(
 
@Redgie
It's not aligning vertically in Internet Explorer but it is in Firefox.
I'm sure there are other ways, but adding what has been suggested will probably work.

Add margin-top:15px to the .innercontentbox styling.
Only problem is that it won't stay centered if the box grows.

I've been looking for a solution for a very similar problem I was having, but like you, I only found solutions involving table-cells :(

Is this what you mean?
When you add text and line breaks to innercontentbox, that grows bigger as does contentbox?

Code:
.contentbox {
	
	
	height:auto;
	width:1024px;
	background-color:#999;
	margin-left:auto;
	margin-right:auto;
	padding-bottom:15px;
	padding-top:15px;
	
}

Code:
.innercontentbox {
	
	height:auto;
	width:300px;
	margin-left:auto;
	margin-right:auto;
	background-color:black;
	color:#fff;
		
}
 
Add margin-top:15px to the .innercontentbox styling.
Only problem is that it won't stay centered if the box grows.

I've been looking for a solution for a very similar problem I was having, but like you, I only found solutions involving table-cells :(

Unfortunately that pushes the parent div up with it, which is not what I'm going for :(
 
That somewhat worked, but meant that it took away the previously working horizontal centering and means that when the page expands vertically the elements move out of the center.
 
Why the center and not just pushed 15px down?

Also, is there any particular reason you're using 'onclick' for your navigation tabs (which will fail if Javascript is disabled)? You can just wrap the div's in normal <a> tags instead.
 
Last edited:
That was just done for simplicity, it's only a concept site at the mo :)

If I push it down by 15px it still has the same problem, by saying that I'm trying to center it, I don't mean center it via centering code, I mean center it by dropping it down by 15px without pushing anything else away, which margins and padding both do.
 
Code:
.contentbox {position: relative;}

.innercontentbox {position: absolute; left: 50%; top: 50%; margin: -100px 0 0 -100px; height: 200px; width: 200px}

The negative margins need to be half the width/height that you have defined. This will position the innercontentbox to the absolute centre of the contentbox container
 
To be honest, I have no idea what you're trying to achieve here. Do you have a design mockup of what the site's supposed to look like when finished?

Not really, or not one outside of my head anyway :p

The box is aligned centrally now (Thanks guys :) ) but because of the use of relative and absolute positioning, adding them side by side doesn't work particularly well, I'd rather do it without any form of positioning, and so see the use of tables as perhaps my only option :(
 
display: block;
margin-left: auto;
margin-right:auto;
position: relative;
top: Xpx;

Does that help at all?

Thank you, this did it :) Along with the width and height declarations (Otherwise it didn't quite fit).

Thank you very much, and to everybody else who helped!
 
Back
Top Bottom