PDA

View Full Version : can somebody explain the logic in this css bug/feature


deadseasquirrel
04-14-2004, 03:55 AM
I love CSS in someways, and in some ways it is driving me crazy because it appears to have no rhyme or reason.

First I had my doctype set as:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

And nothing seem to work. Then somebody told me to use another doctype, exactly why, I don't know, but it did the trick. It was as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

From what I understand of CSS, now is that for position (which is why a lot of people want to use it and get rid of tables) the following are the definitions.

relative - relative to where it would have been put otherwise
absolute - the absolute position from 0,0 (which is the upper left-hand corner of the most recently positioned item.
static - is just like default.

Like everybody, I am trying to do a column effect without messing with too much margin/padding hacking. I have my columns set as follows:

#mainpage {
width: 750px;
border: 1px solid #FF0000;
background-color: #eee;
position: static;
}

#mainpage #logoleft {
background-image: url(../images/pedialogo1.jpg);
background-repeat: no-repeat;
position: absolute;
height: 50px;
width: 250px;
}

#mainpage #logoright {
background-image: url(../images/pedialogo.jpg);
background-repeat: no-repeat;
position: absolute;
height: 50px;
width: 220px;
left: 300px;

}


I would think that this meant that the mainpage window would constantly center as the browser window is resized (which it does), and that the leftlogo item would always be on the left-hand margin of the mainpage window, and that logoright would always be on the right-hand margin of the mainpage. But instead what happens is, logoright is set to 300px from the left hand of the browser, NOT mainpage.

But then when I set mainpage to be positioned relative, things work as I wanted. Why is that. What is the rhyme and reason for that, what are the specs once and for all, and what type of doctype do I need to use to get those specs to happen. It is so frustrating, please somebody help a person who desparately want to get a solid handle on CSS.

kansel
04-14-2004, 05:01 AM
This book (http://www.amazon.com/exec/obidos/tg/detail/-/159059231X/qid=1081914642/sr=8-2/ref=sr_8_xs_ap_i2_xgl14/103-5494741-5713458?v=glance&s=books&n=507846) Has an excellent chapter on the CSS box-model and lots of useful info on other aspects of CSS (including what bugs are in which browsers and how to get around them). I was in about the same position you are before reading it. You probably don't need to run out and by this book as you'll eventually get the same info out of this forum, but it's handy to have around.

The longer doctype (with the dtd url) is the correct one. The other is incomplete and puts the browser into quirks mode. (Unfortunately it's the one used by default in Dreamweaver and Homesite and possibly other editors.)

Here are two more things to remember about absolute vs. relative position:
Absolute positioned elements are removed from document flow - that is an absolute positioned element will not affect the positioning of elements below it in the source code.
Relative positioned elements are not removed from document flow (and a "hole" is left behind in the original position).
An element with a position other than static (absolute, relative, fixed) will become the origin for its child elements.

I hope this info helps a little bit. I'm going to submit this and then take a few minutes to look at your code to see if I can be of further assistance.

kansel
04-14-2004, 05:16 AM
Ok, I notice in your original code that #mainpage is position: static. Note that in my above post, an element with position other than (that is, not) static, will become origin for its children. This is why setting #mainpage to position relative makes it work.

If you want the #logoright to hang off the right margin of #mainpage, you might use position:absolute; right:-220px; This will attach the #logoright to the right edge of #mainpage, which means if you change the width of #mainpage, you won't have to recalculate the position of #logoright.

If you still have trouble, post a URL or more complete code and we'll get you set up.

kansel
04-14-2004, 05:23 AM
For more info (and maybe some ready-to-use layouts), check out this thread (http://codingforums.com/showthread.php?t=20862).

Most of those links are CSS reference. Under the Various Resources heading you'll find links to Brainjar, Glish and Bluerobot - these are some great examples of column layout with CSS.

deadseasquirrel
04-14-2004, 11:53 AM
Kansel, thanks for a very thorough answer. Do you have a definitive CSS book that you would suggest buying?

kansel
04-14-2004, 08:01 PM
I posted a link to it in the first response. Cascading Style Sheets: Separating Content from Presentation (http://www.amazon.com/exec/obidos/tg/detail/-/159059231X/qid=1081968782/sr=8-1/ref=sr_8_xs_ap_i1_xgl14/104-9052379-6991929?v=glance&s=books&n=507846). It's currently in 2nd edition. The 1st edition is a good read through with great examples and theory, but lacks reference section for specific style attributes. I don't know if they corrected this in the 2nd edition.

For pure reference I'd recommend the 2nd edition of O'Reilly's Cascading Style Sheets: the Definitive Guide (http://www.amazon.com/exec/obidos/tg/detail/-/0596005253/qid=1081968958/sr=1-1/ref=sr_1_1_xs_stripbooks_i1_xgl14/104-9052379-6991929?v=glance&s=books).

Although I'm a little book crazy... you can really get all the info you need from the links posted in this forum. (See the CSS Documentation sticky at the top of this forum)

coder_seth
04-15-2004, 06:49 PM
to be more specific, that DOCTYPE didn't work because you'll notice it lacked a link to the actual DOCTTYPE Definition file.. sort of important!

deadseasquirrel
04-16-2004, 04:56 AM
See that's the thing though, even though as you pointed my first doctype was really screwy it still worked. So that makes it really hard to learn something right, when things work whether you do it right or not. I guess that is one of my big gripes with web design right now and css also. A lot of just weirdness going on, I'm a stickler for rules and order. I am sure everybody here (who is a programmer) is also.

coder_seth
04-16-2004, 06:49 PM
no.. see, you are pointing fingers in the wrong directions. the DOCTYPE in the first case, with no reference to the DTD file.. DID NOT work. what happened, as i understand things, is that the BROWSER then referred to it's own version of the DOCTYPE. now, why wouldn't it be identical you ask? maybe it is, in which case it would appear as if it worked even with a faulty DOCTYPE. but it's still just smart to include the proper DOCTYPE which refers to the DTD that the W3 put together.

and in most cases, the seemingly random nature of CSS and HTML isn't random at all. it's usually a combination of user error and browsers that all interpret the specs in different ways.

don't get me wrong, i understnad the frustration completely. just never hurts to be clear on what's really going on though. especially when it then enables you to work around such things and experience LESS FRUSTRATION. :O!!!