简体   繁体   English

将浮动块之间的行内块元素居中

[英]Centering inline-block element between floats

I have a header which contains elements on the left, middle, and right. 我有一个标题,其中包含在左侧,中间和右侧的元素。 To achieve this, I have a float left div, a float right div, and a margin 0 auto div, as exampled here: http://jsfiddle.net/huggz/uuq4Z/1/ 为了实现这一点,我有一个左浮动div,右浮动一个浮点和0边距自动div,例如此处所示: http : //jsfiddle.net/huggz/uuq4Z/1/

<div style="text-align: center;" >

    <div style="width: 200px; height: 100px; background: red; float: left;"></div>
    <div style="width: 100px; height: 100px; background: blue; float: right;"></div>

    <div style="background: green; margin: 0 auto; display: inline-block;">
        <div style="width: 150px; height: 100px; background: orange;"></div>
    </div>

</div>

I have the middle div set inline-block in an attempt to allow the middle content to vary in width. 我让中间div设置了内联块,以尝试使中间内容的宽度变化。 However doing so, makes the margin: auto treat the floated elements like they're actually holding space. 但是,这样做有余地:自动处理浮动元素,就像它们实际上在容纳空间一样。 If left is wider than right, I end up with a slightly (or not so slightly) off-centered middle element. 如果左侧的宽度大于右侧的宽度,那么我最终会得到一个稍微偏心的(或不太偏心的)中间元素。

Is there a way to do what I'm trying to do, preferably without resorting to scripts. 有没有一种方法可以做我想做的事情,最好不要使用脚本。 If that need be the case, I'd rather just deal with the consequences of making middle column fixed width. 如果需要,我宁愿处理使中间列固定宽度的后果。

[EDIT] I should mention, there are instances where no middle or right content will exist. [编辑]我应该提到,在某些情况下,中间或正确的内容将不存在。

you can use position instead of float: http://jsfiddle.net/uuq4Z/5/ 您可以使用位置代替浮动: http : //jsfiddle.net/uuq4Z/5/

<div style="text-align: center; position: relative;" >

    <div style="width: 200px; height: 100px; background: red; position: absolute; left: 0; top: 0"></div>
    <div style="width: 100px; height: 100px; background: blue; position: absolute; right: 0; top: 0;"></div>
    <div style="background: green; display: inline-block;">
        <div style="width: 150px; height: 100px; background: orange;"></div>
    </div>

</div>

I think you want something like this: 我想你想要这样的东西:

<div>
    <div style="width: 200px; height: 100px; 
                background: red; float: left;"></div>
    <div style="width: 100px; height: 100px; 
                background: blue; float: right;"></div>

    <div style="background: green; display: block; margin: 0 100px 0 200px;">
        <div style="width: 150px; height: 100px; 
                    background: orange; margin: 0 auto;"></div>
    </div>

</div>

with the fiddle: http://jsfiddle.net/audetwebdesign/AFQV3/ 与小提琴: http : //jsfiddle.net/audetwebdesign/AFQV3/

I would keep the center element in-flow (not floated) and set the left and right margins so allow for the left and right floated elements. 我将使中心元素保持流动(不浮动)并设置左右边距,以便允许左右浮动元素。

The center div will then have a block of space in which you can set other elements, for example, the orange div, which you center between the floats. 然后,中心div将有一个空间块,您可以在其中设置其他元素,例如,橙色div,将其放置在浮点之间。

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

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