Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I centre absolutely positioned content of unknown width?

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?

like image 940
goose Avatar asked Feb 20 '12 20:02

goose


People also ask

How do you center an absolute position in CSS?

If you want to center something horizontally in CSS you can do it just by, using the text-align: center; (when working with inline elements) or margin: 0 auto; (when working with block element).

How do you center an absolute positioned image?

With a relative positioned parent element, an absolute positioned element has a boundary. And with the left , right , top and bottom properties with a value of 0 (specifying the distance of the edges), and an auto margin, the absolute element is centered in the parent element.

How do I center align an absolute div?

Answer: Use the CSS margin , left & right property You can align any absolutely or fixed positioned <div> element horizontally center using the CSS margin property in combination with the left and right position property.


3 Answers

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

like image 176
Samec Hadra Avatar answered Sep 27 '22 21:09

Samec Hadra


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).

like image 41
Greg Avatar answered Sep 27 '22 22:09

Greg


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%);
}
like image 38
John O Avatar answered Sep 27 '22 22:09

John O