简体   繁体   English

根据图像纵横比匹配按钮高度

[英]Match button height based on image aspect ratio

I have a group of 16x9 images that are set to a percentage width (20%) with an add button thrown into the mix.我有一组 16x9 图像,它们设置为百分比宽度 (20%),并在其中添加了一个添加按钮。 I need the height of the add button to equal that of the images.我需要添加按钮的高度等于图像的高度。 I've tried setting the "height" of the button using padding-top and I've set it to 11.25% which is 9/16 of 20%.我尝试使用padding-top设置按钮的“高度”,并将其设置为 11.25%,即 20% 的 9/16。

This almost works in Chrome but in the example, when you resize the window (in fullscreen), sub-pixel rendering kicks in and renders a height greater for the button than the image resulting in wrapping issues.这几乎适用于 Chrome,但在示例中,当您调整窗口大小(全屏)时,子像素渲染开始并渲染按钮的高度大于图像,从而导致环绕问题。

Is there a better way of achieving this using flexbox perhaps?是否有更好的方法使用 flexbox 来实现这一点? one that will work without sub-pixel rendering issues?一种可以在没有亚像素渲染问题的情况下工作的吗?

 .imageSection { box-sizing: border-box; overflow: hidden; } .imageSection > img, .imageSection > button { width: 20%; float: left; box-sizing: border-box; } .imageSection > button { padding: 0; border: 0; margin: 0; padding-top: 11.25%; /* (20 / 16) * 9 */ position: relative; background: #bd2a72; color: #fff; } .imageSection > button::after { content: "+"; position: absolute; width: 30px; height: 14px; top: 50%; left: 50%; margin-left: -15px; margin-top: -7px; }
 <div class="imageSection"> <button></button> <img src="http://placehold.it/1600x900" /> <img src="http://placehold.it/1600x900" /> <img src="http://placehold.it/1600x900" /> <img src="http://placehold.it/1600x900" /> <img src="http://placehold.it/1600x900" /> <img src="http://placehold.it/1600x900" /> </div>

What about this?那这个呢?

 .imageSection { box-sizing: border-box; overflow: hidden; display: flex; align-items: stretch; position: relative; flex-wrap:wrap; } .imageSection > img, .imageSection > button { width: 20%; box-sizing: border-box; height:100%; } .imageSection > button { padding: 0; border: 0; margin: 0; position: relative; background: #bd2a72; color: #fff; height:inherit; } .imageSection > button::after { content: "+"; position: absolute; width: 30px; height: 14px; top: 50%; left: 50%; margin-left: -15px; margin-top: -7px; }
 <div class="imageSection"> <button></button> <img src="http://placehold.it/1600x900" /> <img src="http://placehold.it/1600x900" /> <img src="http://placehold.it/1600x900" /> <img src="http://placehold.it/1600x900" /> <img src="http://placehold.it/1600x900" /> <img src="http://placehold.it/1600x900" /> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM