简体   繁体   English

li里面的CSS垂直对齐文本

[英]CSS vertical alignment text inside li

I am displaying number of boxes in a row with fix height and width, generated from <li> tags. 我正在显示从<li>标签生成的具有固定高度和宽度的行数。 now I need to align the text in the vertical center. 现在我需要在垂直中心对齐文本。 The CSS vertical-align has no impact, maybe I am missing something??? CSS vertical-align没有影响,也许我错过了什么?

I am not looking for tricks using (margin, padding, line-height), these will not work because some text are long and will break into two lines. 我不是在寻找使用(边距,填充,行高)的技巧,这些都行不通,因为有些文本很长并且会分成两行。

Please find the actual code: 请找到实际代码:

CSS code CSS代码

ul.catBlock{
  width:960px; 
  height: 270px; 
  border:1px solid #ccc; 
}

ul.catBlock li{
  list-style: none; 
  float:left; 
  display:block; 
  text-align: center; 
  width:160px; 
  height: 100px;
}

ul.catBlock li a{ 
  display: block;  
  padding: 30px 10px 5px 10px; 
  height:60px;
}

HTML code HTML代码

<ul class="catBlock">
 <li><a href="#">IP Phone</a></li>
 <li><a href="#">Dual SIM Switch Server</a></li>
 <li><a href="#">IP PBX</a></li>
</ul>

使用display: table定义父级,使用vertical-align: middledisplay: table-cell定义元素本身。

line-height is how you vertically align text. line-height是您垂直对齐文本的方式。 It is pretty standard and I don't consider it a "hack". 这是非常标准的,我不认为它是“黑客”。 Just add line-height: 100px to your ul.catBlock li and it will be fine. 只需在你的ul.catBlock li添加line-height: 100px就可以了。

In this case you may have to add it to ul.catBlock li a instead since all of the text inside the li is also inside of an a . 在这种情况下,您可能必须将其添加到ul.catBlock li a替代,因为里面所有的文字li也是一个内部a I have seen some weird things happen when you do this, so try both and see which one works. 我发现当你这样做时会发生一些奇怪的事情,所以试试两个并看看哪个有效。

Surprisingly (or not), the vertical-align tool actually works best for this job. 令人惊讶(或不), vertical-align工具实际上最适合这项工作。 Best of all, no Javascript is required. 最重要的是,不需要Javascript。

In the following example, I am positioning the outer class in the middle of the body, and the inner class in the middle of the outer class. 在下面的例子中,我定位outer体内的中产阶级,和inner类在中间outer类。

Preview: http://jsfiddle.net/tLkSV/513/ 预览: http//jsfiddle.net/tLkSV/513/

HTML: HTML:

<div id="container">
    <span></span><div class="outer">
        <span></span><div class="inner">

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

CSS: CSS:

html, body {
    height: 100%;
    margin: 0;
    padding: 0; }
#container {
    text-align: center;
    height: 100%; }
span { 
    height: 100%;
    vertical-align: middle;
    display: inline-block; }
.outer {
    width: 100px;
    height: 200px;
    padding: 0;
    border: 1px solid #000;
    vertical-align: middle;
    display: inline-block; }
.inner {
    background: red;
    width: 30px;
    height: 20px;    
    vertical-align: middle;
    display: inline-block; }

Vertical align works by aligning the centers of elements that are next to each other. 垂直对齐通过对齐彼此相邻的元素的中心来工作。 Applying vertical-align to a single element does absolutely nothing. 将vertical-align应用于单个元素绝对没有任何意义。 If you add a second element that has no width but is the height of the container, your single element will move to vertically center with this no-width element, thus vertically centering it. 如果添加第二个没有宽度但是容器高度的元素,则单个元素将使用此无宽度元素移动到垂直居中,从而垂直居中。 The only requirements are that you set both elements to inline (or inline-block), and set their vertical-align attribute to vertical-align: middle . 唯一的要求是将两个元素都设置为内联(或内联块),并将其vertical-align属性设置为vertical-align: middle

Note: You may notice in my code below that my <span> tag and <div> tag are touching. 注意:您可能会在下面的代码中注意到我的<span>标记和<div>标记正在触及。 Because they are both inline elements, a space will actually add a space between the no-width element and your div, so be sure to leave it out. 因为它们都是内联元素,所以空格实际上会在无宽度元素和div之间添加一个空格,因此请务必将其保留。

In the future, this problem will be solved by flexbox. 将来,这个问题将由flexbox解决。 Right now the browser support is dismal, but it is supported in one form or another in all current browsers. 目前浏览器支持很惨淡,但在所有当前浏览器中都支持这种或那种形式。

Browser support: http://caniuse.com/flexbox 浏览器支持: http//caniuse.com/flexbox

