html – 使元素的宽度与动态大小的图像相同?
我有一个灵敏的幻灯片式布局,每个图像下方都有字幕。
我试图获得与图像宽度相同的标题。问题是图像被缩放以适应垂直浏览器,我的标题是在缩放之前获取图像的宽度。 Fiddle #big_container { display:block; position:relative; width:100%; padding-bottom:40%; white-space:nowrap; overflow-x:scroll; overflow-y:hidden; } #big_container>div { position:absolute; top:0; right:0; bottom:0; left:0; } .little_container { display:inline-block; height:100%; width:100%; text-align:center; } #big_container figure { display:inline-block; height:100%; margin:0; } figure img { max-height:calc(100% - 40px); /* subtract height of caption */ } figcaption { display:block; width:100%; text-align:left; box-sizing:border-box; margin:0; padding:10px; line-height:20px; background-color:#ddd; } <div id="big_container"> <div> <div class="little_container"> <figure> <img src="http://placekitten.com/500/440"> <figcaption> have a kitty!!1 </figcaption> </figure> </div> <div class="little_container"> <figure> <img src="http://placekitten.com/450/400"> <figcaption> moar kitty! </figcaption> </figure> </div> <div class="little_container"> <figure> <img src="http://placekitten.com/300/440"> <figcaption> too many kitty.. </figcaption> </figure> </div> </div> </div> 如何根据流体图像的宽度进行缩放? 更新 为了清楚起见,以下简要概述我的具体要求: >标题元素的宽度必须与图像的宽度相同(可以将标题文字左右对齐到图像的边缘) 可以更改html标记。 解决方法
更新
根据您为此问题设置的确切要求,它不能仅用CSS解决。 这是我能够想出来的最好的。 Fiddle demo 1(固定高度为文字,图像完全可见) 我主要使用的技巧是拥有一个隐藏的img来弥补空间,然后是一个背景图像,以保持比例缩放到最大宽度/高度。 为了方便,我添加了内联样式的背景图像,因此可以在HTML内部处理内容。 为了使其完美,需要一个脚本,计算标题的内容并调整图像/标题的缩小/高度。 片段演示1 html,body { margin: 0; white-space: nowrap; overflow-y: hidden; } .container { display: inline-block; white-space: normal; width: 100%; } .wrap { margin: 0 auto; display: table; } .image { display: table-cell; background-position: center bottom; background-repeat: no-repeat; background-size: contain; } .image img { visibility: hidden; max-width: 100vw; min-width: 100%; height: calc(100vh - 80px); } .caption { display: table-caption; caption-side: bottom; height: 40px; line-height: 22px; padding: 8px; background-color: #ddd; overflow: hidden; } .right { text-align: right; } <div class="container"> <div class="wrap"> <div class="image" style="background-image: url('http://placekitten.com/450/300')"> <img src="http://placekitten.com/450/300"> </div> <div class="caption right"> moar kitty! moar kitty! moar kitty! </div> </div> </div> <div class="container"> <div class="wrap"> <div class="image" style="background-image: url('http://placekitten.com/500/440')"> <img src="http://placekitten.com/500/440"> </div> <div class="caption"> have a kitty!!1 have a kitty!!1 have a kitty!!1 have a kitty!!1 have a kitty!!1 have a kitty!!1 have a kitty!!1 </div> </div> </div> <div class="container"> <div class="wrap"> <div class="image" style="background-image: url('http://placekitten.com/300/440')"> <img src="http://placekitten.com/300/440"> </div> <div class="caption"> too many kitty.. too many kitty.. too many kitty.. </div> </div> </div> 片段演示2 html,body { margin: 0; white-space: nowrap; overflow-y: hidden; } .container { position: absolute; height: 100%; display: inline-block; white-space: normal; width: 100%; background: white; opacity: 0; } .wrap { margin: 0 auto; display: table; } .image { display: table-cell; background-position: center bottom; background-repeat: no-repeat; background-size: contain; } .image img { visibility: hidden; max-width: 100vw; min-width: 100%; height: 100vh; } .caption-wrap { display: table-caption; caption-side: bottom; position: relative; } .caption { position: absolute; left: 0; right: 0; bottom: 100%; height: auto; line-height: 22px; padding: 8px; background-color: rgba(0,0.6); color: white; } .right { text-align: right; } .center { text-align: center; } .container:nth-child(3) { animation: xfade 12s 0s infinite; } .container:nth-child(2) { animation: xfade 12s 4s infinite; } .container:nth-child(1) { animation: xfade 12s 8s infinite; } @keyframes xfade{ 17% { opacity:1; } 45% { opacity:0; } 92% { opacity:0; } } <div class="container"> <div class="wrap"> <div class="image" style="background-image: url('http://placekitten.com/450/300')"> <img src="http://placekitten.com/450/300"> </div> <div class="caption-wrap"> <div class="caption right"> moar kitty! text .. right aligned </div> </div> </div> </div> <div class="container"> <div class="wrap"> <div class="image" style="background-image: url('http://placekitten.com/500/440')"> <img src="http://placekitten.com/500/440"> </div> <div class="caption-wrap"> <div class="caption"> have a kitty!!1 have a kitty!!1 text .. left aligned </div> </div> </div> </div> <div class="container"> <div class="wrap"> <div class="image" style="background-image: url('http://placekitten.com/300/440')"> <img src="http://placekitten.com/300/440"> </div> <div class="caption-wrap"> <div class="caption center"> text .. centered </div> </div> </div> </div> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |