try 将图像放入可根据屏幕大小改变尺寸的Div框中.这对我来说还是个新手.

我需要这个图像总是保持它的比例,并被裁剪的分区框,可能是垂直或水平.最好是我想要的图像居中的分割框和5px的出血.

或者,是否也可以在图像上设置一个焦点,这样当分割框改变大小时,该焦点就是居中位置?

非常感谢!

这是我目前的代码,在哪里可以改进,这样图像就会被Div裁剪?

body {
  background: white;
  display: flex;
  height: 100vh;
}

div {
  width: calc(100% - 50px);
  height: calc(100% - 50px);
  background: #f0e8e6;
  overflow: hidden;
  margin: auto;
  display: flex;
}

#container {
  max-width: auto;
  overflow: hidden;
}

img {
  width: calc(100% - 5px);
  /* wrong calc? */
  object-fit: contain;
}
<body>

  <div>

    <img src="https://euclois.xyz/desert-statue.jpg">


  </div>

  <style type="text/css">
    html,
    body {
      margin: 0;
      height: 100%;
      overflow: hidden
    }
  </style>

</body>

enter image description here

推荐答案

这将是最好的CSS background.

body {
  background: white;
  display: flex;
  height: 100vh;
}

div {
  width: calc(100% - 50px);
  height: calc(100% - 50px);
  overflow: hidden;
  margin: auto;
  display: flex;
  padding: 5px;
  background-clip: content-box;
  background-color: #f0e8e6;
  background-image: url("https://euclois.xyz/desert-statue.jpg");
  background-origin: border-box;
  background-repeat: no-repeat;
  background-position-x: 60%;
  background-position-y: 35%;
  background-size: cover;
}

#container {
  max-width: auto;
  overflow: hidden;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden
}
<body>

  <div></div>

</body>

Html相关问答推荐

打印pdf时100 vh迪夫有半页高

Chrome中是否有一个本地显示密码功能,用于`input type = password/`?""<"">

如何使div按钮链接到另一个网页

Font Awesome 6插入符号未显示

在 bootstrap 中的弹性项之间添加连接行

将输入字段下方的两个按钮设置为两侧对齐

更改垫子输入的涟漪 colored颜色

窗口视图之外的下拉菜单位置

在将DevExtreme升级到版本23.2之后,到处都会生成一个Pesudo类,我在其中使用了<;DXi-Item Title=&Quot;&Quot;>;

Django中的图像未从静态显示

如果浏览器通过http接收html,为什么客户端内置表单验证不起作用?

在TWebLabel标题中设置换行符/换行符的方法?

如何使用css横向显示英文和中日韩文字?

列表项文本在占据剩余空间之前在标记下换行

为什么一个 CSS 网格框比其他网格框低?

页面文件夹内的 HTML 页面未加载主目录的 style.css

为什么当我按第三个按钮时,前两个按钮会跳动?

如何使用 Bootstrap Grid 系统修复大屏幕上图片和进度条之间的空白?

将表格标题和过滤器调整到表格的顶部边缘

我无法设置使用 Tailwind CSS 创建的仪表板的网格 struct