Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 11th, 2008, 05:35 PM
Ian Hobson
Guest
 
Posts: n/a
Default CSS not doing what I expect - why?

Hi all,

If you look at these two pages..

http://homepage.ntlworld.com/ian.hob...mple/ex4a.html and

http://homepage.ntlworld.com/ian.hob...mple/ex4b.html

The only difference is that on ex4a.html the #main has a 1px border top
and bottom.

Can some kind person please explain to me why, when this is removed, the
white bands appear as in ex4b.html?

Other testing has shown that if I kill the top and bottom margins on
#main p, this will also fix the problem, but makes the layout of the
#main text look terrible. This would imply that it is something to do
with the margins on the P "sticking out" of the div, and pushing the
header and footer away. Should they do that?

Safari, Firefox 2 and 3, and Opera all handle the page the same way, so I
rather doubt this is a bug.

Thanks for your input.

Ian



  #2  
Old August 11th, 2008, 07:05 PM
Harlan Messinger
Guest
 
Posts: n/a
Default Re: CSS not doing what I expect - why?

Ian Hobson wrote:
Quote:
Hi all,
>
If you look at these two pages..
>
http://homepage.ntlworld.com/ian.hob...mple/ex4a.html and
>
http://homepage.ntlworld.com/ian.hob...mple/ex4b.html
>
The only difference is that on ex4a.html the #main has a 1px border top
and bottom.
>
Can some kind person please explain to me why, when this is removed, the
white bands appear as in ex4b.html?
Because the first paragraph inside it has a top margin and the last
paragraph has a bottom margin, from line 26 in reset.css.
Quote:
>
Other testing has shown that if I kill the top and bottom margins on
#main p, this will also fix the problem, but makes the layout of the
#main text look terrible. This would imply that it is something to do
with the margins on the P "sticking out" of the div, and pushing the
header and footer away. Should they do that?
Yes, so if the first paragraph shouldn't have a top margin and the last
paragraph shouldn't have a bottom margin, you should set them so, either
by assigning each of them a class or by using the pseudo-classes
:first-child and :last-child. The latter would be the better choice if
not for the fact that even version 7 of IE doesn't recognize last-child
(though it does recognize first-child).

  #3  
Old August 11th, 2008, 07:15 PM
Harlan Messinger
Guest
 
Posts: n/a
Default Actually: Re: CSS not doing what I expect - why?

Harlan Messinger wrote:
Quote:
Ian Hobson wrote:
Quote:
>Hi all,
>If you look at these two pages..
>>
>http://homepage.ntlworld.com/ian.hob...mple/ex4a.html and
>>
>http://homepage.ntlworld.com/ian.hob...mple/ex4b.html
>>
>The only difference is that on ex4a.html the #main has a 1px border
>top and bottom.
>Can some kind person please explain to me why, when this is removed,
>the white bands appear as in ex4b.html?
>
Because the first paragraph inside it has a top margin and the last
paragraph has a bottom margin, from line 26 in reset.css.
>
Quote:
>>
>Other testing has shown that if I kill the top and bottom margins on
>#main p, this will also fix the problem, but makes the layout of the
>#main text look terrible. This would imply that it is something to do
>with the margins on the P "sticking out" of the div, and pushing the
>header and footer away. Should they do that?
>
Yes, so if the first paragraph shouldn't have a top margin and the last
paragraph shouldn't have a bottom margin, you should set them so, either
by assigning each of them a class or by using the pseudo-classes
:first-child and :last-child. The latter would be the better choice if
not for the fact that even version 7 of IE doesn't recognize last-child
(though it does recognize first-child).
>
Actually, you can also fix the appearance by giving div#main the same
top and bottom padding of 0.3em so that the first and last paragraph's
outer vertical margins will be absorbed within the main div and not
stick out as they do now.
  #4  
Old August 11th, 2008, 10:15 PM
Ian Hobson
Guest
 
Posts: n/a
Default Re: Actually: Re: CSS not doing what I expect - why?

