Get the position of a div/span tag
This function will tell you the x,y position of the element relative to the page. Basically you have to loop up through all the element's parents and add their offsets together.
function getPos(el) {
// yay readability
for (var lx=0, ly=0;
el != null;
lx += el.offsetLeft, ly += el.offsetTop, el = el.offsetParent);
return {x: lx,y: ly};
}
However, if you just wanted the x,y position of the element relative to its container, then all you need is:
var x = el.offsetLeft, y = el.offsetTop;
To put an element directly below this one, you'll also need to know its height. This is stored in the offsetHeight/offsetWidth property.
var yPositionOfNewElement = el.offsetTop + el.offsetHeight + someMargin;
How to position div relatively to a span?
Maybe u want this? If you want to make dropdown menu do it other way
.hidden-word{ position:absolute; visibility:hidden; font-size:11px;}
.word{ display:inline-block;}.word:hover > .hidden-word{ visibility:visible;}
<div> <span>Blah blah...</span> <span class="word">some word <div class="hidden-word">Hidden word</div> </span> <span>Bluh bluh...</span></div>
move span element right to div tag
<div style='width:600px;padding-top: 2px;padding-bottom: 1px;border:1px solid red;'> <span style='padding-left: 25px'>" + rank + "</span > <span style='padding-left: 35px'><img id='img1' class='img-rounded' alt='' width='50' height='50' src='test.jpg' /></span> <span style='padding-left: 30px' >" + o.Title + "</span> <span style='float:right'>{IMG}<img class='img-rounded' alt='' width='20' height='10' src='images/Information.png' /> </span> </div >
get coordinates of the character in a span in context of the container div
To get the coordinates of a word, it must be wrapped in an element first
$('.container').html(function(_, html) {
return html.replace('standard', '<span class="standard">standard</span>');
});
Then you get the offset()
position of that element relative to the document, or relative to the parent element with position()
var position = $('.standard').offset();
FIDDLE
Related Topics
Make a Link Use Post Instead of Get
Pass a String Parameter in an Onclick Function
How to Go to a Specific Element on Page
Can You Style an HTML Radio Button to Look Like a Checkbox
How to Simulate a Click to an Anchor Tag
How to Use Mechanize to Process JavaScript
Cross-Browser Multi-Line Text Overflow With Ellipsis Appended Within a Fixed Width and Height
How to Prevent a Click on a '#' Link from Jumping to Top of Page
Restricting Input to Textbox: Allowing Only Numbers and Decimal Point
How to Draw an Inline Svg (In Dom) to a Canvas
Get City Name Using Geolocation
How to Decode HTML Entities Using Jquery
Is There a Spec That the Id of Elements Should Be Made Global Variable
Get Dom Content of Cross-Domain Iframe
Do You Need Text/JavaScript Specified in Your ≪Script≫ Tags