How to get width and height for img tags
See original GitHub issueThis probably sounds like a very stupid question but how do I get the parsed values for an image without downloading the actual image. The reason why I need this is because I need to know if an image is going to fit on a page (for example a letter) and I want to avoid downloading every image when it is not needed.
I want to detect the width and height for the image in the following cases:
- the width and height is declared like this
<img src="example.jpg" width="200px" height="300px">
- the width and height is declared with a class like this
...
<style>.image {width: 100px; height 200px}</style>
...
<img src="example.jpg" class="image">
- the width and height is declared in the style attribute like this
<img src="example.jpg" style="width: 400px; height:300px">
I already tried things like this:
var config = Configuration.Default.WithCss();
var parser = new HtmlParser(config);
var document = parser.Parse(webpage);
// ReSharper disable once PossibleInvalidCastExceptionInForeachLoop
foreach (var htmlImage in document.Images)
{
// The local width and height attributes always go before css width and height
var attributeWidth = htmlImage.Attributes.GetNamedItem("width");
var attributeHeight = htmlImage.Attributes.GetNamedItem("height");
var attributeStyle = htmlImage.Attributes.GetNamedItem("style");
if (attributeStyle != null)
{
var css = parser.Parse(attributeStyle.Value);
}
if (htmlImage.ClassList != null)
{
if (htmlImage.ClassList != null)
{
foreach (var value in htmlImage.ClassList)
{
var css = document.QuerySelector(htmlImage.TagName + "." + value);
}
}
}
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:9 (4 by maintainers)
Top Results From Across the Web
HTML img width Attribute
The width attribute specifies the width of an image, in pixels. Tip: Always specify both the height and width attributes for images. If...
Read more >How to get the image size (height & width) using JavaScript
var tempImg = $('<img id="testImage" />'); // If you want to get the height with respect to any specific width you set. //...
Read more >Setting Height And Width On Images Is Important Again
With no image dimensions, you can get away with just specifying max-width: 200px in the CSS without having to specify height: auto and...
Read more >HTML img tag
If width and height are not specified, the page might flicker while the image loads. Tip: To link an image to another document,...
Read more >width and height of an image - TAG index
The width and height attributes of the IMG element specifies the width and height of an image. ... The data size of the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
On a general note:
There have been efforts to write a complete documentation, but since community (support) was missing it was pretty much abandoned. Obviously, there is no money in OSS development (no problem with that), but if even essential support / engagement is missing then motivation and prioritization is going down. If you want to start something (and may it just be a collection of all the questions that come to your mind - it would indeed be a helpful starter) then we could at least end up with a “cookbook” style documentation (incl. general introduction, recipes, and FAQs). 🍻