简体   繁体   English

将div与文本垂直对齐

[英]Vertically align div with text

It seems that I can't get the hang of this, and ugh it's pretty frustrating. 看来我无法理解这一点,而且非常令人沮丧。 This is a similar question to my previous question here . 这是我刚才的问题类似的问题在这里

http://jsfiddle.net/bm33e/ How would I get the red dot in vertically middle relative to the text? http://jsfiddle.net/bm33e/如何在相对于文本的垂直中间获得红点? I've tried solutions and slight variations but couldn't get it working. 我尝试过解决方案和轻微变化,但无法使其正常工作。

CSS CSS

body { padding: 50px; background-color: #222; }        
    .led {
        display: table-cell;
        width: 16px;
        height: 16px;
        border-radius: 50%;
        float: left;
    }
    .led-red {
        background-color: #ED3029;
        box-shadow: #000 0 -1px 6px 1px, inset #920 0 -1px 8px, #ED3029 0 3px 11px;
    }
    .led-green {
        background-color: #00A448;
        box-shadow: #000 0 -1px 6px 1px, inset #490 0 -1px 8px, #00A448 0 3px 11px;
    }

    .hi {
        max-width: 40%;
        -moz-box-shadow:inset 0px 0px 0px 0px #646464;
        -webkit-box-shadow:inset 0px 0px 0px 0px #646464;
        box-shadow:inset 0px 0px 0px 0px #646464;
        background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #707070), color-stop(1, #646464));
        background:-moz-linear-gradient(top, #707070 5%, #646464 100%);
        background:-webkit-linear-gradient(top, #707070 5%, #646464 100%);
        background:-o-linear-gradient(top, #707070 5%, #646464 100%);
        background:-ms-linear-gradient(top, #707070 5%, #646464 100%);
        background:linear-gradient(to bottom, #707070 5%, #646464 100%);
        filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#707070', endColorstr='#646464',GradientType=0);
        background-color:#707070;
        -moz-border-radius:4px;
        -webkit-border-radius:4px;
        border-radius:4px;
        cursor:pointer;
        color:#ffffff;
        font-size:40px;
        font-weight:bold;
        padding:14px 20px;
        text-align: right;
        height: auto;
    }

HTML HTML

    <div class="hi">
            <div class="led led-red" vertical-align="middle">
            </div>        
            Deluxe
    </div>        

Thanks again stackoverflow helpers :) 再次感谢stackoverflow助手:)

Since your div.hi has relative position and your .led height is constant, you can absolutely position .led and use calc() to center it: 由于你的div.hi有相对位置,你的.led高度是常数,你可以绝对定位.led并使用calc()来居中它:

.led{
    position: absolute;
    top: calc(50% - 8px);
}

JSFiddle 的jsfiddle


Note: this is a css3 solution is not compatible in really old browsers 注意:这是一个css3解决方案在真正的旧浏览器中 兼容

if you don't mind unloading some of that css. 如果你不介意卸载一些css。

I would use the inline-block property on that div. 我会在该div上使用inline-block属性。 I think its more aptly done that way. 我认为它更适合这样做。

.led {display:inline-block;}

http://jsfiddle.net/bm33e/4/ http://jsfiddle.net/bm33e/4/

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

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