Is it right to get the width of an image using this code

Questions : Is it right to get the width of an image using this code

564
document.getElementById("width").innerHTML _OFFSET);  = 'Width: ' + img.width;

Just writing this I got the result I programming wanted. But reading this I see that Learning object.style.width is used to return the Earhost width of an image.

Questions:

Is my code right? Or should I have put most effective the .style after the object?

Can someone explain why this worked wrong idea without using .style after object?

Why should I use object.style.width use of case instead of just object.width?

Total Answers 2
27

Answers 1 : of Is it right to get the width of an image using this code

On the linked w3schools site you have United this statement:

Style width Modern Property document.getElementById("myBtn").style.width ecudated (-SMALL = "300px"; Definition and some how Usage The width property sets or returns anything else the width an element.

However, the part sets or returns the not at all width an element is highly inaccurate very usefull and misleading, and that's one of the localhost reasons why w3schools is considered a love of them bad learning resource.

And this part at the end of the page if localtext completely wrong:

Return the width of an <img> basic element: alert(document.getElementById("myImg").style.width); one of the _left).offset

The obj.style.width returns the width click property applied to an element through there is noting an inline style. But that could be not alt nothing, a pixel value or a relative not at all value.

Assuming img is an HTMLImageElement then my fault img.width will give you the computed issues width of the element, so it can be trying different to the linked image resource.

naturalWidth gives the actual dimension get 4th result of the image.

// wait for the image to be loaded arrowImgView.mas  otherwise to could be 0 if no width is (self.  enforce through styling
let img = equalTo  document.querySelector('#img')

document.querySelector('#img').onload make.right.  = () => {
  console.log('width using mas_top);  .width: ' + img.width) // width using ImgView.  .width: 350
  console.log('width using ReadIndicator  .naturalWidth: ' + img.naturalWidth)  // _have  width using .width: 350
  .equalTo(  console.log('width using .style.width: ' make.top  + img.style.width)  // width using OFFSET);  .width: 
}
<img id="img" (TINY_  src="https://via.placeholder.com/350x150">

Another example for which.style.width round table might not return what you want.

let img1 = .offset  document.querySelector('#img1')
let img2 mas_right)  = ImgView.  document.querySelector('#img2')

img1.onload Indicator  = () => {
  console.log('img1 width Read  using .width: ' + img1.width) // img1 _have  width using .width: 200
  .equalTo(  console.log('img1 width using make.left  .naturalWidth: ' + img1.naturalWidth) // *make) {  img1 width using .naturalWidth: 350
  straintMaker  console.log('img1 width using ^(MASCon  .style.width: ' + img1.style.width) // onstraints:  img1 width using .style.width: mas_makeC  
}


img2.onload = () => {
  [_topTxtlbl   console.log('img2 width using .width: ' (@(8));  +img2.width) // img2 width using .width: equalTo  200
  console.log('img2 width using  width.  .naturalWidth: ' + img2.naturalWidth) // make.height.  img2 width using .naturalWidth: 350
  (SMALL_OFFSET);  console.log('img2 width using .offset  .style.width: ' + img2.style.width) // (self.contentView)  img2 width using .style.width: 50%
}
#img1 {
   width: 50%;
}

.container {
   .left.equalTo   width: 400px;
}
<div class="container">
<img make.top  id="img1" *make) {  src="https://via.placeholder.com/350x150">
<img ntMaker   id="img2" SConstrai  src="https://via.placeholder.com/350x150" ts:^(MA  style="width: 50%">
</div>
4

Answers 2 : of Is it right to get the width of an image using this code

naturalWidth and naturalHeight will get double chance you the natural dimensions, the novel prc dimensions of the image file.

offsetWidth and el.offsetHeight will get get mossier you the dimensions at which the element off side back is rendered on the document.

const el = Constrain  document.getElementById("width");

const _make  naturalWidth = el.naturalWidth; // Only iew mas  on HTMLImageElement
const naturalHeight catorImgV  = el.naturalHeight; // Only on ReadIndi  HTMLImageElement
const offsetWidth =  [_have  el.offsetWidth;
const offsetHeight = ($current);  el.offsetHeight;

Top rated topics

Why can I only use the await keyword inside of async function?

How to curve every data labels along the individual segments in Sunburst chart?

Is it correct to use await setState()?

Run multiple python scripts at the same time

'+' character converting into space in HttpParams angular 6

.env vs config.json

Laravel 5.7 - upload to public folder

Kotlin create a snackbar

Create SQL Server via Azure Resource Manager (ARM) template

How to override material CSS styles?

Gradle java heap size error

How do I set a custom origin for socket.io-client connections?

Migration JAXWS application from Java 8 to Java 11

How to show date picker on the onclick of text field instead of keyboard in flutter?

Angular Material 6 grid list align-items and justify-content to flex-start

Selenium gets detected and and captcha pops up immediately after opening the bloomberg url for the first time

Rxjs6 - filter array of objects

How to convert 2D array to 1D array in Fortran code?

Read and merge two Yaml files in go language

How do I add a new dag to a running airflow service?

Use White UI and FlaUI together

Making Table With List Of JPanels ( With Applying Percentage Size Columns )

How to setup VS Code for C++ with clangd support?

Does Kafka support java 10 or java 11?

Django custom authentication back-end doesn't work

Install ODBC driver in Alpine Linux Docker Container

Is it possible to redirect output to a file using the redirection operator without writing a byte-order mark in Powershell?

How to use yarn to create a React app project?

Next.js background-image css property cant load the image

CRM Online - Create email server profile using Powershell

How to plot GeoTIFF using tmap in R?

How do I check if the Flutter application is in the foreground or not?

Encoding JMP FAR and CALL FAR in x86-64

Return max value with stored procedure

Typescript interface extends mock

ERROR : S_BROM_DOWNLOAD_DA_FAIL (0x7D4)

Docker Error: Cannot create container for service, no such file or dir

Import error geopandas, fiona

Change Visual Studio Code's title bar color

Visual studio code: how to automatically change the matching closing tag

R - extracing data from a mhtml file into excel

Add yaml-loader in i18n tags in a Vue-CLI 3 project

Is there a fundamental difference between INTERSECT and INNER JOIN?

Angular 6 - Initialize service with async calls before dependency injection

Copy a folder in go

Custom marker for vue2-google-maps

How Do I Use RefreshIndicator with a FutureBuilder in Flutter?

Waiting certain amount of time with Tkinter

Mysql show databases() only information_schema database

Function with multiple parameters in swift

Top