[英]Cannot fit inner image into its parent container on this ReactJS app
I have one very basic ReactJS
component called: Designer
which displays the Eiffel Tower
as well as a bottom panel with some controls in it.我有一个非常基本的ReactJS
组件,称为: Designer
,它显示Eiffel Tower
以及带有一些控件的底部面板。
The Designer
is inside a div
with the following size: { width: 510px, height: 300px}
. Designer
位于具有以下大小的div
: { width: 510px, height: 300px}
。
The Eiffel Tower
image has the following size: { width: 300px, height: 800px}
. Eiffel Tower
图像具有以下大小: { width: 300px, height: 800px}
。
This is the current rendered output of the Designer
:这是Designer
的当前渲染输出:
But my problem is that the Eiffel Tower
image is not getting fit into its parent div
.但我的问题是Eiffel Tower
图像没有适应其父div
。
My goal is what you can see on the following image:我的目标是您可以在下图中看到的内容:
If on the file: /src/CanvasCrop/CanvasCrop
we do: let showImage = false;
如果在文件上: /src/CanvasCrop/CanvasCrop
我们这样做: let showImage = false;
, we get the following: ,我们得到以下信息:
so, we confirm that its parent div
( green
background) gets sized accordingly having its own width
and height
whether the image is getting shown or not.因此,我们确认其父div
( green
背景)的大小相应地具有自己的width
和height
无论图像是否显示。
Here is its source code (original):这是它的源代码(原始):
/src/CanvasCrop/CanvasCrop
: /src/CanvasCrop/CanvasCrop
:
import React, { Component } from "react";
import "./CanvasCrop.scss";
class CanvasCrop extends Component {
render() {
let showImage = true;
return (
<div className="partial-designer-canvas-crop">
<div>
{
showImage &&
<img
src="https://i.ibb.co/DKxRFtb/image.jpg"
style={{
maxWidth: "100%",
maxHeight: "100%",
transform: "scale(-50%, -50%)"
}}
alt="target"
/>
}
</div>
</div>
);
}
}
export default CanvasCrop;
Here is the outer source code:这是外部源代码:
/index.js
: /index.js
:
import React from "react";
import ReactDOM from "react-dom";
import "./styles.scss";
import Designer from "./Designer/Designer";
function App() {
return (
<div>
<span class="title">Trying to fit Eiffel Tower</span>
<div className="App"
style={{
// keep this as it is
overflow: 'auto',
width: '510px',
height: '300px',
}}
>
<Designer />
</div>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Here you have the CodeSandbox.io
you can experiment with:在这里,您可以尝试使用CodeSandbox.io
:
https://codesandbox.io/s/7jv56qr1yx https://codesandbox.io/s/7jv56qr1yx
Could you please, provide back a fork of the above CodeSandbox.io
with the Eiffel Tower
fitted into its parent div
without setting manually a fixed width / height
?您能否提供一个上述CodeSandbox.io
,并将Eiffel Tower
安装到其父div
而无需手动设置固定的width / height
?
Side note: if instead of a portrait
image, we use a landscape
image, it should also fit into its parent div
without going beyond its limits.旁注:如果我们使用landscape
图像而不是portrait
图像,它也应该适合其父div
而不超出其限制。
Example image (size: { width: 800px, height: 300px}
):示例图像(大小: { width: 800px, height: 300px}
像素{ width: 800px, height: 300px}
像素{ width: 800px, height: 300px}
):
https://i.stack.imgur.com/jUeCD.jpg https://i.stack.imgur.com/jUeCD.jpg
Thanks!谢谢!
If I understand your problem correctly, it looks like you are missing a height: 100%
on your .partial-designer-canvas
.如果我正确理解您的问题,看起来您在.partial-designer-canvas
上缺少height: 100%
。
Here, try this:来,试试这个:
https://codesandbox.io/s/x664y69ow https://codesandbox.io/s/x664y69ow
I only changed src/Canvas/Canvas.scss
.我只改变了src/Canvas/Canvas.scss
。 You can add a calc to account for your tool bar too, like so:您也可以添加一个 calc 来说明您的工具栏,如下所示:
.partial-designer-canvas {height: calc(100% - 60px)}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.