Here’s what I’m trying to accomplish:
I need a button which is positioned a certain distance from the right side of a div, and is always that same distance from the side of the div no matter the size of the viewport, but will scroll with the window.
So it is x pixels from the right side of the div at all times, but y pixels from the top of the view port at all times.
Is this possible?
Position Fixed Element Horizontally Based Off Another Element
(Disclaimer Note: The solution offered here is not technically “absolute horizontally” as the question title stated, but did achieve what the OP originally wanted, the fixed position element to be ‘X’ distance from the right edge of another but fixed in its vertical scroll.)
I love these types of css challenges. As a proof of concept, yes, you can get what you desire. You may have to tweak some things for your needs, but here is some sample html and css that passed FireFox, IE8 and IE7 (IE6, of course, does not have
position: fixed).HTML:
CSS (borders and all dimensions for demonstration):
The key is to not set the
leftorrightat all for the horizontal on thediv.fixedbut use the two wrapper divs to set the horizontal position. Thediv.positioneris not needed if thediv.inflowis a fixed width, as the left margin of thediv.fixedcan be set to known width of the container. However, if you desire than container to have a percentage width, then you will need thediv.positionerto push thediv.fixedto the right side of thediv.inflowfirst.Edit: As an interesting side note, when I set
overflow: hidden(should one need to do that) on thediv.inflowhad no effect on the fixed position div being outside the other’s boundaries. Apparently the fixed position is enough to take it out of the containing div’s context foroverflow.