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.

Creating image links: wrapping <Image> with <Anchor> causes image to render full size, ignoring parent <Box>

See original GitHub issue

Expected Behavior

Anchor should have no effect on sizing of children

Actual Behavior

Wrapping <Image> with <Anchor> causes image to render full size, ignoring parent <Box> height and width.

URL, screen shot, or Codepen exhibiting the issue

https://codesandbox.io/s/grommet-github-issue-3186-0m2dl (see src/Image.js and https://0m2dl.codesandbox.io/image)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
Mr-Magnificentcommented, Oct 8, 2019

I believe max-width: 100%; max-height: 100% on styled.img in src/js/components/Image/StyledImage.js could solve the issue? Can I take this up?

2reactions
haloclinecommented, Jul 13, 2020

@markjackson02 – I looked into this and think there are a couple of options here, but my recommendation would be to move the Anchor so that it contains the Box and Image, rather than the Anchor being placed in between the Box and Image.

<Anchor>
  <Box height="small" width="small" >
    <Image fit="cover" />
  </Box>
</Anchor>

I have modified your CodeSandbox example to demonstrate: https://codesandbox.io/s/grommet-github-issue-3186-gingn?file=/src/Image.js

Explanation If we were to perform the same implementation in pure HTML/CSS, we would run into a similar issue where an <a> in between a <div> and <img>. This is because <a> is an inline element and receives it dimensions from its contents. When the anchor is immediately wrapping the image, there is nothing (outside of the source file) setting the image’s dimension. The solution here would be similar: <a><div><img /></div></a>.

There is a newer CSS spec recommendation for display: contents which when applied to the anchor would achieve the desired results, but it is not production ready and still has accessibility issues: https://caniuse.com/#feat=css-display-contents & https://developer.mozilla.org/en-US/docs/Web/CSS/display

@markjackson02 Can you checkout the modified CodeSandbox and confirm this alternate approach solves your needs?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Wrapping a link around an image destroys flexbox layout and ...
Wrapping a link around an image destroys flexbox layout and causes browser rendering quirks · The middle row, which I'm calling section. ·...
Read more >
How to fill a box with an image without distorting it
In this guide you can learn a technique for causing an HTML image to completely fill a box.
Read more >
Learn how to wrap text around objects in InDesign
Learn how to wrap text around any object, including text frames, imported images, and objects you draw in InDesign.
Read more >
How To Style Figure and Image HTML Elements with CSS
This tutorial will lead you through examples of image CSS styling for web pages, allowing you to make informed decisions about how images...
Read more >
float | CSS-Tricks
Ignoring the text wrap will allow the words to flow right over the image like it wasn't even there. This is the difference...
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