tags:

views:

58

answers:

5

hi there,

in the header section of my web page, the design is something like the logo and navigation div should overlap a repeat-x image(i.e bg-header).

this is the html i have used

<div id="header">
    <div id="header-bar">
        <p>kljslakdjlaskjdkljasdlkjasdlkjaskldjasjd</p>
    </div>
</div>

and this is the css

#header {
    min-width: 1040px;
    height: 111px;
    position: relative;
}
#header-bar {
    margin-top:50px;
    height:53px;
}

now when in the #header-bar if i give margin-top:50px then the header div shifts the position to 50px from top. i want to achieve something like

the header div is to define the height of the header content.

i want to wrap header-bar in the header div and the elements or the div wrapped inside the header div should should have the margin of 50px from within the header.

for example the header-bar should have a margin of 50px from the top of the header div.

when i use the above code it moves the position of header div too. i want the header div to be fixed on top only the sub div or content within the header div is what i want to position.

hope i am not confusing you.

where i am going wrong?

thank you

EDIT: it works if i use padding-top but excluding the background with repeat-x property.

i want to move the image with repeat-x property. in the header-bg div

+1  A: 

Margin doesn't affect the position of elements relative to their parents.

To achieve the effect you want, you need to use padding on the #header, for example:

#header {
    min-width: 1040px;
    height: 61px;
    position: relative;
    padding-top: 50px;
}
#header-bar {
    height:53px;
}
Ben James
A: 

Use padding on the header div rather than margin.

#header {
    min-width: 1040px;
    height: 111px;
    padding:50px 0px 0px 0px;
}
#header-bar {
    height:53px;
}
jimplode
yes i tried that way too. but again it is giving the output the same as margin. it is leaving the space from outside and not inside.
Ibrahim Azhar Armar
that does not sound right!! Can you post a full code which demonstrates the error and I will make it work for you.
jimplode
thank you jim i really appreciate your help. well i am close to the solution. i will surely get back to you. if i need any help. thanks again for that kind word.
Ibrahim Azhar Armar
+1  A: 

If you add "overflow:hidden" to the #header div, it'll work like a charm! Note that there is padding, but also margin. If you remove the padding, there will still be space left, that's the margin!

Jsfiddle example here

Justus Romijn
Your example is misleading. This has nothing to do with `overflow: hidden` - the fix comes from your use of padding on `#header`
Ryan Kinal
The padding is there because i copied the existing code. If you remove the padding, there is still 50 pixels margin.
Justus Romijn
Oh hey, you're right. Apologies, and I'd rescind my downvote if I could.
Ryan Kinal
Oh that's too bad.
Justus Romijn
Can't you just use upvote again? that -1 is ugly :)
Justus Romijn
I think if you edit the answer, I might be able to.
Ryan Kinal
I've added a bit of context for the readers :)
Justus Romijn
A: 

You're running into something called margin-collapse. In essence, adjacent margins will collapse together, and only display the larger one - that is, the one with more absolute distance from 0. Since your #header margin (0px) is adjacent to your #header-bar margin (50px), the 50px margin is the one that is displayed, and it affects both of your elements.

If you were to add even 1px of padding to the top of #header, you would get the desired effect:

#header {
    min-width: 1040px;
    height: 111px;
    position: relative;
    padding-top: 1px;
}
Ryan Kinal
A: 

I'm not sure I understood the question.

Does it seem like your answer : link ?

Chouchenos