.vertically_aligned {

    /* older webkit */
    display: -webkit-box;
    -webkit-box-align: center;
    -webkit-justify-content: center;

    /* older firefox */
    display: -moz-box;
    -moz-box-align: center;
    -moz-box-pack: center;

    /* IE10*/
    display: -ms-flexbox;
    -ms-flex-align: center;
    -ms-flex-pack: center;

    /* newer webkit */
    display: -webkit-flex;
    -webkit-align-items: center;
    -webkit-box-pack: center;

    /* Standard Form - IE 11+, FF 22+, Chrome 29+, Opera 17+ */
    display: flex;
    align-items: center;
    justify-content: center;
}

Background on Flexbox: http://css-tricks.com/snippets/css/a-guide-to-flexbox/ Flexbox背景: http ://css-tricks.com/snippets/css/a-guide-to-flexbox/

However many years late this response may be, anyone coming across this might just want to try 无论多少年后这种反应可能是,任何遇到这种情况的人都可能只是想尝试一下

li {
    display: flex;
    flex-direction: row;
    align-items: center;
}

Browser support for flexbox is far better than it was when @scottjoudry posted his response above, but you may still want to consider prefixing or other options if you're trying to support much older browsers. 浏览器对flexbox的支持比@scottjoudry在上面发布他的响应要好得多,但是如果你试图支持更旧的浏览器,你可能仍然想要考虑前缀或其他选项。 caniuse: flex caniuse:flex

There are no perfect answers provided here except Asaf's answer which doesn't provide any code nor any example, so I would like to contribute mine... 除了Asaf的回答没有提供任何代码或任何示例,这里没有提供完美的答案,所以我想贡献我的...

Inorder to make vertical-align: middle; 为了使vertical-align: middle; work, you need to use display: table; 工作,你需要使用display: table; for your ul element and display: table-cell; 为你的ul元素和display: table-cell; for li elements and than you can use vertical-align: middle; 对于li元素,你可以使用vertical-align: middle; for li elements. 对于li元素。

You don't need to provide any explicit margins , paddings to make your text vertically middle. 您不需要提供任何明确的marginspaddings以使文本垂直居中。

Demo 演示

ul.catBlock{
    display: table;
    width:960px; 
    height: 270px; 
    border:1px solid #ccc; 
}

ul.catBlock li {
    list-style: none;
    display: table-cell; 
    text-align: center; 
    width:160px; 
    vertical-align: middle;
}

ul.catBlock li a { 
    display: block;
}

As explained in here: https://css-tricks.com/centering-in-the-unknown/ . 如下所述: https//css-tricks.com/centering-in-the-unknown/

As tested in the real practice, the most reliable yet elegant solution is to insert an assistent inline element into the <li /> element as the 1st child, which height should be set to 100% (of its parent's height, the <li /> ), and its vertical-align set to middle. 正如在实际测试中所测试的那样,最可靠而又优雅的解决方案是将辅助内联元素作为第一个子元素插入到<li />元素中,其高度应设置为100%(其父级的高度, <li /> ),并将其vertical-align设置为middle。 To achieve this, you can put a <span /> , but the most convenient way is to use li:after pseudo class. 要实现这一点,你可以放一个<span /> ,但最方便的方法是使用li:after伪类。

Screenshot: 截图: 在此输入图像描述

ul.menu-horizontal {
    list-style-type: none;
    margin: 0;
    padding: 0;
    display: inline-block;
    vertical-align: middle;
}

ul.menu-horizontal:after {
    content: '';
    clear: both;
    float: none;
    display: block;
}

ul.menu-horizontal li {
    padding: 5px 10px;
    box-sizing: border-box;
    height: 100%;
    cursor: pointer;
    display: inline-block;
    vertical-align: middle;
    float: left;
}

/* The magic happens here! */
ul.menu-horizontal li:before {
    content: '';
    display: inline;
    height: 100%;
    vertical-align: middle;
}

Give this solution a try 试试这个解决方案吧

Works best in most of the cases 在大多数情况下效果最佳

you may have to use div instead of li for that 你可能不得不使用div而不是li

 .DivParent { height: 100px; border: 1px solid lime; white-space: nowrap; } .verticallyAlignedDiv { display: inline-block; vertical-align: middle; white-space: normal; } .DivHelper { display: inline-block; vertical-align: middle; height:100%; } 
 <div class="DivParent"> <div class="verticallyAlignedDiv"> <p>Isnt it good!</p> </div><div class="DivHelper"></div> </div> 

Simple solution for vertical align middle... for me it works like a charm 垂直对齐中间的简单解决方案......对我而言,它就像一个魅力

ul{display:table; text-align:center; margin:0 auto;}
li{display:inline-block; text-align:center;}
li.items_inside_li{display:inline-block; vertical-align:middle;}

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

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