
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);