Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DIV layout

Status
Not open for further replies.

RandyRiegel

Programmer
Sep 29, 2003
256
US
This is probably simple because I haven't didn't web development for many years. But now I got thrown into a web project at work. I used to always use tables for layouts but some of this is already written using <div>'s.

I am trying to get a top "header" on my page that does not scroll with the rest of the page. If I use "position:fixed;" in my CSS the next div is either behind the header and if i use margin-top on the content div it moves the header bar down. I have an example of what it is doing on my personal site.
Here is the code I have:
HTML:
<html>
<head>
<title>Randy's Test Site</title>
<link href="css/layout.css" type="text/css" rel="stylesheet"/>

</head>
<body>

<div class="top_header_bar">
    <div style="float:left;padding-left: 20px;">
        Hello
    </div>
    <div style="float:right;padding-right: 20px;">
        Goodbye
    </div>

</div>

<div id="content" style="width:800px; height:1000px; background-color:#cccccc;margin-top:51px;">
Randy
</div>
</body>
</html>

CSS:
/* Layout.css */

body {
    margin-left: 0px;
    margin-right: 0px;
    margin-top: 0px;

}


.top_header_bar {
    width: 100%;
    height: 50px;
    border-bottom: 1px solid black;
    background-color: #f3f4c0;
    position: fixed;

}

What am I doing wrong?
Thanks
 
RandyRiegel said:
What am I doing wrong?

Not understanding positioning.


Position absolute and fixed remove elements from the document flow so they take up no space, and you need to use margins to compensate for that.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
I read the website you sent me to and understand now about not taking up space. How can I get a DIV to take up space on the top without moving and position all the other elements under it? I've been trying tons of things and nothing I can figure out seems to work.
 
Your linked page works as expected in Chrome, and Firefox.

If you are having issues in explorer its probably the lack of doctype sending IE into quirks mode, which produces unpredictable results.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
If you want a fixed-position header that does not scroll then its best to set the BODY top-margin to whatever the height of the header is + any extra padding you may want for the first set of information on the page to have so not to crowd the header.

I would also suggest using a 70-80% opaque 1x1px png as the bg of the header so that it looks cleaner and keeps the header content easy to read.

Darryn Cooke
| Marketing and Creative Services
 
I finally found out what I was missing. I added "top:0px;" to the top_header_bar class and now it keeps the bar up top.

I am going to add a background image to the header. This was just an example and a test for me. It definitely won't look close to the same once things are done :)
 
How can I get a DIV to take up space on the top without moving and position all the other elements under it?

Add a top margin to the first static/relative element in the flow that is equal to the height of the fixed/absolute element (pluse margins and padding).

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top