HTMLImageElement
interface's read-only
naturalWidth
property returns the intrinsic (natural), density-corrected width of the image in
CSS pixels
.
This is the width the image is if drawn with nothing constraining its width; if you neither specify a width for the image nor place the image inside a container that limits or expressly specifies the image width, this is the number of CSS pixels wide the image will be.
The corresponding
naturalHeight
method returns the natural height of the image.
let naturalWidth = htmlImageElement.naturalWidth;
An integer value indicating the intrinsic width of the image, in CSS pixels. This is the width at which the image is naturally drawn when no constraint or specific value is established for the image. This natural width is corrected for the pixel density of the device on which it's being presented, unlike the value of
width
.
If the intrinsic width is not available—either because the image does not specify an intrinsic width or because the image data is not available in order to obtain this information,
naturalWidth
returns 0.
This example simply displays both the natural, density-adjusted size of an image as well as its rendered size as altered by the page's CSS and other factors.
<div class="box"> <img src="/files/16797/clock-demo-400px.png" class="image"> </div> <div class="output"> </div>
The HTML features a 400x398 pixel image which is placed inside a
<div>
.
.box {
width: 200px;
height: 200px;
}
.image {
width: 100%;
}
.output {
padding-top: 2em;
}
The main thing of note in the CSS above is that the style used for the container the image will be drawn in is 200px wide, and the image will be drawn to fill its width (100%).
let output = document.querySelector(".output");
let image = document.querySelector("img");
window.addEventListener("load", event => {
output.innerHTML += `Natural size: ${image.naturalWidth} x ` +
`${image.naturalHeight} pixels<br>`;
output.innerHTML += `Displayed size: ${image.width} x ` +
`${image.height} pixels`;
});
The JavaScript code simply dumps the natural and as-displayed sizes into the
<div>
with the class
output
. This is done in response to the window's
load
event handler, in order to ensure that the image is available before attempting to examine its width and height.
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'HTMLImageElement.naturalWidth' in that specification. |
实时标准 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
naturalWidth
|
Chrome 1 | Edge 12 | Firefox Yes | IE 9 | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android Yes | Opera Android ? | Safari iOS Yes | Samsung Internet Android Yes |
完整支持
兼容性未知
HTMLImageElement
BeforeUnloadEvent
DOMStringMap
ErrorEvent
GlobalEventHandlers
HTMLAnchorElement
HTMLAreaElement
HTMLAudioElement
HTMLBRElement
HTMLBaseElement
HTMLBaseFontElement
HTMLBodyElement
HTMLButtonElement
HTMLCanvasElement
HTMLContentElement
HTMLDListElement
HTMLDataElement
HTMLDataListElement
HTMLDialogElement
HTMLDivElement
HTMLDocument
HTMLElement
HTMLEmbedElement
HTMLFieldSetElement
HTMLFormControlsCollection
HTMLFormElement
HTMLFrameSetElement
HTMLHRElement
HTMLHeadElement
HTMLHeadingElement
HTMLHtmlElement
HTMLIFrameElement
HTMLInputElement
HTMLIsIndexElement
HTMLKeygenElement
HTMLLIElement
HTMLLabelElement
HTMLLegendElement
HTMLLinkElement
HTMLMapElement
HTMLMediaElement
HTMLMetaElement
HTMLMeterElement
HTMLModElement
HTMLOListElement
HTMLObjectElement
HTMLOptGroupElement
HTMLOptionElement
HTMLOptionsCollection
HTMLOutputElement
HTMLParagraphElement
HTMLParamElement
HTMLPictureElement
HTMLPreElement
HTMLProgressElement
HTMLQuoteElement
HTMLScriptElement
HTMLSelectElement
HTMLShadowElement
HTMLSourceElement
HTMLSpanElement
HTMLStyleElement
HTMLTableCaptionElement
HTMLTableCellElement
HTMLTableColElement
HTMLTableDataCellElement
HTMLTableElement
HTMLTableHeaderCellElement
HTMLTableRowElement
HTMLTableSectionElement
HTMLTemplateElement
HTMLTextAreaElement
HTMLTimeElement
HTMLTitleElement
HTMLTrackElement
HTMLUListElement
HTMLUnknownElement
HTMLVideoElement
HashChangeEvent
历史
ImageData
定位
MessageChannel
MessageEvent
MessagePort
Navigator
NavigatorGeolocation
NavigatorID
NavigatorLanguage
NavigatorOnLine
NavigatorPlugins
PageTransitionEvent
Plugin
PluginArray
PopStateEvent
PortCollection
PromiseRejectionEvent
RadioNodeList
Transferable
ValidityState
Window
WindowBase64
WindowEventHandlers
WindowTimers