[Solved] How to set top position using jquery

I am creating custom div scroller and want to set top position of content div. My jquery code is as below:

            //$("#content").css("top",( contentTopPosition-(containerOuterHeight/20) ) + "px");
            //alert("Fontsize"+$('#content').css('font-size') );

and html is:

<div id='messagePopUpContainer' style='background-color:#ffffff; overflow: hidden;'>
<a href='javascript:void(0);' id='popupanchor' onkeydown='PopupKeyHandler("' + elmId + '");'></a>

<div id='content' style='width:350px'>' + methods.settings[elmId].text + '</div >
<div id='popupReturn'>Return</div></div></div>'

Solution #1:

You can use CSS to do the trick:

$("#yourElement").css({ top: '100px' });
Respondent: Ravi Gadag

Solution #2:

Accessing CSS property & manipulating is quite easy using .css(). For example, to change single property:

$("selector").css('top', '50px');
Respondent: Starx

Solution #3:

You could also do

   var x = $('#element').height();   // or any changing value

   $('selector').css({'top' : x + 'px'});


You can use directly

$('#element').css( "height" )

The difference between .css( "height" ) and .height() is that the latter returns a unit-less pixel value (for example, 400) while the former returns a value with units intact (for example, 400px). The .height() method is recommended when an element’s height needs to be used in a mathematical calculation. jquery doc

Respondent: CG_DEV

Solution #4:

Just for reference, if you are using:


To get the position, it can be affected by the position of the parent element. Thus you may want to be consistent and use the following to set it:

$(el).offset({top: pos});

As opposed to the CSS methods above.

Respondent: Cameron

Solution #5:

And with Prototype:

$('yourDivId').setStyle({top: '100px', left:'80px'});
Respondent: ehrhardt

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy