Forum Moderators: not2easy

Message Too Old, No Replies

Simple Example of Border Weirdness

And sample, simple code!

         

SethCall

2:55 am on Jan 6, 2004 (gmt 0)

10+ Year Member



Situation: extremely simple example here: the code is below.
For some reason, if the commented code is uncommented in the code below ( be sure to comment the following line of code) , Mozilla displays the only div in this page one line (I believe) down below the top of the page. But, using the code exactly as below, it displays appropriately.

If anyone cares to explain this, I would be grateful. Im just confused :(


<?xml version="1.0" encoding="UTF-8"?>
<!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" xml:lang="en" lang="en">
<head>
<style>
html
{
width:100%;
height:100%;
}

body
{
position: absolute;
top: 0px;
bottom: 0px;
left: 0px;
right: 0px;
margin: 0px;
padding-left: 0px;
padding-right: 0px;
padding-top: 0px;
padding-bottom: 0px;
background-color:#FCC4FF;
color:black;
font-family: verdana,arial,sans-serif;
font-size:1em;
height:100%;
width:100%;
}

#header
{
background-color:#9CB0FB;
position: relative;
border:solid black;
/*border-width: 0px 0px 1px 0px; */
border-width: 1px 0px 1px 0px;
}
</style>
</head>
<body>
<div id="header">
<h1>Sample Text</h1></div>
</body>
</html>

pageoneresults

3:06 am on Jan 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



/*border-width: 0px 0px 1px 0px; */

Wild guess but, is it because there is no space between your first comment declaration?

/* border-width: 0px 0px 1px 0px; */

P.S. I may be misunderstanding the Q.

SethCall

3:33 am on Jan 6, 2004 (gmt 0)

10+ Year Member



oh, I tested it without the comment anyway. Its not the comment that is the question, I just put the offending code in the comment.

The code, as is, works well. The top of the page, starting at 0px, is that div, as expected.

But, if you use the commented code instead, the top div is shifted down a line. With only that small change? I tested all this without any commented code, so that shouldnt be the problem.

Thx so far.
Congrats on mod status: I just noticed PageOne : )

pageoneresults

3:49 am on Jan 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



body
{
margin:0;
padding:0;
background-color:#FCC4FF;
color:black;
font-family:verdana,arial,sans-serif;
font-size:1em;
height:100%;
width:100%;
}

Have you tried the process of elimination method? ;)

I think if you remove those first four attributes for absolute positioning, you may see different results. The margin:0; and padding:0; should give you your 0 margin that you are looking for.

<added> I'd also suggest using this format for your embedded style reference...

<style type="text/css"></style>

instead of <style></style>.

SethCall

4:31 am on Jan 6, 2004 (gmt 0)

10+ Year Member



ohh hehehe right right :) sorry, I was doing some real hack and slash to get the styling out of the external css file.

As to the removing some of the overkill in the body property: didnt help a bit...

<added>
I just noticed for the first time that the div in IE, when I use

border-width: 0px 0px 1px 0px;
, also grows smaller by one line.
</added>

Hester

9:17 am on Jan 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If a value is zero you don't need the pixel declaration. So "padding:0px" can be written as "padding:0".

SuzyUK

9:48 am on Jan 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Sethcall

it's Collapsing Margins [w3.org].

without the top border in place the top margins of the two elements (div and H1(default)) are collapsing (or overlapping, kinda describes it better) and you're seeing the larger of the two.. Then the Browser differences are because they are deciding which background to apply ~ Moz again (though I did doubt for a while this time) is right I think...

...If the element's margins are collapsed with its parent's top margin, the top border edge of the box is defined to be the same as the parent's....

One way to remove it safely cross browser would be to declare margin: 0; on the <h1> and set any spacing required with padding instead.
{I do this on hn and p elements anyway)
or:
use a 1px top border (same color as background if it's not actually required to show) as this may save further children causing the same problem

The border has the effect of seperating the elements so their margins are not touching and therefore wont collapse.

to test: set h1 {margin: 0;}

Suzy

<added> The difference in box model rendering is what is causing the different scenario in IE.. it never could get the margins right anyway ;), and that's why I mentioned the cross browser solutions...<added>

grahamstewart

2:37 pm on Jan 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As Suzy says it sounds like differences in the box model - caused because IE is operating in 'Quirks' mode.

a full doctype MUST be the first line in the file to make IE operate in 'Standards' mode. So you'll have to remove the

<?xml version="1.0" encoding="UTF-8"?>
declaration (I don't think its needed, but those with more experience with XHTML may contradict).

DrDoc

3:54 pm on Jan 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Besides, your CSS isn't valid as it is:
border:solid black;
border-width: 0px 0px 1px 0px;

The border shorthand must have (in this order): width, style, color

So, the proper way to write what you want would be:

border: 0 solid black;
border-bottom-width: 1px;

SethCall

10:23 pm on Jan 6, 2004 (gmt 0)

10+ Year Member



Hester: true
DrDoc: false -- see doc @ [w3.org...]

border:
[ <'border-width'> ¦¦ <'border-style'> ¦¦ <color> ] ¦ inherit

# A bar (¦) separates two or more alternatives: exactly one of them must occur.
# A double bar (¦¦) separates two or more options: one or more of them must occur, in any order.
# Brackets ([ ]) are for grouping.

grahamstewert: true

SuzyUK: oh my, thank you so much: that clearly is what seems to be happening.

Thank you, too: Hester, DrDoc, and Graham, for throwing the tips at me. And of course PageOneResults.

<added>
I never get to bust on DrDoc! =P. Suzy, you watch it. Your hegemony of perfection is coming to an end =p j/k guys/gals.
</added>

DrDoc

1:03 am on Jan 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



SethCall, you are obviously right.
I stand corrected :)

SuzyUK

9:46 am on Jan 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



SethCall

You're welcome and LOL ;)

Suzy