On Mon, 11 Aug 2008 14:06:14 -0400, Harlan Messinger wrote:
Quote:
Harlan Messinger wrote:
[snip]
Quote:
Actually, you can also fix the appearance by giving div#main the same
top and bottom padding of 0.3em so that the first and last paragraph's
outer vertical margins will be absorbed within the main div and not
stick out as they do now.
Thanks Harlan,

I like that solution. It fits with the careful work getting H1 to H4 to
fit in properly also.

Will have to re-read up about margins and padding.

Thanks again.

Ian

  #5  
Old August 11th, 2008, 10:25 PM
Harlan Messinger
Guest
 
Posts: n/a
Default Re: Actually: Re: CSS not doing what I expect - why?

Ian Hobson wrote:
Quote:
On Mon, 11 Aug 2008 14:06:14 -0400, Harlan Messinger wrote:
>
Quote:
>Harlan Messinger wrote:
[snip]
Quote:
>Actually, you can also fix the appearance by giving div#main the same
>top and bottom padding of 0.3em so that the first and last paragraph's
>outer vertical margins will be absorbed within the main div and not
>stick out as they do now.
>
Thanks Harlan,
>
I like that solution. It fits with the careful work getting H1 to H4 to
fit in properly also.
>
Will have to re-read up about margins and padding.
>
Thanks again.
You're welcome. Also, I forgot to mention one other thing: IIRC, a
border on a block box prevents a margin from one of its children from
stretching beyond it. So when you had a border, it constrained the
paragraphs' margins. That's why the problem only occurred when you
removed the border.

In fact, a hack you can use is to set a 1px border in the same color as
the background.
  #6  
Old August 11th, 2008, 11:25 PM
Gus Richter
Guest
 
Posts: n/a
Default Re: Actually: Re: CSS not doing what I expect - why?

Ian Hobson wrote:
Quote:
On Mon, 11 Aug 2008 14:06:14 -0400, Harlan Messinger wrote:
>
Quote:
>Harlan Messinger wrote:
[snip]
Quote:
>Actually, you can also fix the appearance by giving div#main the same
>top and bottom padding of 0.3em so that the first and last paragraph's
>outer vertical margins will be absorbed within the main div and not
>stick out as they do now.
>
Thanks Harlan,
>
I like that solution. It fits with the careful work getting H1 to H4 to
fit in properly also.
>
Will have to re-read up about margins and padding.
Also read in the Specifications regarding "collapsing margins" because
that is the reason for what you have encountered:

<http://www.w3.org/TR/CSS21/box.html#collapsing-margins>
<http://www.w3.org/TR/CSS21/box.html#mpb-examples>

--
Gus
  #7  
Old August 12th, 2008, 07:05 AM
Ben C
Guest
 
Posts: n/a
Default Re: Actually: Re: CSS not doing what I expect - why?

On 2008-08-11, Harlan Messinger <hmessinger.removethis@comcast.netwrote:
Quote:
Ian Hobson wrote:
Quote:
>On Mon, 11 Aug 2008 14:06:14 -0400, Harlan Messinger wrote:
>>
Quote:
>>Harlan Messinger wrote:
>[snip]
Quote:
>>Actually, you can also fix the appearance by giving div#main the same
>>top and bottom padding of 0.3em so that the first and last paragraph's
>>outer vertical margins will be absorbed within the main div and not
>>stick out as they do now.
>>
>Thanks Harlan,
>>
>I like that solution. It fits with the careful work getting H1 to H4 to
>fit in properly also.
>>
>Will have to re-read up about margins and padding.
>>
>Thanks again.
>
You're welcome. Also, I forgot to mention one other thing: IIRC, a
border on a block box prevents a margin from one of its children from
stretching beyond it.
Yes, that's right. Margins don't collapse "across" borders is the way
the spec explains this.
Quote:
So when you had a border, it constrained the
paragraphs' margins. That's why the problem only occurred when you
removed the border.
>
In fact, a hack you can use is to set a 1px border in the same color as
the background.
Or just 1px padding, which is then automatically the right colour, and
will also get any background image rendered on top of it in the expected
way.
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles