简体   繁体   English

确保float:right元素垂直对齐到顶部,即使在float:left元素旁边也是如此

[英]Make sure a float:right element is vertically aligned to the top, even if next to a float:left element

I ran into a small problem with floats that i demonstrate in this fiddle . 在这个小提琴中演示一个浮子的小问题。

I have a DIV which floats to the left, whose width is dynamic (unknown). 我有一个DIV浮动到左侧,其宽度是动态的(未知)。 I have another one that floats to the right in the same block, width dynamic as well. 我有另一个浮在同一块中的右边,宽度也是动态的。

The problem is that if the width of the first block extends so that it would collide with the right float, the right float will (correctly) drop downwards to make sure no collision is happening. 问题是,如果第一个程序块的宽度扩大,使其与右浮动块发生碰撞,则右浮动块将(正确地)向下下落以确保没有发生碰撞。 However, i want it to stay on top (vertically, that is - not in terms of z-index). 但是,我希望它保持在最前面(垂直方向,而不是z-index角度)。

Basically it seems that the text is prioritized as to "displace" the block on the right side. 基本上,似乎优先考虑在右侧“替换”该块。 This should be the other way around, but with the text on the left using up the available space on the topmost line before it even starts to wrap. 这应该是另一种方法,但是左边的文本在开始换行之前就用尽了最上面一行的可用空间。

I guess the solution is fairly simple. 我猜解决方案很简单。 Its just that it doesn't come to my mind at all and any searches i did didn't find me what i was looking for. 只是根本没有想到它,而我所做的任何搜索都没有找到我想要的东西。

You might want to try using css tables/ Just create both elements and make it a table, then make your right and left elements table-cells: 您可能想尝试使用css表/只需创建两个元素并使其成为一个表,然后将左右元素设置为table-cells:

#wrapper {
  display: table;
  width: 100%;
}
#leftside, #rightside {
  display: table-cell;
  width: 50%; /* Both sides will be rendered on one line */
  vertical-align: top;
}
/* Position elements within the cell */
#leftside { text-align: left; }
#rightside { text-align: right; }
#leftside > div, #rightside > div {
  display: inline-block;
  text-align: left; /* Reset text alignment */
}

Explanation: The table structure will keep the elements in one line with width 50%; 说明:表结构将元素保持在一行中,宽度为50%; The inner elements (divs in this case) will be inline-blocks so that they can be aligned left or right. 内部元素(在这种情况下为div)将是内联块,因此它们可以向左或向右对齐。 Now when one of the inner divs exceeds the max width of 50% it will just make the other 'cell' side smaller 现在,当一个内部div超过最大宽度50%时,它将使另一“单元格”侧变小

Float the label div inside the title div, that will wrap the title text around the label regardless of the width of either. 将标签div浮动在标题div内,无论标签的宽度如何,标题文本都会环绕标签。

<div class="infoBox">
<div class="inner">
    <div class="entry">
        <div class="title">
        <div class="type">
            LABEL
        </div>
            If this text is longer, the LABEL will drop downwards.
            I would like to have the LABEL float right (as it does here) but also be at the top of the block. 
        </div>
    </div>
</div>

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

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