CSS - 使所有元素与最高元素的高度相同 [英] CSS - make all elements the same height as the tallest element
问题描述
我有一个问题,我在一个 div 中有三个浮动按钮.其中一个按钮的内容比其他按钮多,因此它更高.
我希望能够使所有按钮与最高按钮的高度相同.我试过 height: 100%;
但这没有用.
<头><title></title><meta charset="utf-8"/><style type="text/css">.容器 {宽度:320px;}.容器按钮{向左飘浮;背景:#FFFFFF;边框:1px 实心#CCCCCC;宽度:33.33%;}</风格>头部><身体><div class="容器"><按钮><span class="big-text">好的</span><span class="little-text">123</span>按钮><按钮><span class="big-text">另一个选项</span><span class="little-text">456</span>按钮><按钮><span class="big-text">不!</span><span class="little-text">789</span>按钮>
</html>
https://jsfiddle.net/xpdxyaz6/1/
只需将 display:flex
添加到您的容器类中,如下所示:
.container {宽度:320px;显示:弹性;}
jsFiddle:https://jsfiddle.net/AndrewL32/xpdxyaz6/2/
注意:
截至 2021 年 5 月,浏览器的 Flex
属性兼容性如下:
Google Chrome 部分支持前缀 v4 - v20 |完全支持前缀v21 - v28 |完全支持v29+
Mozilla Firefox 部分支持前缀v2 - v21 |部分支持v22 - v27 |完全支持v28+
Internet Explorer 部分支持前缀 v10 |部分支持前缀 v11+
Safari 部分支持前缀 v3.1 - v6 |完全支持前缀v6.1 - v8 |完全支持v9+
Edge 完全支持 v12+
I have a problem where I have three floating buttons in a div. one of the buttons has more content that the others, so it's taller.
I want to be able to make all buttons the same height as the tallest button. I tried height: 100%;
but that didn't work.
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<style type="text/css">
.container {
width: 320px;
}
.container button {
float: left;
background: #FFFFFF;
border: 1px solid #CCCCCC;
width: 33.33%;
}
</style>
</head>
<body>
<div class="container">
<button>
<span class="big-text">Okay</span>
<span class="little-text">123</span>
</button>
<button>
<span class="big-text">Another Option</span>
<span class="little-text">456</span>
</button>
<button>
<span class="big-text">Nah!</span>
<span class="little-text">789</span>
</button>
</div>
</body>
</html>
https://jsfiddle.net/xpdxyaz6/1/
Just add display:flex
to your container class like this:
.container {
width: 320px;
display:flex;
}
jsFiddle: https://jsfiddle.net/AndrewL32/xpdxyaz6/2/
NOTE:
As of May 2021, the Flex
property compatibility for browsers are as follows:
Google Chrome Partial support with prefix v4 - v20 | Full support with prefix v21 - v28 | Full support v29+
Mozilla Firefox Partial support with prefix v2 - v21 | Partial support v22 - v27 | Full Support v28+
Internet Explorer Partial support with prefix v10 | Partial support with prefix v11+
Safari Partial support with prefix v3.1 - v6 | Full support with prefix v6.1 - v8 | Full Support v9+
Edge Full support v12+
这篇关于CSS - 使所有元素与最高元素的高度相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!