I've got the beginnings of a website, with the header and navigation bar already implemented. I also have a main section implemented, but only in absolute positioning. I've tried many things, but how would I go about positioning it relatively? Every time I try, the section and the header get all mixed in with each other. I want the section to display in the middle. Here's my HTML...:

<!DOCTYPE html>

<html>
    <head>
        <title>B by Bruno | A friendly corporation</title>

        <link href="styles.css" rel="stylesheet" type="text/css">
    </head>

    <body>
        <header>
            <a href="#">
                <h1>B by Bruno</h1>
                <h6>A friendly corporation<span id="point">.</span></h6>
            </a>
        </header>

        <nav>
            <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#">About Us</a></li>
                <li><a href="#">Contact Us</a></li>
                <li><a href="#">The Team</a></li>
            </ul>
        </nav>

        <section>
            <h2>BIENVENU</h2>
            <p>Voici le site de notre compagnie cr&eacute;&eacute;e entre amis!</p>
        </section>
    </body>
</html>

...and my CSS:

@import url(http://fonts.googleapis.com/css?family=Nobile:400,700italic);

body {
    background-color: #222222;
    background-image: url("bg.png");
    color: #D8D8D8;
    font-family: "Nobile", sans-serif;
}

a {
    color: #D8D8D8;
    text-decoration: none;
}

header {
    float: left;
    font-style: italic;
    font-weight: 700;
    line-height: 50%;
    margin: 1em;
    text-align: center;
    width: 15em;
}

header a {
    display: block;
}

header a h1 {
    text-decoration: underline;
    -moz-text-decoration-color: red;
}

#point {
    color: red;
}

nav {
    float: left;
}

nav ul {
    background-image: -moz-linear-gradient(top, #ff0000, #8f0000);
    background-image: linear-gradient(top, #ff0000, #8f0000);
    border-radius: 0.5em;
    height: 3em;
    list-style-type: none;
    margin: 2em 0 0 1em;
    width: 60em;
}

nav ul li {
    display: block;
    float: left;
    height: 3em;
    margin: 0;
    position: relative;
    padding: 0 2.5em;
    text-align: center;
    text-shadow: 1px 1px 1px #000000;
}

nav ul li:hover {
    background-image: -moz-linear-gradient(top, #121212, #222222);
    background-image: linear-gradient(top, #121212, #222222);
}

nav ul li a {
    display: block;
    height: 2em;
    padding: 1em 0 0;
}

section {
    margin: 15em 10em;
    position: absolute;
    width: 95%;
}

section h2 {
    font-style: italic;
    font-weight: 700;
}

What exactly would I have to change, add, or remove?

Dani AI

Generated

Brief diagnosis: the overlap happens because the section is taken out of normal flow (absolute positioning) while the header/nav are floated. Absolute positioning ignores the space that floated or normal-flow elements occupy, so the section ends up overlaying the header. As noted, a float-clearing strategy is needed; as pointed out, older browsers may treat semantic HTML5 tags as inline unless declared block-level.

Two practical fixes (pick one):

  • Minimal-change (keep floats): wrap the header+nav in a single container and clear the floats, then remove absolute positioning from the section and center it with a max-width and auto margins. Example CSS:

    .topbar::after {
      content: "";
      display: table;
      clear: both;
    }
    
    section {
      position: static;        /* allow normal flow */
      max-width: 900px;        /* desired content width */
      margin: 2rem auto;       /* centers horizontally */
      padding: 1rem;
      box-sizing: border-box;
    }

    (Wrap header and nav in <div class="topbar">...</div> so the clearfix applies.)

  • Modern layout (preferred for new projects): ditch floats and use flexbox for the top row so header and nav sit side-by-side reliably; the section then naturally flows below and can be centered the same way:

    .topbar { display: flex; align-items: center; gap: 1rem; }
    .topbar > header { flex: 0 0 15rem; }
    .topbar > nav    { flex: 1; }

Compatibility/troubleshooting notes:

  • For very old IE, add a small rule to make semantic tags block-level: header, nav, section, footer, article, aside { display: block; } or include an HTML5 shim.
  • If overlap persists, inspect computed styles (position/top/left/z-index) in devtools and check for any ancestor with positioning that changes absolute positioning context.
  • Prefer max-width + margin:auto over giant fixed widths; use box-sizing to keep padding inside the width.

Recommended Answers

All 2 Replies

set your section to clear both floating divs.

section {clear:both}

I am not sure those tag names are recognized by older browser (especially IE). Also, the default display behavior of custom tags would be similar to <span>, so you may need to specify the display as block in your CSS...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.