html - .container > .wrapper > img : maximize img, 但不大于 .container(祖 parent ) - 怎么样?

标签 html image css

我有 .container > .wrapper > img。这是一个任务:

  1. img 必须具有可能的最大宽度/高度,保持纵横比,100% 可见且不超过 .container 的大小。
  2. 图片不得超过其自然尺寸。
  3. 图片的宽度/高度未知。
  4. .container 已知具有固定的(以像素为单位)宽度/高度,但确切尺寸未知。
  5. .wrapper 必须紧密贴合 img(必须与图像具有相同的宽度和高度)。 Wrapper 是一种特殊元素,用于将内容放在图像上,例如。 G。徽章。我在代码片段中添加了示例标签来演示这一点。这在某种程度上应该是可能的。

标记:

<div class="container">
  <div class="wrapper">
    <div class="label">new!</div>
    <img src="https://via.placeholder.com/150x300">
  </div>
</div>

我想我可以使用 display: block; max-height: 100% 对于 img,但它 does not 工作,因为 .wrapper(图像父级)高度不固定,它不能'不固定 - 参见第 5 点。

我还可以做些什么来使用纯 CSS 完成所描述的任务?我更喜欢在 IE11 中工作的解决方案,但其他人也会受到赞赏。

编辑:容器和图像可以是任意大小非常重要。我在代码段中添加了设置以测试不同尺寸。

如果图像大于容器,则渲染时不应大于容器。

如果图像小于容器,则渲染时不应大于其自然大小。

它应该适用于水平容器/垂直图像和垂直容器/水平图像。

编辑 2:.wrapper 只是一个“讨厌的干扰”元素,这也非常重要。它是功能性的:wrapper 用于在其中的图像上放置绝对定位的内容(例如标签、徽章),它必须支持转换(镜像、翻译)、css 过滤器等,一般来说——我们通常用 block 元素做的所有事情.

Playground :

$(function() {
  $('input[name=container-width]').on('change', function() {
    $('.container').css('width', $(this).val() + 'px')
  })

  $('input[name=container-height]').on('change', function() {
    $('.container').css('height', $(this).val() + 'px')
  })

  $('input[name=image-width]').on('change', function() {
    var width = $(this).val()
    var height = $('input[name=image-height]').val()
    $('img')[0].src = 'http://via.placeholder.com/' + width + 'x' + height
  })
  
  $('input[name=image-height]').on('change', function() {
    var height = $(this).val()
    var width = $('input[name=image-width]').val()
    $('img')[0].src = 'http://via.placeholder.com/' + width + 'x' + height
  })

})
.container {
  width: 200px; /* can have any value in pixels */
  height: 200px; /* can have any value in pixels */
  background-color: green;
}

.wrapper {
  outline: 1px solid yellow;
  position: relative; /* for label */
}

.label {
  position: absolute;
  background-color: blue;
  color: white;
}

.label.-top-left {
  top: 10px;
  left: 10px;
}

.label.-bottom-right {
  bottom: 10px;
  right: 10px;
}
<h2>Settings</h2>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Container: width <input type="number" step="10" name="container-width" value="200"> height <input type="number" step="10" name="container-height" value="200">
<br>
Image: width <input type="number" step="10" name="image-width" value="150"> height <input type="number" step="10" name="image-height" value="300">
<br>
<br>

<h2>Demo</h2> 

<div class="container">
  <div class="wrapper">
    <div class="label -top-left">new!</div>
    <div class="label -bottom-right">good!</div>
    <img src="http://via.placeholder.com/150x300">
  </div>
</div>

<br>
<br>
<br>
<br>
<br>
<br>
<br>

<h2>How it should look like</h2>

<p>This is not the solution, because is has many hardcoded dimensions. It's just a visual demo of what I want to achieve.</p>

<div style="width: 200px; height: 200px;background-color: green">
  <div style="width: 100px; height: 200px; position: relative;outline: 1px solid yellow">
    <div style="position: absolute;
  top: 10px;
  left: 10px;
  background-color: blue;
  color: white;">new!</div>
      <div style="position: absolute;
  bottom: 10px;
  right: 10px;
  background-color: blue;
  color: white;">good!</div>
    <img style="max-width: 100%; max-height: 100%;" src="http://via.placeholder.com/150x300">
  </div>
</div>

最佳答案

这对你有用吗?

.container {
  width: 200px;
  /* can have any value in pixels */
  height: 200px;
  /* can have any value in pixels */
  background-color: green;
}

.container-2 {
  width: 300px;
  height: 300px;
}

.wrapper {
  outline: 1px solid yellow;
  display: inline;
  height: inherit;
}

img {
  width: auto;
  max-height: 100%;
  display: inline-block;
  vertical-align: bottom;
}
<div class="container">
  <div class="wrapper">
    <img src="http://via.placeholder.com/150x300">
  </div>
</div>
<br>
<div class="container container-2">
  <div class="wrapper">
    <img src="http://via.placeholder.com/30">
  </div>
</div>

关于html - .container > .wrapper > img : maximize img, 但不大于 .container(祖 parent ) - 怎么样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50286432/

相关文章:

html - CSS 动画不透明度 (0) 回到 1

html - 调整大小时如何使四个按钮保持在同一行?

javascript - 如果在输入框中输入特定值,则使用 javascript 显示一条消息

javascript - 单选按钮作为图片

html - 悬停时覆盖的背景

php - 检查元素的数量是否等于特定数量 PHP

java - 如何使用引用 RGB 值对 RGB 值进行归一化

java - 调整 .GIF 图像的大小

html - Internet Explorer 定位不正确

html - 如何在 ionic 中获取 ionic 复选框的值?