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.

The responsive layout is incompatible with a Flexbox container

See original GitHub issue

What version of Next.js are you using?

11.0.1

What version of Node.js are you using?

14.16.1

What browser are you using?

Chrome

What operating system are you using?

Ubuntu 20.04.2 LTS

How are you deploying your application?

yarn start

Describe the Bug

Images with the responsive layout are not displayed on the page if put inside a flexbox container.

Expected Behavior

Responsive images should be visible.

To Reproduce

  1. Create an app through:
npx create-next-app next-image-flexbox-bug
cd next-image-flexbox-bug
  1. Create a Next page with the following code:
// pages/next-image-flexbox-bug.js
import Image from 'next/image';

export default function NextImageFlexboxBug() {
  return (
    <div style={{display: 'flex'}}>
      <Image
        src="/sunset1.jpg" 
        alt="Sunset" 
        width={600} 
        height={450} 
        layout="responsive"
      />
    </div>
  )
}
  1. Run the server:
yarn dev
  1. Open in Chrome:

http://localhost:3000/next-image-flexbox-bug

  1. Nothing is displayed on the page (the white screen).

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
kkomelincommented, Aug 25, 2021

Thanks @styfle, Please find the bug-flexbox-wrapper branch of this example project https://github.com/kkomelin/nextjs-image-optimization-examples/tree/bug-flexbox-wrapper You may run the project with yarn dev and then go to this example http://localhost:3000/responsive-images The related styles are global and located here https://github.com/kkomelin/nextjs-image-optimization-examples/blob/bug-flexbox-wrapper/styles/globals.css#L27

1reaction
styflecommented, Aug 5, 2021

This is a known issue for layout="responsive".

There’s a proposal to add layout="flex-item" to solve this use case in #18637.

In the meantime, you can use a wrapper <div style={{ width: "100%" }}> around the image so that becomes the flex item.

import Image from "next/image";

export default function Home() {
  return (
    <div style={{ display: "flex" }}>
      <div style={{ width: "100%" }}>
        <Image
          src="/sunset1.jpg"
          alt="Sunset"
          width={600}
          height={450}
          layout="responsive"
        />
      </div>
    </div>
  );
}

https://codesandbox.io/s/determined-taussig-jjn9w?file=/pages/index.js

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why flexbox not working responsive design with flex- ...
I'm using the Flexbox to do the design desktop and mobile but it not working... It is like this, as i want, using...
Read more >
Backwards compatibility of flexbox - CSS - MDN Web Docs
In the following live example, I have floated two blocks and then set display: flex on the container. The items are now flex...
Read more >
The responsive order conflict for keyboard focus - AlastairC
However, with flexbox and grid, the layout can be independent from that order. This is a good thing overall, and allows a lot...
Read more >
4. Flexbox Examples - Flexbox in CSS [Book]
Flexbox Examples You now have a complete understanding of the CSS Flexible ... In just a few lines of CSS, we can make...
Read more >
Never make your text container a flexbox container
Loosely speaking, the flex items of a flex container are boxes ... element a flexbox container, that has an explicit impact on 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