简体   繁体   English

根据列表中最大的高度/宽度调整所有项目的大小

[英]Resize all items based on biggest height/width in a list

I have this list 我有这个清单

<ul>
    <li><img src="https://via.placeholder.com/650x100"></li>
    <li><img src="https://via.placeholder.com/150x350"></li>
    <li><img src="https://via.placeholder.com/100"></li>
    <li><img src="https://via.placeholder.com/350x350"></li>
</ul>

with each li a different width and height. 每个li的宽度和高度都不同。 I need to render this as follows: 我需要渲染如下:

1) Each li needs to have the width of the widest li
2) Each li needs to have the height of the tallest li 
3) There is a max-width and max-height for li

So, with Flexbox I can easily satisfy 1) demo But I cannot satisfy width and height with css. 因此,使用Flexbox我可以轻松满足1) 演示,但是我不能满足CSS的宽度和高度。 What would be the preferred solution here. 什么是这里的首选解决方案。 Is this even possible without javascript ? 没有javascript甚至可能吗?

UPDATE: I've implemented the suggestions here 更新:我已经在这里实施了建议

My preferred solution would be to use Javscript, I don' know how you'd do it with CSS alone. 我首选的解决方案是使用Javscript,我不知道您如何单独使用CSS。 Something like this, you should make sure that the images are loaded; 像这样的事情,您应该确保已加载图像。

 const forEach = Function.bind.call(Function.call, Array.prototype.forEach); const reduce = Function.bind.call(Function.call, Array.prototype.reduce); const sizeProperties = ['height', 'width']; const images = document.querySelectorAll('ul>li>img'); const size = reduce(images, (dimensions, image) => { const dimension = window.getComputedStyle(image); sizeProperties.forEach((property) => { dimensions[property] = Math.max( dimensions[property], Number.parseInt(dimension[property], 10), ); }); return dimensions; }, { width: 0, height: 0, }); forEach(images, (image) => { sizeProperties.forEach((property) => { image.parentNode.style[property] = `${size[property]}px`; }); }) 
 li { border-style: solid; border-width: 1px; list-style: none; } 
 <ul> <li><img src="https://via.placeholder.com/650x100"></li> <li><img src="https://via.placeholder.com/150x350"></li> <li><img src="https://via.placeholder.com/100"></li> <li><img src="https://via.placeholder.com/350x350"></li> </ul> 

There's no way to retrieve an unknown height and store it as a variable with CSS even using a preprocessor like SASS , so there's no way to do #2 with CSS alone. 即使使用像SASS这样的预处理器,也无法检索未知高度并将其存储为CSS变量,因此无法单独对CSS进行#2处理。 I realize you're hoping for something purely with stylesheets, but I'm including a clean example of how you can achieve both #1 and #2 with a combination of CSS and jQuery . 我意识到您只是希望使用样式表,但是我提供了一个干净的示例,说明如何结合CSSjQuery来实现#1和#2。

I hope this helps! 我希望这有帮助!

 var tallest; $('li').each(function() { tallest = tallest > $(this).height() ? tallest : $(this).height(); }); $('li').each(function() { $(this).height(tallest); }); 
 li { border: 1px solid gray; width: 100%; list-style-type: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li><img src="https://via.placeholder.com/650x100"></li> <li><img src="https://via.placeholder.com/150x350"></li> <li><img src="https://via.placeholder.com/100"></li> <li><img src="https://via.placeholder.com/350x350"></li> </ul> 

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

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