/    Sign up×
Community /Pin to ProfileBookmark

How can a border ensure the normal flow do not break?

I have created a page that has a fixed header when scrolled. It works fine, but It is very strange why! It seems rather complicated to me, but I hope that one of you guys can rumble what is going on.

[B]Permanent screen header – how it works[/B]

My study can be seen on [url]http://ovovow.dk/fixedtop[/url].

The page has two headers, one for the page when it is not scrolled, and one for the scrolled page, I gave them ids as respectively pageheader and screenheader.

The default box model is changed to border-box in the CSS, and the headers and the main content is laid out using the position and z-index elements.

The page has 3 layers. The headers are in the two upper layers. The third layer in the bottom contains the main content and the footer.

[FONT=Courier New] 1_____ __ Pageheader with title and topline (z-index: 2) – scrollable
2__ Topline (z-index: 1) – not scrollable
3 __
_
____________ __ The main content and the footer (z-index: unspecified, that is 0) – scrollable[/FONT]

The first layer is the upper layer in the z-index stack and the topmost content on the page. This layer contains the title and the topline. The middle layer contains the screenheader. This is always placed topmost on the screen (position: fixed), and on the z-index layer underneath the pageheader. The lower layer contains the main content followed by the footer.

To start with you can not see the fixed screenheader, it is hidden by the upper pageheader layer. Scrolling do not affect the screenheader, so it becomes visible when you scroll the pageheader out of the screen.

The page content will also be scrolled out of the screen, but under the screenheader in the z-index stack. This way you will always have a header on the page or screen.

[B]How come?[/B]

The strange and tricky part of the code lies in the border property on the pageheader. If you take that away the whole idea is broken.

The first line in the screenheader will be visible in the area where the h1 margin in the pageheader should cover it, and, in the bottom of the pageheader the p tag lose its margin-bottom. What is really tricky, the screenheader’s first line seems to be followed by its parent, the wrapper – something weird will come across the normal flow!!

And that is what my question is about, how come?

to post a comment
CSS

11 Comments(s)

Copy linkTweet thisAlerts:
@jedaisoulDec 12.2015 — Hi and welcome to the site. I've had a brief look at your problem, and I can't make head or tails of it! ? One thought, what browsers have you used to check the behavior?
Copy linkTweet thisAlerts:
@cootheadDec 13.2015 — Hi there wwbee,

[indent]

and a warm welcome to these forums. ?

Unless contained by padding or border of the parent element

it's child element's top and bottom margins will overflow.

This example may help...

[color=navy]
<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>untitled document</title>

<style media="screen">
body {
background-color:#f0f0fe;
text-indent:10px;
}
h6 {
width:25%;
padding:5px 0;
border-radius:10px;
background-color:#333;
color:#fff;
}
.grandparent {
background-color:#999;
}
.parent {
width:75%;
background-color:#ccc;
}
.child {
width:75%;
padding:5px 0;
margin-top:20px; /*this will overflow unless contained */
margin-bottom:20px;
background-color:#fff;
}
.padding {
padding-top:1px;
padding-bottom:1px;
}
.border {
border:2px solid #000;
}
</style>

</head>
<body>

<h6>example 1</h6>

<div class="grandparent margin">
<div class="parent">
<div class="child">the child's margin is uncontainted</div>
</div>
</div>

<h6>example 2</h6>

<div class="grandparent padding">
<div class="parent">
<div class="child">the child's margin is containted by the grandparent's padding</div>
</div>
</div>

<h6>example 3</h6>

<div class="grandparent">
<div class="parent padding">
<div class="child">the child's margin is containted by the parent's padding</div>
</div>
</div>

<h6>example 4</h6>

<div class="grandparent border">
<div class="parent">
<div class="child">the child's margin is containted by the grandparent's border</div>
</div>
</div>

<h6>example 5</h6>

<div class="grandparent">
<div class="parent border">
<div class="child">the child's margin is containted by the parent's border</div>
</div>
</div>

</body>
</html>[/color]


[/indent]

[i]coothead[/i]
Copy linkTweet thisAlerts:
@wwbeeauthorDec 15.2015 — First of all thanks for your kind welcomes! Unfortunately for some reason I haven't been able to login again until now, for network reasons had to use another computer and bla bla bla, ... eventually I succeded :-)

@jedaisoul

I've created a page with a #pageheader and a #screenheader, where the screenheader replaces the pageheader in top of the screen and stay there when the page is scrolled.

Screenheader have z-index=1 and position:fixed, pageheader have z-index=2 and position:relative, page content have z-index=default (0).

The entire site is contained in a div, #wrapper.

If pageheader have a border everything work as it is meant to, without a border it doesn't and I do not understand why.

I hope that's more understandable?

@coothead

