...

View Full Version : Centering Layers with CSS



holbrookphotos
02-19-2009, 10:24 PM
So. For the past 72 hours I have been relentlessly trying to solve this issue. It seems fairly simple, and in theory it should work. But theory is currently shafting me.

I have two layers, both in html. One is a simple background image while the other is a gathering of transparent slices to be used as links. Now, I know you may be asking why I have the two separate, but I want to have the image load cleaner than it has been and would like to have the links load invisibly.

I have tried many ways of centering the layers including <center>, <absmiddle>, 50% margins, etc.....and it still doesn't work! Its slightly infuriating. My last resort has been to work in out in CSS. Now I am in no way experienced, so I've been fishing around in forums for spare code in a vain attempt to piece together something that resembles code. It hasnt worked. PLease. If you have any ideas, it would be AMAZING!

-Phil

BoldUlysses
02-19-2009, 10:31 PM
Hi Phil,


My last resort has been to work in out in CSS.

CSS should be your first resort when it comes to layout issues. :) Seriously.

You should know that "layers" are an artificial construct used by graphics programs and WYSIWYG programs that create HTML code and really don't have any relevance to actual code. In other words, there's no <layer> tag.

That said, what do you have so far? There are a couple of ways of centering sites using CSS, all of them quite simple and easy. A link to a page, all the HTML and CSS you have so far, or even a screen capture would be helpful.

Excavator
02-20-2009, 01:03 AM
Hello holbrookphotos,
The very basics of centering is

DocType
width
margin:auto;


Like this example where you see the div #wrap is centered:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
html, body {
background: #FC6;
}
#wrap {
width: 800px;
height: 600px;
margin: 30px auto;
background: #999;
}
</style>
</head>
<body>
<div id="wrap">
<!--end wrap--></div>
</body>
</html>

holbrookphotos
02-20-2009, 08:25 AM
SO! I know that there is a lot here, and I am really fishing in the dark, so it most likely is jumbled and garbled....sorry about that. I have the css sheet first and the html doc afterwards. The most important thing to me is having the layers, so if thats possible to keep in, thatd be great. Again, sorry for the mass amount of text. In the HTML file you will see links to tons of graphics, those are all my slices so that I can link to other pages on my site. Thanks in advance, this is a huge help!



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Centered Element of unknown height and width</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="style.css" rel="stylesheet" type="text/css" /></head>

body{

text-align:center;
}


#Layer1 {
position: absolute;
top: 0;
left: 50%;
margin-top: 0;
margin: 0 auto;
z-index: 1;
}

#Layer2 {
position: absolute;
top: 0;
left: 50%;
margin-top: 0;
margin: 0 auto;
z-index: 2;
}

#Layer3 {
position:absolute;
top: 0px;
left: 50%;
right: 50%;
position: absolute;
z-index: 1;
}

#Layer4 {
position:absolute;
top: 0px;
left: 0%;
right: 50%;
position: absolute;
z-index: 2;
}

</body>
</html>




HTML!!!::::




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Center Test 2</title>

<link rel=stylesheet type="text/css" href="../style.css">

</head>
<body bgcolor="#000000" leftmargin="auto" rightmargin="auto" topmargin="0" bottommargin="0" marginwidth="0" marginheight="0">

<div id="top" style="left: 500px; top: 485px; position: absolute">

<table id="Table_01" table class="center" width="1025" height="769" border="0" cellpadding="0" cellspacing="0">

