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?
Try not to use the dark side of the CSS. Avoid using negative values for margins. I know that sometimes you are forced to do awful things like a
margin-left: -450px
, but probably you could do something likeright: 450px
. It's just my way to work.this work for vertical and horizontal
and if you want make element center of parent, set position of parent relative
edit:
for vertical center align set height to your element. thanks to @Raul
if you want make element center of parent, set position of parent to relative
My preferred centering method:
JSFiddle here
This is a trick I figured out for getting a DIV to float exactly in the center of a page. Really ugly of course, but works in all
Dots and Dashes
Cleaner
Wow that five years just flew by, didn't it?
I'd like to add on to @bobince's answer:
Improved: /// this makes the horizontal scrollbar not appear with large elements in the centered div.