【问题标题】:How to match height of 2 divs如何匹配2个div的高度
【发布时间】:2018-09-05 14:20:26
【问题描述】:
<div class="order-summary">
  <ul>
     <li class="li-summary"><div class="summary-left">Name:</div><div class="summary-right">Ehtesham Ali</div></li>
     <li class="li-summary"><div class="summary-left">Delivery Address:</div><div class="summary-right">House 1, Street 1</div></li>              
  </ul>
</div>

summary-left 和 summary-right 的高度为 100%:我希望 summary-right 与 summary-left 的高度相匹配。

summary-left 有背景颜色,而 summary-right 没有。 li-summary 有一个边框底部。因此,当 summary-right 变为 2 行时,summary-right 上的背景颜色不会填充 100%

.order-summary{
  width: 60%;
  border: 1px solid #cccccc;
  margin: 80px auto;
  font-family: 'Lato','Open Sans','Helvetica Neue','Arial','San-serif';
  text-align: left;
  border-bottom:0;
}
.summary-left{
  width: 25%;
  float: left;
  height: 100%;
  background: #fbfbfb;
  padding: 10px;
}
.summary-right{
  overflow: hidden;
  padding: 10px 0 10px 15px;
  height: 100%;
  border-left: 1px solid #cccccc;
}
.li-summary{
  border-bottom: 1px solid #cccccc;
}

Image of the Issue

解决方案:我发现解决问题的最简单方法是添加渐变背景

.li-summary{
    background-image: linear-gradient(left, #fbfbfb, #fbfbfb 25%, transparent 25%, transparent 100%);
background-image: -webkit-linear-gradient(left, #fbfbfb, #fbfbfb 25%, transparent 25%, transparent 100%);}

【问题讨论】:

  • 也分享 CSS
  • 请用正确的语法调整您的帖子。就目前而言,很难弄清楚您要做什么。
  • 使用 flex box 会自动对等高行进行排序
  • 在原始帖子中添加了 css,也是问题的照片,flex 不适用于我的代码

标签: javascript html css


【解决方案1】:

你可以使用 jquery 来做到这一点:

// get height of left summary and set right summary to the same on page load
var summaryLeftHeight = $(".summary-left").height();
$(".summary-right").css("height", summaryLeftHeight + "px");

// add this to make sure they have the same height on page resize 
$(window).on("resize", function(){
   var summaryLeftHeight = $(".summary-left").height();
   $(".summary-right").css("height", summaryLeftHeight + "px");
});

您也可以使用 http://brm.io/jquery-match-height/ 来执行此操作: 在代码中包含 cdn

<script src="jquery.matchHeight.js" type="text/javascript"></script>

更新的 HTML(我将 class="summary" 添加到您希望具有相同高度的每个元素):

<div class="order-summary">
  <ul>
     <li class="li-summary">
        <div class="summary summary-left">Name:</div>
        <div class="summary summary-right">Ehtesham Ali</div>
     </li>
     <li class="li-summary">
        <div class="summary summary-left">Delivery Address:</div>
        <div class="summary summary-right">House 1, Street 1</div>
     </li>              
  </ul>
</div>

并在你的js文件或脚本标签中初始化代码

$(function() {
    $('.summary').matchHeight();
});

插件默认选择高度最大的元素,但您可以通过选择选项target来选择目标,如下所示:

$(function() {
    $('.summary').matchHeight({
        target: $('.summary-left') // using summary left as the target 
    });
});

【讨论】:

  • 这也是一个可行的选择,但请记住,如果您考虑性能,这不是理想的方式。
  • gist.github.com/dei79/3965331 这是我发现的最简单的解决方法,所以汽车可以帮助我了解如何选择更大的高度而不是更小的高度
  • @EhteshAmAlIi 插件应该自动选择具有较大高度的元素,但我已经编辑了我的答案以显示如何使用插件选择目标元素:另请参阅此处查看其他选项:github.com/liabru/jquery-match-height
  • 是的,我试过这个插件,它的工作原理和我给的一样,它选择较小的高度并匹配那个,我的较小高度是 44 像素,较大的是 70 像素,但第一列有 44 像素两边,所以如果它使用第一列的高度,那么 idk
  • @EhteshAmAlIi 我做了一个codepen,告诉我这是不是你要找的codepen.io/punsammy/pen/zJdzYd
【解决方案2】:

你可以使用 flexbox 来查看Set height of CSS flex elements to the same amount?

我用你的代码创建了一个jsfiddle,你可以自己看看。

ul {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

li{
  display: flex;
  flex-direction: row;
  align-items: stretch;
  justify-content: flex-start;
}

【讨论】:

  • 你的小提琴不工作用更多数据检查这个jsfiddle.net/nje09Lq6/5
  • 你说得对,我忘记了一些额外的 flex 和通用 CSS。我已经更改了答案中的网址,或者您可以在此处查看 jsfiddle.net/nje09Lq6/10
  • 现在左边的宽度不一样,名字大于收货地址,弄乱了整张桌子
  • 我也为您更改了此设置,请参阅jsfiddle.net/nje09Lq6/15 这对您来说是否足够好,还是仍然缺少一些东西?
  • 它正在处理小提琴,但它仍然把网站上的桌子弄乱了,我知道为什么谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-01
  • 2011-06-22
  • 1970-01-01
相关资源
最近更新 更多