html - 没有回流的并排响应图像

标签 html css reflow

想要显示 2 个响应式图像,而不是让浏览器在加载图像时重排屏幕。我目前正在使用此答案的修改版本:responsive-design, image, how to set width/height to avoid reflow on image-load

这是单个响应图像的 fiddle :https://jsfiddle.net/hxraak4u/

HTML

<div class="image-wrapper" style="max-width: 608px; ">
    <div style="padding-bottom: 49.34%; "> <!-- Height / Width -->
        <img src="http://met.live.mediaspanonline.com/assets/31069/example-608web_w608.jpg" style="max-height: 300px;" />
    </div>
</div>

CSS

.image-wrapper {
    margin: 15px auto;
    page-break-inside: avoid;
}
.image-wrapper div {
    position: relative;
    height: 0;
    overflow: hidden;
}
.image-wrapper div img {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

注意:我使用一些内联样式是因为我使用 Razor 为图像创建 HTML 并设置宽度、高度和高度/宽度(底部填充 %)。

我现在正在尝试并排显示 2 个不同高度的图像,同时保留我已经工作的内容。具体来说:

  • 并排显示不同高度的图像
  • 图像在其父 div 中居中
  • 图片显示不大于 100%
  • 如果显示的宽度减小到小于 2 个图像中任何一个的宽度,则让这 2 个图像垂直堆叠
  • 如果显示进一步缩小,则根据需要缩小图像的宽度,同时保持宽高比 - 标准响应式 UI
  • 打印时,不要将图像分页,无论是两张图片还是一张图片,但不要求两张图片放在不同的行时不会分页
  • 加载 HTML 时,根据显示宽度和宽高比为每个图像预留垂直空间 - 防止回流

最佳答案

演示:jsFiddle

Alt: Full Screen

HTML/CSS

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
html { box-sizing: border-box; font: 700 16px/1.5 Consolas; }

*, *:before, *:after { box-sizing: inherit; margin: 0; padding: 0; border: 0; }

.flexWrapper { margin: 2em auto; max-width: 38em; page-break-inside: avoid; display: flex; flex-direction: row; justify-content: center; }

.flex { width: -moz-fit-content; width: -webkit-fit-content; width: fit-content; height: -moz-fit-content; height: -webkit-fit-content; height: fit-content; overflow: none; outline: 3px solid red; flex: 0 1 auto; }

.exampleImg0, .exampleImg1 { max-height: 20em; width: 100%; height: 100%; display: block; }

@media screen and (max-width:37em) {
  .flexWrapper { flex-direction: column; align-items: center; }
}
</style>
</head>

<body>
<div class="flexWrapper">
  <div class="flex">
    <img class="exampleImg0" src="http://met.live.mediaspanonline.com/assets/31069/example-608web_w608.jpg" />
  </div>
  <div class="flex">
    <img class="exampleImg1" src="http://placehold.it/240x320/fff/000.png&text=240x320" />
  </div>
</div>
</body>
</html>
进行了

根本更改,例如:

  • box-sizing

  • reset marginpaddingborder

  • font-size声明于 root

    只是为了了解一切在测量、定位等方面的行为......

重命名元素(我需要使用奇怪的名字来更好地关联事物)

  • .image-wrapper............ .flexWrapper

  • .image-wrapper``div ......... .flex

  • .image-wrapper``div img... .exampleImg0(原始 img) 和 .exampleImg1(已添加 img)

简要说明:

  • .flexWrapper``{ 显示:flex; flex 方向:行;证明内容:居中;... 参见 this `

  • .flex { 宽度:-moz-fit-content;宽度:-webkit-fit-content;宽度:适合内容;高度:-moz-fit-content;高度:-webkit-fit-content;高度:适合内容;溢出:无;...参见 this ...outline: 3px solid red; 只是为了更好地查看 flex 元素... ... flex :0 1 自动; 这允许包装 imgs 的 2 个 div (.flex) 在您建立的标准内表现得非常好。

  • .exampleImg0, .exampleImg1 { 最大高度:20em;宽度:100%; 高度:100%;显示: block ; } 这些属性有助于 imgs 以其父 .flex 为中心并保持响应比率。

  • 一个简单的媒体查询...@media screen and (max-width:37em) { .flexWrapper { flex-direction: column;对齐元素:居中; } } ...所以每当图像缩小时,它们就会堆叠在一起。

关于html - 没有回流的并排响应图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31798607/

相关文章:

html - Bootstrap 响应式导航栏菜单给我带来问题

javascript - 如何确保 JQuery 正确地动态注入(inject) CSS 并准备好使用?

css - ie7 中的导航菜单 css 样式子菜单差距

css - 使用 CSS 将元素与页面对齐

javascript - 强制浏览器立即重新绘制 dom 元素

php - 您的 SQL 语法有误;

jquery - 用 span 元素替换复选框

javascript - 如何使DIV无法聚焦?

javascript - 更多 DOM 元素造成性能问题

javascript - 如果元素的位置是绝对的,浏览器的渲染是否回流?