[英]Make flexbox items the same height
我正在学习如何使用 Flexbox,但我无法让我的盒子做我想做的事。 我想:
我面临的主要问题是,当一个文本比其他文本多时,这些框的大小不同。 尽管父元素具有 align-items:stretch 属性,但我认为应该纠正该属性,但仍会发生这种情况。
谢谢你的帮助。 这是我的代码:
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
margin: 0;
padding: 0;
font-size: 30px;
}
.parent {
margin: 0;
padding: 0;
display: flex;
justify-content: space-between;
align-items: stretch;
gap: 5px;
}
.parent li {
list-style: none;
background: silver;
padding: 100px 10px;
text-align: center;
flex: 1 0 25%;
margin: auto;
box-sizing: border-box;
}
.parent li a {
margin: auto;
padding: 100px 10px;
text-decoration: none;
color: #000000;
}
</style>
</head>
<body>
<ul class="parent">
<li><a href="#">One</a></li>
<li><a href="#">Two</a></li>
<li><a href="#">This is box number three, and it has a lot more content than the others.</a></li>
<li><a href="#">Four</a></li>
</ul>
</body>
</html>
我在那里添加了随机文本,以确保它会增加高度,并且仍然以文本为中心。
所以基本上,我所做的就是使用你所做的和a
标签上的另一个 flexbox,使其居中,无论高度如何。
body { margin: 0; padding: 0; font-size: 30px; } .parent { margin: 0; padding: 0; gap: 5px; display: flex; align-items: stretch; flex-wrap: wrap; } .parent li { list-style: none; background: silver; padding: 100px 10px; text-align: center; flex: 1; margin: auto; box-sizing: border-box; padding: 5px; margin: 5px; } .parent li a { margin: auto; text-decoration: none; color: #000000; display: flex; justify-content: center; align-items: center; height: 100%; }
<head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <ul class="parent"> <li><a href="#">One</a></li> <li><a href="#">Two</a></li> <li><a href="#">This is box number three, and it has a sjdsddsd lot more content thddd dsdssdd dssd sdsdsddddddan the others.</a></li> <li><a href="#">Four</a></li> </ul> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.