这个问题可能看起来微不足道,但有3年的前端开发经验,我在这方面失败了.我创造了一个最小的可重现的例子.

这是我的示例代码.要查看它,我建议创建一个html文件并使用iPhone SE屏幕尺寸(375x667)在浏览器中查看代码,因为这样y溢出就很明显了.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Image and Div Layout</title>
        <style>
            body {
                margin: 0;
            }

            .container {
                max-height: 100vh;
            }

            .container img {
                max-width: 100%;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <img src="https://wallpapercave.com/dwp2x/wp4471355.jpg" alt="Image">
            <div>
                <p>1Your content here</p>
                <p>2Your content here</p>
                <p>3Your content here</p>
                <p>4Your content here</p>
                <p>5Your content here</p>
                <p>6Your content here</p>
                <p>7Your content here</p>
                <p>8Your content here</p>
                <p>9Your content here</p>
                <p>10Your content here</p>
                <p>11Your content here</p>
                <p>AAAAAAAAAAAAAAAAA</p>
            </div>
        </div>
    </body>
</html>

在这张图中,你可以看到唯一的数字 8不再是可见的,但不是9,10,11…

enter image description here

我正在笔记本电脑上的谷歌Chrome开发工具中测试iPhone SE的大小.

我总是希望能看到S所有的<p>米,永远不会有任何的y-溢出.如果没有足够的空间,那么<img>元素应该会收缩.

因此,<p>AAAAAAAAAAAAAAAAA</p>元素应该位于页面的最底部.然后向上看剩下的<p>个元素,最后图像应该会占据剩余的空间.

<img>应该缩小,因为它的高度和宽度应该按照正确的比例变小.关键词:object-fit: contain(但对我不起作用)

图像也应该水平居中&垂直居中在其"剩余空间容器"内.

如果可能的话,我更喜欢不使用任何视窗单位的解决方案,因为在我的实际用例中,页面上还有其他元素,这将与之冲突.因此,解决方案应该依赖于当前可用的高度,在本例中为100vh,但实际上可能并不总是如此.

但如果使用视窗真的是only解决方案(这将是非常令人惊讶的),那么它也可以使用.

我试着用height: 100vh和Flexbox、Flex-Growth、GRID做一些事情,但它们都不起作用.

推荐答案

尝尝这个

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Image and Div Layout</title>
    <style>
        body {
            margin: 0;
        }

        .container {
            height: 100vh;
            display: flex;
            flex-direction: column;
            overflow-y: hidden;
        }

        .img-container {
            flex: 1 1 auto;
            height: 100%;
            background-color: #550000;
            display: flex;
            justify-content: center;
            overflow-y: hidden;
        }

        .img-container img {
            object-fit: contain;
            max-width: 100%;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="img-container">
            <img src="https://wallpapercave.com/dwp2x/wp4471355.jpg" alt="Image">
        </div>

        <div>
            <p>1Your content here</p>
            <p>2Your content here</p>
            <p>3Your content here</p>
            <p>4Your content here</p>
            <p>5Your content here</p>
            <p>6Your content here</p>
            <p>7Your content here</p>
            <p>8Your content here</p>
            <p>9Your content here</p>
            <p>10Your content here</p>
            <p>11Your content here</p>
            <p>AAAAAAAAAAAAAAAAA</p>
        </div>
    </div>
</body>

</html>

Html相关问答推荐

在Apps Script中连接CSS与HTML

如何使用wai-aria标签访问相关的复选框?

悬停时跳转的内容

应用于文本而不是弹性容器的Flexbox属性

如何收集<;p&>元素下的<;p>;子元素

当溢出到滚动条中时, bootstrap 按钮样式会更改

我怎样才能在我的网站上制作花哨的角落

如何使用CSS创建文件夹选项卡的形状?

带下划线的文本应该有延长的下划线,以使其图像悬停

如何在ASP.NET Core中添加换行符

根据Tailwind CSS中的子计数而非子宽度进行对齐,并水平对齐

悬停表格单元格文本时,使元素的字体大小更大,同时保持表格单元格的高度不变

HTML 邮箱在 Gmail 中无法正确显示

即使必填字段为空,HTML 表单也已成功提交

使用简单的 HTML 设计公司徽标和文本

如何将内容放置在侧边栏旁边?

样式化单选按钮 背景 colored颜色

如何将两个平行 div 的页面内的表格居中?

如何将元素均匀分布到容器的边缘?

CSS Padding 将右对齐的对象推离页面