Search code examples
htmlcssimagecentergraphical-logo

Centering image at all times


I have an image, which i'm using as a logo.

I want this to be centered at all times (regardless of window size).

I haven't been able to do this with auto margins.

The image has to go over the bottom of my nav, which is placed above the logo.

HTML:

<div id="logo">

<img src="images/logorz.png" alt="logo" width="180px" />
</div>

CSS:

#logo {
    position: absolute;
    margin-left:41%;
    margin-top:-7%;


}

NAV HTML:

<div id="nav">

<ul>
    <li><img src="images/kranznav.png" alt="kranz" /><a href="index.php">COMPETE</a></li>
    <li><img src="images/thumbnav.png" alt="thumb" /><a href="index.php">SCORE</a></li>
    <li><img src="images/bagnav.png" alt="bag" /><a href="index.php">SHOP</a></li>
    <li><img src="images/morenav.png" alt="more" /><a href="index.php">MORE</a></li>
</ul>
</div>
<div class="clear">
</div>

NAV CSS:

#nav {
    background:#ffffff;
    width:100%;
    margin-top:-2em;

}


#nav ul {
    list-style-type:none;
}

#nav li {
    display:inline;
    float:left;
    width:20%;
    margin-left:2%;
    margin-right:2%;
    margin-top:5%;
    text-align:center;
}



#nav a {
    display:block;
    margin-right:0% auto;
    padding-left:0% auto;
    color:#5E09CB;
    text-decoration:none;

}


Solution

  • There is no need to use javascript for this.

    You can do it this way:

    #logo {
        position: absolute;
        left:50%;
        margin-left:-50px;
        width:100px;
    
    
    }
    

    Note, that the element needs to have a width, and the negative left margin is half the width.

    http://jsbin.com/ukexox/1/edit