简体   繁体   English

与CSS垂直对齐

[英]Vertical alignment with CSS

Yeah, yeah, I know this is yet another question about vertical alignment with CSS and it's been done a million times before. 是的,是的,我知道这是关于与CSS垂直对齐的另一个问题,它之前已经完成了一百万次。 Rest assured that I have come across this problem many times and I've already done the reading about various ways to centre vertically with CSS. 请放心,我已经多次遇到过这个问题,而且我已经完成了关于使用CSS垂直居中的各种方法的阅读。 I'm asking here because none of those ways do quite what I want to do, and I just want to make sure that my suspicion (that CSS vertical alignment is broken and will not do what I want it to do) is definitely correct. 我在这里问,因为这些方法都没有做我想做的事情,我只想确保我的怀疑(CSS垂直对齐被破坏而不会做我想做的事情)绝对是正确的。

First up, here's my test case: http://www.game-point.net/misc/testAlign/ 首先,这是我的测试用例: http//www.game-point.net/misc/testAlign/

Here's the criteria: 这是标准:

  • I want to align the 'centred text' vertically, with respect to the DIV containing the 'TestTestTest...' text. 我想垂直对齐'居中文本',相对于包含'TestTestTest ...'文本的DIV。
  • I don't want to specify ANY heights. 我不想指定任何高度。
  • I want both the 'TestTestTest' and the 'Centred text' DIVs to get their heights dynamically, according to the amount of text they have, and the width limit they have. 我希望'TestTestTest'和'Centered text'DIV根据它们的文本数量和它们的宽度限制动态地获得它们的高度。
  • I don't want to use Javascript. 我不想使用Javascript。

This seems to be impossible even in CSS3, let alone CSS2. 即使在CSS3中,这似乎也是不可能的,更不用说CSS2了。 The annoying thing is that I'm almost there; 令人讨厌的是我几乎就在那里; the position:absolute; top:-50%; position:absolute; top:-50%; position:absolute; top:-50%; DIV works to set the top of that DIV to halfway down the container DIV. DIV用于将DIV的顶部设置为容器DIV的一半。 The problem is that the inner DIV, with style position:relative; top:-50%; 问题是内部DIV,风格position:relative; top:-50%; position:relative; top:-50%; doesn't do anything to move the content up by half its height, to centre it fully, because CSS says that an absolutely positioned DIV doesn't have a height and therefore top:-50% is meaningless. 没有做任何事情将内容向上移动一半的高度,使其完全居中,因为CSS说绝对定位的DIV没有高度,因此top:-50%没有意义。 As far as I can tell, this is just a fundamental flaw in CSS for no particular reason. 据我所知,这只是CSS中的一个根本缺陷,没有特别的原因。 An absolutely positioned element does have a height, and I don't know why CSS pretends it doesn't. 一个绝对定位的元素确实有一个高度,我不知道为什么CSS假装它没有。 I just wanted to ask whether anyone had any ideas as to how I could achieve the desired effect, pictured at the bottom, given the criteria I outlined above. 我只是想问一下是否有人对我如何达到预期的效果有任何想法,如图所示我在上面列出的标准。 Ironically IE6/7/8's 'broken' box model, in quirks mode, gives me this very effect. 具有讽刺意味的是IE6 / 7/8的'破碎'盒子模型,在怪癖模式下,给了我这种效果。 Shame they're 'fixing' it in IE9 so it won't anymore. 很遗憾他们在IE9中'修复'它所以它不再存在了。

