简体   繁体   English

将 textarea 水平和垂直居中

[英]Centering textarea horizontally and vertically

I want to center textarea vertically and horizontally.我想将 textarea 垂直和水平居中。 It needs to be responsive (i want it to change its size when window resizes. I can't get this working. Please help me:)它需要响应(我希望它在 window 调整大小时改变它的大小。我无法正常工作。请帮助我:)

Code is below:代码如下:

        <div class="name-input-container">
            <textarea id="text-name" placeholder="Enter your name..." min="3" maxlength="15"></textarea>
        </div>
.name-input-container{
    width: 100vw;
    height: 100vh;
    background-color: #001f3f;
    color: 80BFFF;
    z-index: 9999;

    #text-name{
        resize: none;
        transform: translateY(+200%);
        width: 75%;
        height: 20%;
        display: table;
        margin: 0 auto;
        -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
        -moz-box-sizing: border-box;    /* Firefox, other Gecko */
        box-sizing: border-box;         /* Opera/IE 8+ */
        line-height: 600%;
        text-align: center;
        font-size: 30px;
        border: 2px solid #00172e;
        background-color: #001a35;
        border-radius: 75px;
    }

}

Somebody is going to suggest an answer using flex which would be better for modern standards but this will also work in your CSS.有人会建议使用flex的答案,这对于现代标准会更好,但这也适用于您的 CSS。

.name-input-container {
    position: absolute;
    left: 50%;
    top: 50%;
    -webkit-transform: translateX(-50%) translateY(-50%);
    -moz-transform: translateX(-50%) translateY(-50%);
    transform: translateX(-50%) translateY(-50%);
}


I was almost gonna use flex, but then I saw @CuteCodeRobs answer...我几乎要使用 flex,但后来我看到 @CuteCodeRobs 回答......

So here is my answer:所以这是我的答案:

The shortest way to center an element is this:使元素居中的最短方法是:

parent-element {
  display: grid;
  place-items: center
}

You can also do this with flex:你也可以用 flex 做到这一点:

parent-element {
  display: flex;
  justify-content: center;
  align-items: center;
}

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

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