简体   繁体   English

我将如何垂直对齐JavaScript文本?

[英]How would I vertically align JavaScript text?

I have an alert box with text inside, but I can't think of any ways to move the text down within the alert box div, vertically centering it. 我有一个内部带有文本的警报框,但是我想不出任何方法将文本在警报框div中向下移动,使其垂直居中。 I've tried line-height but that of course will not work for the two lines of text that I have when you view it on a browser with a min-width of 320px and max 480px. 我已经尝试过行高,但是当您在最小宽度为320px且最大为480px的浏览器中查看文本时,它对于两行文本当然不起作用。 #alertmsg should remain at the top, however, the text within it should be centered when viewed on a mobile browser. #alertmsg应该保留在顶部,但是在移动浏览器上查看时,其中的文本应该居中。 I've also tried Vertical-align, but that doesnt work either. 我也尝试过垂直对齐,但这也不起作用。 Does anyone have any suggestions? 有没有人有什么建议?

Any help would be greatly appreciated. 任何帮助将不胜感激。 I also tried to place a <p> in the JavaScript; 我还尝试在JavaScript中放置<p> no luck. 没运气。

Here is my CSS: 这是我的CSS:

#alertmsg {
    position:absolute;
    background-color:#252525;
    top:0px;
    padding-left:0px;
    width:100%;
    font-family:'gotham-medium', arial, sans-serif;
    src:url("http://www.3elementsreview.com/fonts/gotham-light.ttf");
    font-style:normal;
    font-weight:normal;
    font-size:1.2em;
    line-height:1.2em;
    text-align:center;
    height:63px;
    color:#ffffff;
    opacity:1;
    display:inline-block;
    overflow:hidden;
    z-index:1000;
    border-bottom:2px solid #ff6000;
}

And my JavaScript: 而我的JavaScript:

$(document).ready(function() {
    if (typeof window.sessionStorage != undefined) {
        if (!sessionStorage.getItem('mySessionVal')) {
            $('<div />', {
                id   : "alertmsg",
                text :"Join our email list and receive the latest updates at 3Elements Review.",
                on   : {
                    click: function() {
                        $(this).slideUp(200);
                    }
                }
            }).appendTo('body').fadeIn(1).delay(6000).fadeOut("slow");
            sessionStorage.setItem('mySessionVal', true);
            sessionStorage.setItem('storedWhen', (new Date()).getTime());
        }
    }
});

You could use 你可以用

#alertmsg {
    position:absolute;
    top: 50%;
    height:63px;
    margin-top: -31.5px; /* half height */
}

Demo 演示版

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

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