Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Position an Element Absolute from Center - CSS

Tags:

For a client, I need to put together an absolute div to the left of the body containing an advertisement. The only way I have figured out how to do this thus far is to create an absolute div. The issue here is that in the absolute positioned div, there is a black space (which is the end of the body) and I need to have this "gutter space" ad hugging the page.

To account for this, the best method is to position this a set number of pixels away from the center of the page. I thought I could do this by the following code:

position: absolute;
left:50%;
margin-right:500px;

However, this just keeps the div in the center of the page. Is it possible to position an absolute div from the center of a page?

like image 744
Bob Cavezza Avatar asked Dec 16 '10 17:12

Bob Cavezza


1 Answers

If you know the width of the advertisement then you can do this:

position:absolute;
left:50%;
margin-left:-250px;
width:500px;

notice the negative margin-left which is half of the width of the element to be positioned

like image 79
Mark Steggles Avatar answered Oct 05 '22 01:10

Mark Steggles