I need to place a div
(with position:absolute;
) element in the center of my window. But I am having problems doing so, because the width is unknown.
I tried this. But it needs to be adjusted as the width is responsive.
.center {
left: 50%;
bottom:5px;
}
Any ideas?
sass/compass version of Responsive Solution above:
As far as I know, this is impossible to achieve for an unknown width.
You could - if that works in your scenario - absolutely position an invisible element with 100% width and height, and have the element centered in there using margin: auto and possibly vertical-align. Otherwise, you'll need Javascript to do that.
I know I already provided an answer, and my previous answer, along with others given, work just fine. But I have used this in the past and it works better on certain browsers and in certain situations. So I thought id give this answer as well. I did not "Edit" my previous answer and add it because I feel this is an entirely separate answer and the two I have provided are not related.
HTML:
CSS:
the solution of this question didn't work for my case.. I'm doing a caption for some images and I solved using this
Absolute Centre
HTML :
CSS :
Demo: http://jsbin.com/rexuk/2/
Tested in Google Chrome, Firefox, and IE8
Hope this helps :)
Really nice post.. Just wanted to add if someone wants to do it with single div tag then here the way out:
Taking
width
as900px
.In this case one should know the
width
beforehand.