Вопрос

I need help positioning a logo horizontally center. It also needs to be centered vertically to the top of my links div. Can someone help me?

I'd like my logo to look like this: enter image description here

Below is my HTML and CSS:

HTML - http://codebin.org/view/199faa14

<div id="container">

      <div id="logo">
      <img src="images/images/logo.gif" />
      </div>

      <div id="navigation">
      navigation
      </div>

      <div id="header">
      </div>

      <div id="line">
      </div>

      <div id="content">
      content
      </div>

    </div>

CSS - http://codebin.org/view/dda88d94

body {
background: url(../images/images/bg_page.gif) center center;
}

#container {
width: 940px;
margin: 0 auto;
}

    #header {
    height: 281px;
    background: url(../images/home/header.gif) top center;
    position: relative;
    }

    #logo {
    position: absolute;
    z-index: 2;
    top: 0px
    height: 214px;
    margin: 10px auto 0 auto;
    }

    #navigation {
    position: relative;
    height: 40px;
    background: #fff;
    margin-top: 100px
    }

    #content {
    height: 541px;
    background: url(../images/home/bg_body.png) top center;
    position: relative;
    }

    #line {
    height: 4px;
    background: url(../images/home/line.gif) top center;
    position: relative;
    }
Это было полезно?

Решение

try something like this before using javascript!

.center {
width: 300px;
height: 300px;
position: absolute;
left: 50%;
top: 50%; 
margin-left: -150px;
margin-top: -150px;
}

or this!

.image_container {
   width: 300px;
   height: 300px;
   background: #eee;
   text-align: center;
   line-height: 300px;
 }

.image_container img {
   vertical-align: middle;
 }

Другие советы

I think I understand your question. Since you are calling out your logo img in the html, try this.

<div id="logo">
 <img src="images/images/logo.gif" alt="logo" align="center"/>
</div>

You could achieve this with:

  • display:inline-block;
  • line-height;
  • negative margin
  • text-align:justify (you have 5 items 4 links & 1 img in middle)
  • :after

Demo made in a similar context for someone else :
http://dabblet.com/gist/5375725

Some people will facing some problems while using the vertical-align: middle; styling attribute. CSS needs to be written in the prober way. For people looking for a quick fix.

.logo { left:0; top:0; height:100%; width:100px; }

<div class="logo" style="background:url('/assets/images/site-logo.png') center center no-repeat;">
  <img src="/site/logo.png" style="display:none;" />
</div>

*make sure the container has the position:relative; css attribute.

It's easy handeling retina and compatible with all kind of CSS markups. hope this simple technique can save some time for people :)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top