
Enter a search term above to see results...
Enter a search term above to see results...
The Dimension methods in Query provide tools for getting and setting various dimensional properties of elements, including their size, position, and scroll-related attributes.
Gets or sets the width of elements.
$('selector').width()
$('selector').width(value)
Name | Type | Description |
---|---|---|
value | number/string | The width to set for the element(s) |
Query object (for chaining).
// Get the width of an elementconst width = $('#myElement').width();console.log(width);
// Set the width of an element$('#myElement').width(200);
Gets or sets the height of elements.
$('selector').height()
$('selector').height(value)
Name | Type | Description |
---|---|---|
value | number/string | The height to set for the element(s) |
Query object (for chaining).
// Get the height of an elementconst height = $('#myElement').height();console.log(height);
// Set the height of an element$('#myElement').height(150);
Gets or sets the vertical scroll position of elements.
$('selector').scrollTop()
$('selector').scrollTop(value)
Name | Type | Description |
---|---|---|
value | number | The vertical scroll position to set in pixels |
Query object (for chaining).
// Get the vertical scroll position of an elementconst scrollPosition = $('.scrollable-div').scrollTop();console.log(scrollPosition);
// Set the vertical scroll position of an element$('.scrollable-div').scrollTop(100);
Gets or sets the horizontal scroll position of elements.
$('selector').scrollLeft()
$('selector').scrollLeft(value)
Name | Type | Description |
---|---|---|
value | number | The horizontal scroll position to set in pixels |
Query object (for chaining).
// Get the horizontal scroll position of an elementconst scrollPosition = $('.scrollable-div').scrollLeft();console.log(scrollPosition);
// Set the horizontal scroll position of an element$('.scrollable-div').scrollLeft(50);
Gets the natural width of an element, including images and other HTML elements.
Natural Width - This method works by creating a clone of the element, positioning it off-screen, and measuring its dimensions. While this approach allows for accurate measurements of complex elements like tables, it can cause a reflow in the document. Use judiciously, especially in performance-critical situations.
$('selector').naturalWidth()
// Get the natural width of an imageconst imageNaturalWidth = $('img').naturalWidth();console.log(imageNaturalWidth);
// Get the natural width of a tableconst tableNaturalWidth = $('table').naturalWidth();console.log(tableNaturalWidth);
Gets the natural height of an element, including images and other HTML elements.
$('selector').naturalHeight()
// Get the natural height of an imageconst imageNaturalHeight = $('img').naturalHeight();console.log(imageNaturalHeight);
// Get the natural height of a div with contentconst divNaturalHeight = $('div.content').naturalHeight();console.log(divNaturalHeight);
Get the element that clips (crops) each element’s visual bounds based on overflow properties.
$('selector').clippingParent()
A new Query object containing the clipping parent elements.
// Find what element clips a positioned elementconst $tooltip = $('.tooltip');const $clipper = $tooltip.clippingParent();
// Check if element is clipped by its containerif ($clipper[0] !== document.documentElement) { console.log('Element is clipped by:', $clipper[0]);}
// Find the scrollable container that clips contentconst $content = $('.overflow-content');const $scrollContainer = $content.clippingParent();
// Adjust positioning to avoid clipping$scrollContainer.css('overflow', 'visible');
// Get clipping parents for multiple elements$('.positioned-elements').each((el, index) => { const $el = $(el); const $clipper = $el.clippingParent(); console.log(`Element ${index} is clipped by:`, $clipper[0]);});
Clipping Detection Returns
document.documentElement
when no clipping parent is found. Useful for positioning tooltips, dropdowns, and other overlay elements.
Get the element that establishes the positioning context for each element (where offsetTop
/offsetLeft
are relative to).
Why not
offsetParent
?offsetParent
will often return an element that is different than the valueoffsetTop
andoffsetLeft
are relative to. This is because values liketransform
,will-change
,filter
can all create new positioning contexts.
$('selector').containingParent()$('selector').containingParent({ calculate: true })
$('selector').containingParent({ calculate: false })
Name | Type | Description |
---|---|---|
calculate | boolean | Whether to calculate containing parent using modern CSS properties (default: true) |
A new Query object containing the containing parent elements.
// Find the actual positioning context (includes transform, filter, etc.)const $absoluteEl = $('.absolute-positioned');const $container = $absoluteEl.containingParent();
// Position element relative to its containing parentconst containerRect = $container[0].getBoundingClientRect();$absoluteEl.css({ top: containerRect.height / 2, left: containerRect.width / 2});
// Get browser's reported offsetParent (legacy behavior)const $element = $('.positioned-element');const $offsetParent = $element.containingParent({ calculate: false });
if ($offsetParent[0]) { console.log('Browser offsetParent:', $offsetParent[0]);}
// Fixed elements have no containing parentconst $fixed = $('.fixed-positioned');const $container = $fixed.containingParent();
if ($container[0] === undefined) { console.log('Fixed element has no containing parent');}
// Elements with transforms create new positioning contextsconst $transformed = $('.transformed-element');const $transformContainer = $transformed.containingParent();
// Useful for accurately calculating positionsconst rect = $transformContainer[0].getBoundingClientRect();console.log('Transform container bounds:', rect);