棘手的CSS:在没有直接容器的情况下裁剪图像

Tricky CSS : crop image without direct container

本文关键字:裁剪 图像 情况下 CSS      更新时间:2023-09-26

我目前正在为一个棘手的CSS问题而苦苦挣扎,我无法解决。我想构建一个 img(两行上的二乘二)控制器,它可以显示:

  • 左上角的全尺寸图像,
  • 溢出-X的右上角:滚动(或自动等),
  • 溢出-y中的左下角:滚动(...),
  • 右下角两者兼而有之。

有点像Excel修复列/行功能。

我的网页 :

    <div id="global">
        <div id="feature-container">
            <div id="feature-head">
                <img id="feature-t-l" src="..." alt="..."/>
                <img id="feature-t-l" src="..." alt="..."/>
            </div>
            <div id="feature-body">
                <img id="feature-b-l" src="..." alt="..."/>
                <img id="feature-b-r" src="..." alt="..."/>
            </div>
        </div>
    </div>

我的 CSS :

    html {
      height: 100%;
    }
    html body {
      height: 100%;
      margin: 0 8px 0 8px;
      background-color: darkgrey;
    }
    html body h1 {
      height: 10%;
      width: 100%;
      margin: 0;
      padding-top: 2%;
      box-sizing: border-box;
      text-align: center;
    }
    html body #global {
      height: 90%;
      width: 100%;
      max-width: 100%;
      padding: 0% 2.5% 5% 2.5%;
      box-sizing: border-box;
    }
    html body #global #feature-container {
      height: 100%;
      width: 100%;
      background-color: white;
    }
    html body #global #feature-container #feature-header {
      width: 100%;
      white-space: nowrap;
    }
    html body #global #feature-container #feature-header > img {
      vertical-align: bottom;
    }
    html body #global #feature-container #feature-header img#feature-t-l {
      overflow-x: scroll;
    }
    html body #global #feature-container #feature-body {
      width: 100%;
      white-space: nowrap;
    }

目前,左上角的 img 未裁剪。即使最大尺寸#global,左上角的溢出 x。可以在不使用 px 的情况下解决此问题?

编辑:感谢Fausto NA,一个JSFiddle。在我的项目中,两个顶部 img 在同一条线上,头部和身体之间没有空白。

简短的回答:不。正如这篇文章所解释的,您需要设置 DIV 的宽度或高度,否则它将自动调整为其 IMG 子级的大小。此外,如果您希望每个 IMG 都有自己的滚动,则需要在每个 IMG 周围放置一个滚动 DIV。

长答案:你应该回顾一下你对CSS的理解。 html body #global #feature-container #feature-container的意思是"将这种风格应用于另一个#feature-container的间接子级的#feature-container,这是#global的间接子级,这是一个间接的......而且你没有#feature-container那是另一个#feature-container的孩子.还有其他 CSS 规则与 HTML 中的任何内容都不匹配,例如 img#feature-container

结论:这是怎么回事?

https://jsfiddle.net/xve5jakh/1/