图片超出了其容器

huangapple go评论84阅读模式
英文:

Img overflows its container

问题

我正在创建一个对话框,它应该占据整个屏幕的宽度和高度。
它的内容应该分为两部分 - 图像和页脚区域。页脚区域应该有2行高,图像区域应该占据父元素的剩余空间。
我尝试使用tailwindcss来实现这个效果,但是图像溢出了其容器,并占据了整个对话框的高度。当我将图像注释掉,并在其中放置一个具有相同类名w-full h-full<div>时,它正好占据了我想要的空间。如何使用<img>标签实现相同的效果?

我还尝试了删除h-full,使用h-auto等方法。

非常感谢您的帮助。

英文:

I'm creating a dialog that should take full width and height of the screen.
Its content should be divided to two parts - image and footer area. Footer area should be 2 rows high and the image area should take the rest of the parent.
Using tailwindcss I tried to do this, however the image overflows its container and takes height of the whole dialog. When I commented the image out and put a &lt;div&gt; in there with same class w-full h-full it took exactly the space I wanted. How to achieve the same with the &lt;img&gt; tag?

&lt;dialog
    bind:this={dialog}
    class=&quot;w-screen h-screen rounded-xl bg-surface-50 mx-auto my-auto overflow-hidden border-0&quot; tabindex=&quot;-1&quot;
    onclick=&quot;event.target==this &amp;&amp; this.close()&quot;
&gt;
    &lt;div class=&quot;w-full h-full flex flex-col relative border-0&quot; tabindex=&quot;-1&quot;&gt;
        &lt;div class=&quot;flex-grow&quot;&gt;
            &lt;img src={image?.s3_path} alt=&quot;fotka&quot; class=&quot;object-contain h-full w-full&quot;&gt;
            &lt;!-- &lt;div class=&quot;w-full h-full bg-primary-400&quot;&gt;&lt;/div&gt; --&gt;
        &lt;/div&gt;
        &lt;div class=&quot;w-full h-12&quot;&gt;
            x
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/dialog&gt;

I also tried fiddling around removing the h-full, using h-auto and so on.

Thank you very much for any help.

答案1

得分: 1

为了确保图像在对话框中占据剩余空间并且不溢出,您可以使用Tailwind CSS中的flex和max-height工具的组合。

以下是您更新后的代码:

<dialog
    bind:this={dialog}
    class="w-screen h-screen rounded-xl bg-surface-50 mx-auto my-auto overflow-hidden border-0" tabindex="-1"
    onclick="event.target==this && this.close()"
>
    <div class="w-full h-full flex flex-col relative border-0" tabindex="-1">
        <div class="flex-grow overflow-hidden">
            <img src={image?.s3_path} alt="fotka" class="object-contain w-full max-h-full">
            <!-- <div class="w-full h-full bg-primary-400"></div> -->
        </div>
        <div class="w-full h-12">
            x
        </div>
    </div>
</dialog>

我在<img>标签的class中将h-full替换为max-h-full。这确保图像将占据其容器的整个宽度(w-full),同时保持其纵横比并且不溢出。父级<div>上的overflow-hidden类也有助于将图像限制在其边界内。

英文:

To make sure the image takes the remaining space within the dialog and doesn't overflow, you can use a combination of flex and max-height utilities from Tailwind CSS.

Here is your updated code:

&lt;dialog
    bind:this={dialog}
    class=&quot;w-screen h-screen rounded-xl bg-surface-50 mx-auto my-auto overflow-hidden border-0&quot; tabindex=&quot;-1&quot;
    onclick=&quot;event.target==this &amp;&amp; this.close()&quot;
&gt;
    &lt;div class=&quot;w-full h-full flex flex-col relative border-0&quot; tabindex=&quot;-1&quot;&gt;
        &lt;div class=&quot;flex-grow overflow-hidden&quot;&gt;
            &lt;img src={image?.s3_path} alt=&quot;fotka&quot; class=&quot;object-contain w-full max-h-full&quot;&gt;
            &lt;!-- &lt;div class=&quot;w-full h-full bg-primary-400&quot;&gt;&lt;/div&gt; --&gt;
        &lt;/div&gt;
        &lt;div class=&quot;w-full h-12&quot;&gt;
            x
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/dialog&gt;

I replaced h-full with max-h-full in the <img> tag's class. This ensures that the image will take up the full width of its container (w-full) while respecting its aspect ratio and not overflowing. The overflow-hidden class on the parent <div> also helps in containing the image within its boundaries.

答案2

得分: 1

尝试将h-full替换为您想要的图像容器的特定高度,并设置object-contain类。

可能像这样:

<dialog
    bind:this={dialog}
    class="w-screen h-screen rounded-xl bg-surface-50 mx-auto my-auto overflow-hidden border-0" tabindex="-1"
    onclick="event.target==this &amp;&amp; this.close()">
    <div class="w-full h-full flex flex-col relative border-0" tabindex="-1">
        <div class="flex-grow relative">
            <img src={image?.s3_path} alt="fotka" class="absolute inset-0 object-contain w-full h-full">
        </div>
        <div class="w-full h-12">
            x
        </div>
    </div>
</dialog>

在这里,我将包含图像的div设置为相对定位,将图像设置为绝对定位,以填充整个父级div。我还保持了object-contain类,以保持图像的纵横比。

英文:

Try to replace the h-full with a specific height that you want for the image container, and set the object-contain class.

Maybe like this?

&lt;dialog
    bind:this={dialog}
    class=&quot;w-screen h-screen rounded-xl bg-surface-50 mx-auto my-auto overflow-hidden border-0&quot; tabindex=&quot;-1&quot;
    onclick=&quot;event.target==this &amp;&amp; this.close()&quot;&gt;
    &lt;div class=&quot;w-full h-full flex flex-col relative border-0&quot; tabindex=&quot;-1&quot;&gt;
        &lt;div class=&quot;flex-grow relative&quot;&gt;
            &lt;img src={image?.s3_path} alt=&quot;fotka&quot; class=&quot;absolute inset-0 object-contain w-full h-full&quot;&gt;
        &lt;/div&gt;
        &lt;div class=&quot;w-full h-12&quot;&gt;
            x
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/dialog&gt;

Here I made the div containing the image relative, and the image absolute so that it fills the entire parent div. I also maintained the object-contain class to keep the aspect ratio of the image.

huangapple
  • 本文由 发表于 2023年8月8日 20:50:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76859740.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定