简体   繁体   English

仅使用CSS尝试使最右边的div对齐到底部

[英]Trying to get rightmost div to align to bottom using CSS only

I have some html for displaying 3 divs within the same row in an ASP.Net page. 我有一些HTML,用于在ASP.Net页面的同一行中显示3 div。 While the divs show up next to each other in a row, the problem is that the third div ( rightmost div) is positioned at top of row and I would want to make it bottom aligned with the row. 当div连续显示时,问题是第三个div(最右边的div)位于行的顶部,我想使其与行的底部对齐。

The content within left div ( first div) and center div ( second div) is not constant and can be set to any image, which means the height of these 2 divs is not fixed. 左div(第一div)和中心div(第二div)内的内容不是恒定的,可以设置为任何图像,这意味着这两个div的高度不是固定的。 I have simulated image content in these 2 divs by adding a div with a certain height and width, and also simulated some content in rightmost div. 我通过添加具有一定高度和宽度的div在这2个div中模拟了图像内容,并在最右边的div中模拟了一些内容。

Is it possible to do this using CSS, and if yes then how would I do it? 是否可以使用CSS进行此操作,如果可以,我该怎么做? I do not want to use a table element, but only divs in my html. 我不想使用表格元素,而只在HTML中使用div。

I have this also at jsbin : http://jsbin.com/yovukinu/2/edit?html,output 我在jsbin上也有这个http : //jsbin.com/yovukinu/2/edit?html,输出

 <div style="float: left; text-align: left;
           margin: 0 auto; padding:0px;" id="divLogo">
        <!--some image i.e. img content goes here-->
      <div style='height:120px;width:250px;border:solid 1px green'>IMAGE CONTENT  goes here </div>
 </div>

<div style="float: left; margin-left:100px; " id="divTopAd" runat="server"  >
      <!--some image i.e. img content goes here-->
    <div style='height:240px;width:310px;border:solid 1px red'>IMAGE CONTENT  goes here </div>
</div>

<div style="float:right;margin-right:15px; margin-bottom:4px;padding:0px;"
       id="divRS">
    <!--some non-image content goes here-->
   <div style='font-weight:bold; height:30px;border:solid 1px yellow'> Some Non-Image Content </div>
</div>
<div style='clear:both'>/div>

This displays as in screen shot below. 如下面的屏幕快照所示。 最右边的Div不在行的底部

UPDATE 1: You can see the suggested answer in action at this URL: http://jsbin.com/yovukinu/6/edit?html,output . 更新1:您可以在以下URL上看到建议的答案: http : //jsbin.com/yovukinu/6/edit?html,output Note that the CSS float needs to be removed if table type display is used in div CSS. 请注意,如果在div CSS中使用表类型显示,则需要删除CSS float。

UPDATE 2: I found another answer using jQuery, which makes the above html work in IE 7 also, else the marked answer can be used but it will not work in IE 7. I used the following jQuery to make it work in IE7 and all other browsers. 更新2:我发现使用jQuery的另一个答案,这也使上述html在IE 7中也可以工作,否则可以使用标记的答案,但在IE 7中不起作用。我使用以下jQuery使它在IE7和所有版本中都可以工作其它浏览器。

In this jQuery, I get the maximum height among all the 3 divs, then set the top margin for the rightmost div to (Max. Height - Height of RightMostDiv ) . 在此jQuery中, 我获得了所有3个div中的最大高度, 然后将最右边的div的顶部边距设置为 (Max。Height-Height of RightMostDiv) jQuery is excellent and very powerful for adjusting CSS dynamically, which is what I am doing here. jQuery非常出色,非常强大,可以动态调整CSS,这就是我在这里所做的。

You can see this in action at this URL: http://jsbin.com/yovukinu/5/edit?html,output (Make sure you click Run with JS button after you make the Output pane wide enough by dragging its left border, else you will not see this in action) 您可以在以下URL上看到它的运行情况: http : //jsbin.com/yovukinu/5/edit?html,输出 (请确保在将“输出”窗格拖动到其左侧边框足够宽后,单击“使用JS运行”按钮,否则您将看不到此操作)

<script type="text/javascript">
$(document).ready(function () {
    var div1 = $("#divLogo");
    var div2 = $("div[id*='divTopAd']");
    var div3 = $("#divRS");
    var ht1 = 0;
    if (div1 != null) {
        ht1 = div1.height();
        if(ht1 == null)
        {
            ht1 = 0;
        }
    }

    var ht2 = 0;
    if (div2 != null) {
        ht2 = div2.height();
        if (ht2== null) {
            ht2 = 0;
        }
    }
    var ht3 = 0;
    if (div3 != null) {
        ht3 = div3.height();
        if (ht3 == null) {
            ht3 = 0;
        }
    }
    var maxht = 0;
    if(ht1 >= ht2)
    {
        maxht = ht1;
    }
    else
    {
        maxht = ht2;
    }
    if(maxht < ht3)
    {
        maxht = ht3;
    }

    if (maxht > (ht3 + 10) && div3 != null)
    { div3.css("margin-top", (maxht - ht3) + "px");  }
  });
</script>

Not table elements, but you could set a wrapper div with display: table and set display:table-cell to those 3 divs to work with them just like with table cells. 不是表元素,但是您可以使用display: table设置包装div并将display:table-cell为这3个div,以便与表单元一样使用它们。

EDIT: 编辑:

In table cell you can use vertical-align 在表格单元格中,您可以使用垂直对齐

Is it completely necessary you have varying image sizes, else you can add a common class to all div and give a fixed height for all, 是否完全有必要设置不同的图片大小,否则可以向所有div添加一个通用类并为所有图片指定固定的高度,

if you only three images in a row, set the class to have a width of 32%. 如果连续只显示三张图片,请将类别的宽度设置为32%。

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

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