aur0n,
If possible, the best and easiest way to do this is to use javascript to calculate the height and width of the elements, and then place the inner element accordingly.
To accomplish this, you simply take the width of the containing element and divide it by two, then set the left value of the inner element to this value minus half its width. Then do the same for height.
Also, one of the things you might have missed is that your inner div should be set to position: relative and display: inline . The reason your div is stretched to fit the width of the containing element is because position: absolute takes the element out of the normal stream, and position: relative leaves it in the normal stream.
Here is the code I used (using jQuery):
// centering the width $("#window").css("left", ($(".transparent_background").width()/2)-($("#window").width()/2) + "px"); // centering the height $("#window").css("top", ($(".transparent_background").height()/2)-($("#window").height()/2) + "px");
With this solution, you do not need to manually set the width and height. In addition, having an internal div with multiple lines of text will not be a problem.
script: http://jsfiddle.net/ydSqU/3/
source share