简体   繁体   English

100%高度的输入字段和垂直对齐文本

[英]100% height input-field and align text vertically

Maybe a duplicate but I did not found a question with a similar problem. 也许是重复的,但我没有发现类似问题的问题。

Ok, I've to create a 100% height input field that centers the text vertically and horizontally. 好的,我必须创建一个高度为100%输入字段,以垂直和水平居中放置文本。 The problem is that if I set the line-height to window -width the cursor gets the same size in Chrome. 问题是,如果我将line-height设置为window line-height width,则光标在Chrome中的大小相同。

Any ideas how to center the text without line-height ? 有什么想法如何使文本居中而没有line-height吗?

input {
    position: absolute;
    top: 0;
    left 0;
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    text-align: center;
    border: 0;
    outline: 0
}

Just to set the line-height : 只是设置line-height

$('input').css({
    lineHeight: $(window).height() + 'px'
});

http://fiddle.jshell.net/G9uVw/ http://fiddle.jshell.net/G9uVw/

Update 更新资料

It seems that the question needs to be changed. 看来这个问题需要改变。 The problem is that oldIE doesn't center the text, but all other browsers do. 问题在于, oldIE不会使文本居中,而所有其他浏览器都会使文本居中。 So the new question is, how can we check if a browser supports this auto-center - feature?! (Since we know that 因此,新问题是,我们如何检查浏览器是否支持此auto-center feature?! (Since we know that feature?! (Since we know that ua`-sniffing is evil, I don't want to check for a particular browser...) feature?! (Since we know that ua`嗅探是邪恶的,所以我不想检查特定的浏览器...)

Update2 更新2

It seems that this is a bug in webkit: https://code.google.com/p/chromium/issues/detail?id=47284 似乎这是Webkit中的错误: https : //code.google.com/p/chromium/issues/detail? id = 47284

Try this, should align vertically: 试试这个,应该垂直对齐:

input {

    //// vertical align
    position: relative;
    top: 50%;
    -webkit-transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    transform: translateY(-50%);
    ////

    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    text-align: center;
    border: 0;
    outline: 0
}

Not sure if the height: 100% will avoid the job, try it without height: 100% if it doesn't work. 不确定高度:100%是否会避免工作,如果没有高度,请尝试:100%,如果不起作用。 If that's the case, maybe the solution is to calculate the height everytime with javascript/jquery. 如果是这样,也许解决方案是每次使用javascript / jquery计算高度。

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

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