Thanks for your very illustrative example, it has made me a bit wiser but also a bit more confused.

As far as I can see this doesn't seem to address the issue in my case. I need a border on the child element, not the parent. If your explanation should work on my site, I could give my #wrapper (parent) a border, but it makes no difference. The border has to be placed on the #wrapper's child, the #pageheader. I guess it is related to my use of the positioned elements, but how and when the normal rules turn upside down I do not know, but it actually seem to happen when positioning your elements?
Copy linkTweet thisAlerts:
@cootheadDec 16.2015 — Hi there wwbee,

[indent]
[color=navy]As far as I can see this doesn't seem to address the issue in my case.[/color][/quote]
Then you have not fully understood my example. :eek:

The margin that has to be contained is the default margin of the [color=navy][i]"h1 element"[/i][/color] here...
[color=navy]
<div id="pageheader">
<h1>Pageheader</h1>[/color]

...where [color=navy][i]"<div id="pageheader">"[/i][/color] is the [b]parent[/b] and [color=navy][i]"<h1>Pageheader</h1>"[/i][/color] is the [b]child[/b].

If you don't want to use a border to contain the margin, then [color=navy][i]"padding-top:1px"[/i][/color] will do the job. ?

Here is the the alternative code...
[color=navy]
#pageheader {
padding-top:1px; /* this will contain the margin of the h1 element */
[color=red]/* border: 3px double SeaGreen; ··· NB: Absolutely necessary! ··· */[/color]
position: relative;
z-index: 2;
}
#screenheader {
[color=red]/*border: 3px double darkSeaGreen;*/[/color]
position: fixed;
z-index: 1;
}[/color]

[/indent]

[i]coothead[/i]
Copy linkTweet thisAlerts:
@wwbeeauthorDec 17.2015 — Well, no, I do not fully understand what is going on. Let me ask another question: If pageheader and screenheader are equal except for their position and z-layer properties, the content are the same, one h1 tag each, no borders nor paddings, both child of the wrapper, how come only the pageheader will get the overflow issue?
Copy linkTweet thisAlerts:
@wwbeeauthorDec 17.2015 — Bug fix: z-layer = z-index ?
Copy linkTweet thisAlerts:
@wwbeeauthorDec 27.2015 — As I couldn't find any explanation of why my pagewrapper had to have a border or padding, I made a new study on positioned elements. I created the page with 2 wrappers underneath each other, and with exactly the same content: four 'div's formatted with respectively position static, relative, absolute and fixed. I avoided the use of padding or borders, I gave all 'div's, 'h1's and 'p's a transparent background-image to visualize the edge of every box, and I put a shadow on the h1-texts to make it easier to perceive its position in the stack.

That way it became obvious that the absolute positioned boxes were visually formatted different than the other boxes. Then I added content about my observation.

Anyway it still seemed strange to me. W3.org's description for absolute positioned elements says that Absolutely positioned boxes are taken out of the normal flow. This means they have no impact on the layout of later siblings.[/QUOTE] However my problem wasn't formatting of the box's child(ren), but the box itself.

I kept looking in the CSS Reference on w3.org and eventually I found 9.4.1 Block formatting contexts, that says, that absolutely positioned elements (...) establish new block formatting contexts for their contents.[/QUOTE] If that means that absolutely positioned elements contain/take care of their own overflows, it makes sense to me - what do you think?
Copy linkTweet thisAlerts:
@cootheadDec 27.2015 — Hi there wwbee,

[indent]
[color=navy]

As I couldn't find any explanation of why my page wrapper had to have a border or padding,

I made a new study on positioned elements.[/color]
[/quote]

Before you moved on to pastures new you should have read this article...

[indent][url=https://css-tricks.com/what-you-should-know-about-collapsing-margins/]collapsing margins ?[/url][/indent]

[/indent]

[i]coothead[/i]
Copy linkTweet thisAlerts:
@wwbeeauthorDec 27.2015 — Thank you, I'll have a look at that.
Copy linkTweet thisAlerts:
@wwbeeauthorDec 28.2015 — Good explanation, but nothing new in it to answer my question. I don't see where the clarification lies.
Copy linkTweet thisAlerts:
@wwbeeauthorDec 28.2015 — Now, let me rephrase once more.

If a margin has to be contained by padding or border of the parent element, how come margins on absolutely positioned elements do not overflow? Which element is the parent, which padding or border ensures that absolutely positioned elements margins do not overflow, when the document contains no borders or paddings at all?

If it is because absolutely positioned elements (...) establish new block formatting contexts for their contents[/QUOTE]then new questions arise, eg.:
[LIST=1]
  • [*]Are absolutely positioned elements self-contained?

  • [*]Are margins part of their content?

  • [/LIST]
    ×

    Success!

    Help @wwbee spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.18,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...