How to align an image of any size from the centre, rather than the top/bottom/sides, using css

Go To StackoverFlow.com

2

I'd like to be able to position an image (blue) so that what ever size it is, it is always centered relative to the baseline of the div behind (red, not the containing div). The div behind will always be the same size/position.

enter image description here

Preferably this would be using css only as I'm using drupal and I don't know much about editing beyond the tpl files.

Thanks!

EDIT: Here's the layout http://pastebin.com/SisQHM4y

2012-04-04 21:35
by Supertod
What have you tried so far? any chances of posting some code on jsfiddle.net - Filype 2012-04-04 21:37
Vertical alignment in CSS is a serious pain. Most solutions are nasty hacks and require extra markup to work - Marc B 2012-04-04 21:39
@Filype nothing so far, I didn't really know how to approach it - Supertod 2012-04-04 21:42
You are trying to vertically align or horizontally - chadpeppers 2012-04-04 21:43
I don't believe this is possible with pure CSS unless the height of each image is known - Bryan Downing 2012-04-04 21:45
I want the centre of the image to always be in the same place, so both I guess. - Supertod 2012-04-04 21:45
Okay, thanks @BryanDowning I'll look into other options - Supertod 2012-04-04 21:47
@Supertod - post the HTML structure like TravisJ asked, we might be able to figure something out - Bryan Downing 2012-04-04 21:49
@BryanDowning Added to main post - Supertod 2012-04-04 21:56
@Supertod - If there is any included default css file that inclusion would help as well where relevant (i.e. for the classes listed in that link - Travis J 2012-04-04 22:25
@Supertod - Do the red boxes represent DIV.profile? Do the blue boxes represent DIV.user-picture - Bryan Downing 2012-04-04 22:38


1

Hi you can do this pure css as like this

css

.wraptocenter {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
    width: 500px;
    height: 400px;
    background:green;
}

HTML

<div class="wraptocenter"><img src="//?" alt="images" /></div>

Live demo http://jsfiddle.net/rohitazad/tvrMp/

More information about this http://www.brunildo.org/test/img_center.html

2012-04-05 06:44
by Rohit Azad
Brilliant, thanks - Supertod 2012-04-05 12:50


0

Perhaps something like this:

<style>
 #blue { margin-left:auto;margin-right:auto; }
</style>

<div id="red">
 <div id="blue">
  <img src="?" id="myImg" />
 </div>
</div>

EDIT

I see, so you wish to center the x-axis horizontally, not vertically. And that link is a little messy. Perhaps you could try to

<style>
 .user-picture { margin-top: auto; margin-bottom: auto; }
</style>


<div class="content">
<div class="profile" typeof="sioc:UserAccount" about="/users/diver1">
    <div class="user-picture">
        <a href="/users/diver1" title="View user profile." class="active">
        <img typeof="foaf:Image" src="http://waterworksworldwide.com/sites/default/files/pictures/picture-126-1333572014.gif" alt="Diver1&#039;s picture" title="Diver1&#039;s picture" />
    </a>  
</div>
</div>

I am still having a little bit of a hard time seeing where the overlap between areas is as suggested by the red and blue in the question. If there is no overlap with my suggestion then please let me know and perhaps we can try to use some variations with position: absolute;

2012-04-04 21:39
by Travis J
I believe the OP is asking how to place the center x axis along the baseline of the containing div, not center it horizontally within the containing div - Bryan Downing 2012-04-04 21:44
I'm using drupal so I can't change the html layout without using some php (which I'm not familiar with). At the moment the blue isn't contained within the red div.

EDIT: @BryanDowning Yes, that's right - Supertod 2012-04-04 21:44

@BryanDowning - Ah, I misunderstood that part. That would require some javascript I believe - Travis J 2012-04-04 21:46
@Supertod - If you cannot change the html layout, can you give us an actual sample of what the html layout that is produced looks like - Travis J 2012-04-04 21:47
@TravisJ - Agreed, unless the heights of the images are consistent - Bryan Downing 2012-04-04 21:48
http://pastebin.com/SisQHM4y It's very messy... The background div is a map - Supertod 2012-04-04 21:53
@Supertod - See edit - Travis J 2012-04-04 22:23
Ads