OK, my suspicion was indeed correct and this is not possible (I think it's a flaw in CSS and they should provide a means of vertical alignment within a DIV without specifying inner element heights). 好吧,我的怀疑确实是正确的,这是不可能的(我认为这是CSS的一个缺陷,他们应该提供一种在DIV内垂直对齐的方法,而不指定内部元素的高度)。

The least bad solution I ended up with was this: specify the height of the 'middle' DIV - that is, the DIV which is displayed using position:absolute and contains the real content. 我最终得到的最不好的解决方案是:指定“中间”DIV的高度 - 即使用position:absolute显示的DIV并包含真实内容。 I've added it to the test case page at http://www.game-point.net/misc/testAlign/ under the heading With line-height:100% and hardcoded 'middle' DIV height . 我已经将它添加到测试用例页面http://www.game-point.net/misc/testAlign/ ,标题为“行高”:100%和硬编码“中间”DIV高度 This solution means that you must know the height of the content to be vertically centred in advance, which sucks because the browser calculates this and you shouldn't need to specify it, but it's the only way (until CSS gets its act together). 这个解决方案意味着你必须事先知道要垂直居中的内容的高度,这很糟糕,因为浏览器计算了这个并且您不需要指定它,但这是唯一的方法(直到CSS一起行动)。 I used em s to specify the height too, so that zooming text in and out doesn't ruin the vertical centring. 我也用em来指定高度,这样缩放文本就不会破坏垂直居中。 Turns out that for the 2 lines of 'Centred text' I had, this height equates to exactly 2 em s (at least on my machine). 事实证明,对于我所拥有的2行“中心文本”,这个高度恰好等于2 em (至少在我的机器上)。 If I were to change that content's height, or it were to change dynamically to say 3 lines or 1 line, the parent div 's hardcoded em height would also have to change. 如果我要改变内容的高度,或者它是动态改变的说3线或1号线,父div的硬编码的em高度也将不得不改变。

So, here's the code I finally ended up with if anyone's interested: 所以,如果有人感兴趣,这里是我最终得到的代码:

    <div style="border:1px solid black; padding-left:60px; width:250px; position:relative; word-wrap:break-word;">
        TestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTest
        <!-- Unfortunately, must set this DIV's height explicitly or the contained DIV's relative positioning won't work, as this DIV is considered to have NO implicit height -->
        <div style="position:absolute; display:block; top:50%; left:0px; height:2em;">
            <div style="position:relative; top:-50%; background-color:#00aa00; line-height:100%;">
                Centred text<br/>
                Centred text
            </div>
        </div>
    </div>

使用margin-top:而不是top: like margin-top: -25%;

The thing is though that vertical handling of content is made that way for obvious reasons, which I'm sure you're already aware of. 事情是,内容的垂直处理是出于显而易见的原因,我相信你已经知道了。 But you're sort of trying to solve an unsolvable problem, that's not really a problem. 但是你有点试图解决一个无法解决的问题,这不是一个真正的问题。 (makes sense when you know why things are like they are) (当你知道为什么它们是这样的时候才有意义)

By using absolute position you are just taking content out of the natural flow. 通过使用绝对位置,您只是从自然流中获取内容。 It's much better to make everything like it should be with floats and proper sizes or whatever is needed so it degrades nicely and appears ok looking in all devices. 使用漂浮物和适当的尺寸或任何需要的东西制作一切都好得多,所以它会很好地降级并且在所有设备上看起来都很好看。 Then write two lines of code in jquery to check heights and align everything (which will also work on "all" devices). 然后在jquery中编写两行代码来检查高度并对齐所有内容(这也适用于“所有”设备)。 The 2-3% without JS will need to live with boring sites. 没有JS的2-3%将需要与无聊的网站一起生活。

I have started to get somewhere with the following code ... though needs work. 我已经开始使用以下代码获得某个地方......虽然需要工作。

Based on items found on http://www.jakpsatweb.cz/css/css-vertical-center-solution.html 基于http://www.jakpsatweb.cz/css/css-vertical-center-solution.html上的项目

Note, your code runs in quirks mode, I have changed this to html 5 请注意,您的代码以怪癖模式运行,我已将其更改为html 5

<!DOCTYPE HTML>
<html>
<head>
    <title>Vertical align test</title>
</head>

<style type="text/css">


#outer { overflow: hidden; position: relative; border: 1px solid red; }
#outer[id] {display: table; position: static;}

#middle {position: absolute; top: 50%;} /* for explorer only*/
#middle[id] {position: relative; display: table-cell; vertical-align: middle; width: 100%;}

#inner {position: relative; top: -50%; width: 100px} /* for explorer only */
/* optional: #inner[id] {position: static;} */

</style>

<body>
<h1>New test</h1>

<div id="outer">
  <div id="middle">
    <div id="inner">
      any text<br>
      any height<br>
      any content, for example generated from DB<br>
      everything is vertically centered
    </div>

  </div>

test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test 
</div>


</body>
</html>
.item {
    height: 40px;
    top: 50%;
    margin-top: -20px;
}

position relative or absolute; 相对或绝对的位置;

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

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