[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

horizontally and vertically center image within a div

Posted on 2014-02-11
7
Medium Priority
?
375 Views
Last Modified: 2014-02-11
I want to centre an image within a div (height = 200px, width = 200px)
The image is 175px wide, height is dynamic
Horizontally aligning is not an issue, but vertical alignment is not working.

I don't want to use a background-image

The solution must work in IE7&8   (so not a CSS3 solution)

There are lots of samples in EE but I can't get any to work in IE7 with images that have dynamic heights, without setting the background-image
0
Comment
Question by:joomla
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 54

Expert Comment

by:Scott Fell, EE MVE
ID: 39849579
Try using a position relative div with an absolute div inside.  http://jsbin.com/gogum/1/edit?html,css,output  

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
 
  <div class="outer">
    <div class="inner">
      <img src="" width="200" height="200">  
    </div>
</div>
   
</body>
</html>

Open in new window

.outer{position:relative;width:400px;height:400px;background-color:green;}

.inner{position:absolute;bottom:100px;}

img{background-color:red;}

Open in new window

0
 

Author Comment

by:joomla
ID: 39849594
Hi Scott
not sure I understand

my div is a set dimension (200*200)
my image has a set width of 175
I dont know the height of my image

how do I apply your solution?
0
 
LVL 54

Expert Comment

by:Scott Fell, EE MVE
ID: 39849618
I was thinking of from the bottom...been a long night...

http://jsbin.com/gogum/2/edit

This uses jquery 1.1 and will work with ie.  You can type any height in the with to simulate what will happen.  Just click the "Run with js" after you change the hight (if you test in jsbin).  

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
 
  <div class="outer">
      <img src="" width="175" height="300">  

</div>
   
</body>
</html>

Open in new window

.outer{width:400px;height:400px;background-color:green;text-align:center;}



img{background-color:red;}

Open in new window

$(function() {
  var h = $('.outer img').height();
  var m = (400-h)/2;
  
  $('.outer img').css('margin-top',m);
});

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 54

Expert Comment

by:Scott Fell, EE MVE
ID: 39849623
This of course assumes that the image will not be taller then your 200px outer div.
0
 
LVL 54

Accepted Solution

by:
Scott Fell,  EE MVE earned 800 total points
ID: 39849624
Ok, this time I made the outer 200X200  

.outer{width:200px;height:200px;background-color:green;text-align:center;}
img{background-color:red;}

Open in new window

$(function() {
  var h = $('.outer img').height();
  var m = (200-h)/2;
  
  $('.outer img').css('margin-top',m);
});

Open in new window

0
 

Author Closing Comment

by:joomla
ID: 39849729
Thanks Scott,
the function is just what i needed
really appreciate you sticking in there
M
0
 
LVL 54

Expert Comment

by:Scott Fell, EE MVE
ID: 39849739
Glad it worked out.  I'm sorry you have to worry about ie 7 and 8.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question