简体   繁体   English

浮动元素内的垂直居中对齐图标

[英]Vertically center align icon inside floating element

I have found so many answers about this problem here and tried them out on this with no luck. 我在这里找到了很多有关此问题的答案,并在没有运气的情况下对其进行了尝试。

How to set vertical alignment of all three icons to center/middle? 如何将所有三个图标的垂直对齐设置为居中/居中?

Here is the: Jsfiddle Demo 这是: Jsfiddle演示

 .fl{float:left} .fr{float:right} .vm{vertical-align:middle} .ico {display:inline-block;width:16px; height:16px; line-height:16px; background :url('http://files.fbstatic.com/PostImages/2664562/0/b0617e92-af51-4ca9-9b2f-3225c7607441.png') 0 0 no-repeat} .icoHome{background-position:-48px -160px} .icoHome:hover{background-position:-48px -176px} .icoPrev{background-position:-384px 0px} .icoPrev:hover{background-position:-384px -16px} .icoNext{background-position:-400px 0px} .icoNext:hover{background-position:-400px -16px} .CTitle {border-bottom:1px #e5e5e5 dashed; height:26px; line-height:27px; background-color:#FFFFDF;padding:2px;} .BNav, .BNav2{line-height: 24px; display: block; border: 1px solid #ccc; color: navy; background-color: #CCFFFF; width: 90px; height: 24px; text-align: center; border-radius: 20px; text-decoration: none;} .BNav:hover, .BNav2:hover{color: green; background-color: lime} .BNav2 {padding-left:2px; padding-right:2px; width:80px} .BNav2:hover .icoHome{background-position:-48px -176px} .BNav2:hover .icoMenu{background-position:-112px -16px} .BNav2:hover .icoDel{background-position:-368px -16px} .BNav2:hover .icoMove{background-position:-384px -48px} .BNav2:hover .icoPrev{background-position:-384px -16px} .BNav2:hover .icoNext{background-position:-400px -16px} .BNav2:hover .icoLink{background-position:-400px -48px} 
 <div class="CTitle" style="clear:both; overflow:hidden"> <a class="BNav2 fl" title="Home" href="../A.html"> <!-- float:Left --> <span class="ico icoHome fl vm"></span> <!--float:left, problem : how to align middle vertically--> Test </a> <a class="BNav2 fl" title="Previous" href="../B.html"> <!-- float:left --> <span class="ico icoPrev fl vm"></span> <!--float:left, problem : how to align middle vertically--> Previous </a> <a class="BNav2 fr" title="Next" href="../C.html"> <!-- float:right --> <span class="ico icoNext fl vm"></span> <!--float:left, problem : how to align middle vertically--> Next </a> </div> 

Looks like you use fixed height on both the container and the icon, so you can just to set some margin to it. 看起来您在容器和图标上都使用了固定高度 ,因此您可以为其设置一些margin The container's height is 24px and icon is 16px - (24-16)/2=4 . 容器的高度为24px ,图标为16px (24-16)/2=4

.ico {
    margin-top: 4px;
}

https://jsfiddle.net/9p2fd0kL/7/ https://jsfiddle.net/9p2fd0kL/7/

 .ico {margin-top:4px;} /*ADDED*/ .fl{float:left} .fr{float:right} .vm{vertical-align:middle} .ico {display:inline-block;width:16px; height:16px; line-height:16px; background :url('http://files.fbstatic.com/PostImages/2664562/0/b0617e92-af51-4ca9-9b2f-3225c7607441.png') 0 0 no-repeat} .icoHome{background-position:-48px -160px} .icoHome:hover{background-position:-48px -176px} .icoPrev{background-position:-384px 0px} .icoPrev:hover{background-position:-384px -16px} .icoNext{background-position:-400px 0px} .icoNext:hover{background-position:-400px -16px} .CTitle {border-bottom:1px #e5e5e5 dashed; height:26px; line-height:27px; background-color:#FFFFDF;padding:2px;} .BNav, .BNav2{line-height: 24px; display: block; border: 1px solid #ccc; color: navy; background-color: #CCFFFF; width: 90px; height: 24px; text-align: center; border-radius: 20px; text-decoration: none;} .BNav:hover, .BNav2:hover{color: green; background-color: lime} .BNav2 {padding-left:2px; padding-right:2px; width:80px} .BNav2:hover .icoHome{background-position:-48px -176px} .BNav2:hover .icoMenu{background-position:-112px -16px} .BNav2:hover .icoDel{background-position:-368px -16px} .BNav2:hover .icoMove{background-position:-384px -48px} .BNav2:hover .icoPrev{background-position:-384px -16px} .BNav2:hover .icoNext{background-position:-400px -16px} .BNav2:hover .icoLink{background-position:-400px -48px} 
 <div class="CTitle" style="clear:both; overflow:hidden"> <a class="BNav2 fl" title="Home" href="../A.html"> <!-- float:Left --> <span class="ico icoHome fl vm"></span> <!--float:left, problem : how to align middle vertically--> Test </a> <a class="BNav2 fl" title="Previous" href="../B.html"> <!-- float:left --> <span class="ico icoPrev fl vm"></span> <!--float:left, problem : how to align middle vertically--> Previous </a> <a class="BNav2 fr" title="Next" href="../C.html"> <!-- float:right --> <span class="ico icoNext fl vm"></span> <!--float:left, problem : how to align middle vertically--> Next </a> </div> 

The simplest answer would be to use flexbox. 最简单的答案是使用flexbox。 It's designed for problems just like this one. 它是针对此类问题而设计的。 Flexbox is nice because it will work to center your icons even if you change the height. Flexbox很不错,因为即使您更改高度,它也可以使图标居中。 No calculations necessary. 无需计算。 It even works when you use a fluid height. 当您使用流体高度时,它甚至可以工作。 Here's a nice cheat sheet by Chris Coyier at css-tricks with more of the options flexbox has. 这是Chris Coyier在css-tricks上制作的一个不错的备忘单 ,其中包含flexbox提供的更多选项。 Just in case here's the support tables . 以防万一,这里是支持表

All you need to do is put display: flex and align-items: center in your css rule for .BNav2 . 您需要做的只是display: flexalign-items: center.BNav2的css规则中.BNav2 Flexbox does the rest! 其余的由Flexbox完成!

 body { color: #666; font: 12px/20px'Open Sans', arial, Helvetica, sans-serif; background: #CCD9C8; -webkit-text-size-adjust: none; } body.page { color: #bbb; } .fl { float: left } .fr { float: right } .vm { vertical-align: middle } .ico { display: inline-block; width: 16px; height: 16px; line-height: 16px; background: url('http://files.fbstatic.com/PostImages/2664562/0/b0617e92-af51-4ca9-9b2f-3225c7607441.png') 0 0 no-repeat } .icoHome { background-position: -48px -160px } .icoHome:hover { background-position: -48px -176px } .icoPrev { background-position: -384px 0px } .icoPrev:hover { background-position: -384px -16px } .icoNext { background-position: -400px 0px } .icoNext:hover { background-position: -400px -16px } .CTitle { border-bottom: 1px #e5e5e5 dashed; height: 26px; line-height: 27px; background-color: #FFFFDF; padding: 2px; } .BNav, .BNav2 { line-height: 24px; display: block; border: 1px solid #ccc; color: navy; background-color: #CCFFFF; width: 90px; height: 24px; text-align: center; border-radius: 20px; text-decoration: none; } .BNav:hover, .BNav2:hover { color: green; background-color: lime } .BNav2 { padding-left: 2px; padding-right: 2px; width: 80px; display: flex; align-items: center; } .BNav2:hover .icoHome { background-position: -48px -176px } .BNav2:hover .icoPrev { background-position: -384px -16px } .BNav2:hover .icoNext { background-position: -400px -16px } 
 <div class="CTitle" style="clear:both; overflow:hidden"> <a class="BNav2 fl" title="Home" href="../A.html"> <span class="ico icoHome fl vm"></span> Test </a> <a class="BNav2 fl" title="Previous" href="../B.html"> <span class="ico icoPrev fl vm"></span> Previous </a> <a class="BNav2 fr" title="Next" href="../C.html"> <span class="ico icoNext fl vm"></span> Next </a> </div> 

Why not just use display: table on the body and put everything in a container: 为什么不只使用display: table放在身体上,然后将所有东西放在容器中:

Your Fiddle updated here 您的小提琴在这里更新

CSS 的CSS

body {          /* <-- you could use a div instead of body if you want */
    position:absolute; 
    display: table;    
    height:100%; 
    width:100%;
    margin:0;
    ...
}

#container {
    display: table-cell;
    vertical-align: middle;
    text-align:center;
}

HTML 的HTML

<body>
    <div id="container">
         All your vertically aligned content here
    </div>
</body>

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

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