简体   繁体   English

在div上设置最小高度,但使所有div的高度相同

[英]set min-height on div but make all divs same height

I am using this CSS code: 我正在使用此CSS代码:

<style type="text/css">
html,body {
    font-family:Arial;
}
.container {
    text-align:center;
}
.box {
    width:350px;
    display: inline-block;
    margin:10px 20px 0 auto;
    padding:10px;
    border:1px solid black;
    min-height:60px;
    font-size:60px;
    text-align: center;
}
.box h2 {
    font-size:40px;
    text-align:center;
}
</style>

but i want the divs to all have the same height 但我希望div的高度都相同

i have created a fiddle here: http://jsfiddle.net/UDm3a/ 我在这里创建了一个小提琴: http : //jsfiddle.net/UDm3a/

Use height, instead of min-height. 使用高度,而不是最小高度。 eg: 例如:

height: 220px;

To get the same offsets and heights, you need to float them and set the height. 要获得相同的偏移量和高度,您需要将它们浮动并设置高度。

float: left;
height: 300px;

Like the fiddle I gave you in the comments: http://jsfiddle.net/UDm3a/2/ 就像我在评论中给您提供的小提琴一样: http : //jsfiddle.net/UDm3a/2/

The other option is to use vertical-align: top; 另一种选择是使用vertical-align: top; along with height, so: 以及高度,所以:

vertical-align: top;
height: 300px;

See that here: http://jsfiddle.net/UDm3a/3/ 在这里看到: http : //jsfiddle.net/UDm3a/3/

If you want to set the height dynamically, I'm afraid you'll have to use JavaScript to find the highest on and apply its height to the others. 如果要动态设置高度,恐怕您将不得不使用JavaScript查找最高高度并将其高度应用于其他高度。

You can do this by decreasing the font-size and setting the min-width and max-width as same. 您可以通过减小font-size并将min-widthmax-width为相同来实现。 Even though setting min-height might not be required but it worked for me. 即使可能不需要设置min-height但它对我也有用。

CSS
html,body {
font-family:Arial;
}
.container {
text-align:center;
}
.box {
    display: inline-block;
padding:10px;
border:1px solid black;
text-align: center;
    font-size: 100%;
    padding: 10px;
    min-width: 300px;
    max-width: 300px;
}
.box h2 {
font-size:100%;
text-align:center;
}

You can see the fiddle here http://jsfiddle.net/abhishekverma3189/qvtWR/ 您可以在这里看到小提琴http://jsfiddle.net/abhishekverma3189/qvtWR/

Try this jQuery trick: 试试这个jQuery技巧:

$(function() {
  $.fn.setAllToMaxHeight = function(){
    return this.height(
      Math.max.apply(this, $.map(this, function(e) { return $(e).height() }) )
    )
  }

  $('div.box').setAllToMaxHeight();
  //setTimeout("$('div.box').setAllToMaxHeight();", 10);
});

And CSS add: 和CSS添加:

.box {
   width:350px;
   display: inline-block;
   margin:10px 20px 0 auto;
   padding:10px;
   border:1px solid black;
   min-height:60px;
   font-size:60px;
   text-align: center;
   vertical-align: top;
}

Example: http://jsfiddle.net/UDm3a/7/ 示例: http//jsfiddle.net/UDm3a/7/

If I got you right, then there are several methods to accomplish what you need. 如果我说对了,那么有几种方法可以满足您的需求。

you can find them here: Fluid Width Equal Height Columns 您可以在这里找到它们: 流体宽度等于高度列

They solve the problem like this: 他们解决了这样的问题:

在此处输入图片说明

to be like this: 像这样:

在此处输入图片说明

you can see a DEMO here 你可以在这里看到一个演示

and you can download the source files from here: DOWNLOAD FILES 您可以从此处下载源文件: 下载文件

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

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