<tr>
<td colspan="11">
<img src="images/Head_slices_01.gif" alt="" width="1024" height="37" align="default"></td>
<td>
<img src="images/spacer.gif" alt="" width="1" height="37" align="default"></td>
</tr>
<tr>
<td rowspan="14">
<img src="images/Head_slices_02.gif" alt="" width="74" height="731" align="default"></td>
<td colspan="9" align="default" valign="top">
<img src="images/Head_slices_03.gif" alt="" width="877" height="74" align="default"></td>
<td rowspan="14">
<img src="images/Head_slices_04.gif" alt="" width="73" height="731" align="default"></td>
<td>
<img src="images/spacer.gif" alt="" width="1" height="74" align="default"></td>
</tr>
<tr>
<td colspan="9">
<img src="images/Head_slices_05.gif" alt="" width="877" height="122" align="default"></td>
<td>
<img src="images/spacer.gif" alt="" width="1" height="122" align="default"></td>
</tr>
<tr>
<td rowspan="12">
<img src="images/Head_slices_06.gif" alt="" width="36" height="535" align="default"></td>
<td align="default" valign="top">
<img src="images/Head_slices_07.gif" alt="" width="193" height="91" align="default"></td>
<td colspan="7" rowspan="2">
<img src="images/Head_slices_08.gif" alt="" width="648" height="95" align="default"></td>
<td>
<img src="images/spacer.gif" alt="" width="1" height="91" align="default"></td>
</tr>
<tr>
<td>
<img src="images/Head_slices_09.gif" alt="" width="193" height="4" align="default"></td>
<td>
<img src="images/spacer.gif" alt="" width="1" height="4" align="default"></td>
</tr>
<tr>
<td colspan="2" align="default" valign="top">
<img src="images/Head_slices_10.gif" alt="" width="195" height="89" align="default"></td>
<td colspan="6" rowspan="2">
<img src="images/Head_slices_11.gif" alt="" width="646" height="93" align="default"></td>
<td>
<img src="images/spacer.gif" alt="" width="1" height="89" align="default"></td>
</tr>
<tr>
<td colspan="2">
<img src="images/Head_slices_12.gif" alt="" width="195" height="4" align="default"></td>
<td>
<img src="images/spacer.gif" alt="" width="1" height="4" align="default"></td>
</tr>
<tr>
<td colspan="3" align="default" valign="top">
<img src="images/Head_slices_13.gif" alt="" width="196" height="88" align="default"></td>
<td colspan="5" rowspan="4">
<img src="images/Head_slices_14.gif" alt="" width="645" height="197" align="default"></td>
<td>
<img src="images/spacer.gif" alt="" width="1" height="88" align="default"></td>
</tr>
<tr>
<td colspan="3">
<img src="images/Head_slices_15.gif" alt="" width="196" height="6" align="default"></td>
<td>
<img src="images/spacer.gif" alt="" width="1" height="6" align="default"></td>
</tr>
<tr>
<td colspan="3" align="default" valign="top">
<img src="images/Head_slices_16.gif" alt="" width="196" height="88" align="default"></td>
<td>
<img src="images/spacer.gif" alt="" width="1" height="88" align="default"></td>
</tr>
<tr>
<td colspan="3" rowspan="5">
<img src="images/Head_slices_17.gif" alt="" width="196" height="165" align="default"></td>
<td>
<img src="images/spacer.gif" alt="" width="1" height="15" align="default"></td>
</tr>
<tr>
<td rowspan="4">
<img src="images/Head_slices_18.gif" alt="" width="144" height="150" align="default"></td>
<td colspan="3" align="default" valign="top">
<img src="images/Head_slices_19.gif" alt="" width="129" height="37" align="default"></td>
<td rowspan="4">
<img src="images/Head_slices_20.gif" alt="" width="372" height="150" align="default"></td>
<td>
<img src="images/spacer.gif" alt="" width="1" height="37" align="default"></td>
</tr>
<tr>
<td colspan="3">
<img src="images/Head_slices_21.gif" alt="" width="129" height="12" align="default"></td>
<td>
<img src="images/spacer.gif" alt="" width="1" height="12" align="default"></td>
</tr>
<tr>
<td rowspan="2">
<img src="images/Head_slices_22.gif" alt="" width="22" height="101" align="default"></td>
<td align="default" valign="top">
<img src="images/Head_slices_23.gif" alt="" width="87" height="64" align="default"></td>
<td rowspan="2">
<img src="images/Head_slices_24.gif" alt="" width="20" height="101" align="default"></td>
<td>
<img src="images/spacer.gif" alt="" width="1" height="64" align="default"></td>
</tr>
<tr>
<td>
<img src="images/Head_slices_25.gif" alt="" width="87" height="37" align="default"></td>
<td>
<img src="images/spacer.gif" alt="" width="1" height="37" align="default"></td>
</tr>
<tr>
<td>
<img src="images/spacer.gif" alt="" width="74" height="1" align="default"></td>
<td>
<img src="images/spacer.gif" alt="" width="36" height="1" align="default"></td>
<td>
<img src="images/spacer.gif" alt="" width="193" height="1" align="default"></td>
<td>
<img src="images/spacer.gif" alt="" width="2" height="1" align="default"></td>
<td>
<img src="images/spacer.gif" alt="" width="1" height="1" align="default"></td>
<td>
<img src="images/spacer.gif" alt="" width="144" height="1" align="default"></td>
<td>
<img src="images/spacer.gif" alt="" width="22" height="1" align="default"></td>
<td>
<img src="images/spacer.gif" alt="" width="87" height="1" align="default"></td>
<td>
<img src="images/spacer.gif" alt="" width="20" height="1" align="default"></td>
<td>
<img src="images/spacer.gif" alt="" width="372" height="1" align="default"></td>
<td>
<img src="images/spacer.gif" alt="" width="73" height="1" align="default"></td>
<td></td>
</tr>
</table></div>

<div id="Layer2"><img src="OVERLAY TEST.jpg"></div>

</body>
</html>

jerry62704
02-20-2009, 04:21 PM
Hi Phil,
You should know that "layers" are an artificial construct used by graphics programs and WYSIWYG programs that create HTML code and really don't have any relevance to actual code. In other words, there's no <layer> tag.


You sure about that? Didn't Netscape have <layer> as opposed to <div> back in the mid-90s?

BoldUlysses
02-20-2009, 04:43 PM
You sure about that? Didn't Netscape have <layer> as opposed to <div> back in the mid-90s?

Perhapsibly. But that part of "web history" (http://en.wikipedia.org/wiki/Browser_wars#The_first_browser_war) is thankfully over. :)

jerry62704
02-20-2009, 04:55 PM
I ignored css for a decade because of it. I just couldn't see every command being duplicated for both browsers. What a nightmare.

BoldUlysses
02-20-2009, 05:10 PM
Phil: Let's start over. It's much easier than you think. Two points to remember:

1. No self-respecting web designer uses tables for layout (http://phrogz.net/css/WhyTablesAreBadForLayout.html) any more.
2. You don't have to slice up your images if you lay out your page with CSS. It's a beautiful, wonderful thing.

Excavator demonstrated the preferred method for centering. As far as the rest of your code, ditch the tables, read up on semantic code (http://www.boagworld.com/technology/semantic_code_what_why_how/) and learn how to lay out your page using only CSS (http://www.w3schools.com/).

If you'd like more specific help here, post a link to your page and we can help you rework it. It's difficult/impossible to do without actually being able to see your images and thus deduce your design intent.

abduraooft
02-20-2009, 05:14 PM
Excavator demonstrated the preferred method for centering. As far as the rest of your code, ditch the tables, read up on semantic code and learn how to lay out your page using only CSS. ... and get inspired by seeing the beauty and flexibility of CSS based design : http://www.csszengarden.com/



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum