Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Position absolute behaving like position fixed when adding dynamic HTML via javascript

Here's the code example I'm having problems with:

http://jsfiddle.net/eejpjch3/

I'm trying to add a dynamic delete button above HTML elements when hovering. For instance when I have the following HTML:

   <div class="row-fluid" object="columns-editable">
        <div class="col-md-6" object="column-editable">Column 1</div>
        <div class="col-md-6" object="column-editable">Column 2</div>
    </div>
    <div class="row-fluid" object="columns-editable">
        <div class="col-md-6" object="column-editable">Column 1</div>
        <div class="col-md-6" object="column-editable">Column 2</div>
    </div>

For each row, when the user hovers, an icon pops up above that row which allows them to delete the row. However, when I'm doing this in the code, the position of the delete button stays positioned at the top of the first row even though it's set to an absolute position.

enter image description here

Thank you for your help.

like image 295
FAtBalloon Avatar asked Apr 22 '15 17:04

FAtBalloon


People also ask

What is position absolute in Javascript?

An absolutely positioned element is an element whose computed position value is absolute or fixed . The top , right , bottom , and left properties specify offsets from the edges of the element's containing block. (The containing block is the ancestor relative to which the element is positioned.)

How do you use position absolute and fixed?

Absolutely positioned elements are positioned with respect to a containing block, which is the nearest postioned ancestor. If there is no positioned ancestor, the viewport will be the containing block. Elements with fixed positioning are fixed with respect to the viewport—the viewport is always their containing block.

How do I fix the position of text in HTML?

Fixed Positioning You can use two values top and left along with the position property to move an HTML element anywhere in the HTML document. Move Left - Use a negative value for left. Move Right - Use a positive value for left. Move Up - Use a negative value for top.


2 Answers

Absolute positioning works based on the first parent it has with a relative positioning. Since you have no items with relative positioning it'll be the window that is relative. Adding

.row-fluid { position: relative; }

Would achieve what you want

like image 131
PJUK Avatar answered Oct 20 '22 00:10

PJUK


Why not use relative positioning? Here is your example using some alternative style rules, specifically

.icon {
    float:right;
    position: relative;
    bottom: 15px;
}

.upper-controls {
    position: relative;
}

JSFiddle Link

like image 32
scniro Avatar answered Oct 20 '22 00:10

scniro