2

I am trying to put an image in a div and vertically align to the middle so it follows the previous div. It goes like this

HTML

<div id="navigation">Navigation</div>
<div id="header"><img src="link"/></div>
<div id="content">content</div>

CSS

#header {
  height: 150px;
  width: 760px;
  background: red;
}

What line of code would I need in order to make this happen? Whenever I put the image, it stays in the bottom of the div. I want to raise the image so it goes to the top. There is no css code for aligning to the top. Just left and right.

davidpauljunior
  • 8,238
  • 6
  • 30
  • 54

3 Answers3

1

Can you use jQuery? if so try this:

<script>
    $(document).ready(function(){
        var headerHeight = $(".header").height();
        var imageHeight = $(".header img").height();
        $(".header img").css("margin-top",Math.floor((headerHeight - imageHeight) / 2));
    });
</script>
matt
  • 2,312
  • 5
  • 34
  • 57
1

Already answered here: How to vertically align an image inside div

use an inline-block helper with height: 100% and vertical-align: middle on both elements.

display: inline-block;
height: 100%;
vertical-align:middle;

Example: http://jsfiddle.net/kizu/4RPFa/74/

Community
  • 1
  • 1
Nick Grealy
  • 24,216
  • 9
  • 104
  • 119
0

use the CSS of:

#header {
    left:0;
    right:0;
    margin:auto;
}
07lodgeT
  • 108
  • 9