Before someone asks me why the hell I would want to do this let me come straight out and tell you. That way one of you clever peeps out there can tell me a far better way...
I'm making a viewer for paintings which should stretch to fill the page, well 90% of the height of the screen to be precise. I want to fade the paintings in one over the other and want to center each of them in the middle of the screen.
To fade the paintings in over each other I need to position them 'absolute' to stop them from stacking. Here's where the trouble comes. Ever since I've set them to absolute, every method I use to center the containing div hasn't worked.
Part of the problem is that I'm not setting any width for the paintings as I want them to dynamically size themselves to fill 90% of the user's screen.
I've found a hundreds of methods for centering absolute content and believe I might need to shrink wrap the containing div. However I've not had any success as of yet.
HTML-
<div id="viewer_div">
<img src="" id="first" />
<img id="second" class="hidden"/>
</div>
Style Sheet
#viewer_div {
width:1264px;
}
img {
height:90%;
display:block;
margin-left: auto;
margin-right:auto;
}
The above gives me the desired effect, but doesn't allow me to position the images absolute. Can anyone suggest a way of centering the images but also allows me to fade one over the other?
Either use JavaScript to calculate the width and move it,
use a CSS hack to move the element right and left by 50%,
or don't absolutely position it.
This answer is incredibly short, but it is to the point. If you require something to be centralised (meaning you would like the browser to determine where the centre point is, and position it there), then you can't use absolute positioning - because that takes away control from the browser.
To fade the paintings in over each other I need to position them
'absolute' to stop them from stacking.
This is where your problem lies. You have assumed that you need absolute positioning for the wrong reason.
If you are experiencing problems placing elements on top of each other, wrap the images in an absolutely positioned container which is 100% width and has text-align: center
If you do feel that absolute positioning is necessary, the following hack can be used to achieve your desired results:
div {
position: absolute;
/* move the element half way across the screen */
left: 50%;
/* allow the width to be calculated dynamically */
width: auto;
/* then move the element back again using a transform */
transform: translateX(-50%);
}
Obviously the above hack has a terrible code smell, but it works on some browsers. Be aware: this hack is not necessarily obvious to other developers, or other browsers (especially IE or mobile).
Pushing the element left
by 50%
of its width and then translating it horizontally by 50%
has worked for me.
.element {
position: absolute;
left: 50%;
transform: translateX(-50%);
}
I found the concept in the following link, and then I translated to fit my horizontal align needs: https://gist.github.com/colintoh/62c78414443e758c9991#file-douchebag-vertical-align-css
To go off of Samec's answer, you can also use this to center an absolute position element vertically and horizontally of unknown dimensions:
#viewer_div {
position: relative;
}
#viewer_div img {
position: absolute;
left: 50%;
top: 0%;
transform: translate(-50%, 50%);
}