question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Image size has more than 100% witdh

See original GitHub issue

I’m trying to render the entire html where it has p, a, img tags, but the images seems to be cropped instead be shown as 100% width.

I tried add img style, like normal css, but without success.

const styles = StyleSheet.create({
      a: {
        color: style.content.link.color,
        fontWeight: '300',
      },
      p: {
        color: this.props.data.color,
        fontSize: this.props.data.fontSize,
        lineHeight: this.props.data.lineHeight,
        marginBottom: 0,
        marginTop: 0,
        paddingBottom: 0,
        paddingTop: 0,
      },
    });
<HTMLView
addLineBreaks={false}
value={this.props.data.item.content}
stylesheet={styles}
/>

Imgur

Is there anything to fix this?

"react-native": "0.57.4",
"react-native-htmlview": "^0.13.0",

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:3
  • Comments:6

github_iconTop GitHub Comments

3reactions
HaseemUlHaqcommented, Jan 24, 2020

Just add renderNode attribute in HTMLView tag like this <HTMLView value={this.state.content} stylesheet={styles} renderNode={this.renderNode} <-- this line /> Copy and paste renderNode function with your custom styling renderNode(node, index, siblings, parent, defaultRenderer) { if (node.name == 'img') { const a = node.attribs; return ( <Image style={{width: 100, height:100}} source={{uri: a.src}}/> ); } }

0reactions
MrOttimistacommented, Nov 6, 2020

I did a workaround to solve this issue and it is working fine

import { Image, Dimensions } from 'react-native';

  function renderNode(node) {
    if (node.name === 'img') {
      return (
        <Image
          source={{ uri: node.attribs.src, cache: 'reload' }}
          style={{
            width: 'auto',
            height: node.attribs.height || Dimensions.get('window').height, 
            resizeMode: 'contain',
          }}
        />
      );
    }
  }

 <HTMLView 
   value={content}
 renderNode={renderNode} />
Read more comments on GitHub >

github_iconTop Results From Across the Web

Make an image width 100% of parent div, but not bigger than ...
Just specify max-width: 100% alone, that should do it.
Read more >
Why do we use maximum width 100% to see a full image and ...
Go with min-width: If we take the example above and we have a picture that is 640x400, min-width: 100% will make the images...
Read more >
Setting Height And Width On Images Is Important Again
To prevent layout shifts and improve performance scores, we need to always set width and height attributes on our images.
Read more >
Why setting max-width: 100% prevent an image from larger ...
Literally, setting max-width of an image to 100% should mean the width of the image cannot be greater than the width of its...
Read more >
Sizing items in CSS - Learn web development | MDN
The first image has been given width: 100% and is in a container which is larger than it, therefore it stretches to the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found