英文:
I'm having trouble vertically centering this form
问题
我目前正在使用vue.js开发一个web应用程序,我在垂直居中一些元素时遇到了问题。
我想要将包含表单的div(灰色部分)垂直和水平居中。
这是表单的代码,顶部导航栏是一个不同的组件。此外,我正在使用tailwindcss,但也可以考虑内联或内部样式选项。
<template>
<div class="bg-gray-600">
<form action="">
<div>
<input type="email" name="email" id="email"/>
<label for="email">Email</label>
</div>
<div>
<input type="password" name="password" id="password"/>
<label for="password">Password</label>
</div>
</form>
</div>
</template>
下面的代码位于我的base.css中,它是在vue项目创建时创建的。我不知道它对元素有多大影响或者前面的代码块(带有*)做了什么。
*,
*::before,
*::after {
box-sizing: border-box;
padding: 0;
margin: 0;
position: relative;
font-weight: normal;
}
body {
min-height: 100vh;
color: var(--color-text);
background: var(--color-background);
transition: color 0.5s, background-color 0.5s;
line-height: 1.6;
font-family: Inter, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu,
Cantarell, 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
font-size: 15px;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
我尝试了大多数我能找到的解决方案,但几乎没有变化。如果可能的话,我想避免设置固定的像素高度,因为我希望它在不同视口上垂直居中。
英文:
I'm currently developing a web app using vue.js, and I'm having trouble vertically centering some elements.
I would like to have the div with the form(grey colored bit) to be vertically and horizontally centered.
This is the code for the form, the top navbar is a different component. Also, I'm using tailwindcss but am open to inline or internal styling options the same.
<template>
<div class="bg-gray-600">
<form action="">
<div>
<input type="email" name="email" id="email"/>
<label for="email">Email</label>
</div>
<div>
<input type="password" name="password" id="password"/>
<label for="password">Password</label>
</div>
</form>
</div>
</template>
The code below is in my base.css which was created when the vue project was created. I have no idea how much it affects the elements or what the former block of code(The one with *) does.
*,
*::before,
*::after {
box-sizing: border-box;
padding: 0;
margin: 0;
position: relative;
font-weight: normal;
}
body {
min-height: 100vh;
color: var(--color-text);
background: var(--color-background);
transition: color 0.5s, background-color 0.5s;
line-height: 1.6;
font-family: Inter, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu,
Cantarell, 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
font-size: 15px;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
I've tried most solutions I could find but they made little to no changes. I would like to avoid setting a fixed px height if possible because I would like it to be centered across viewports.
答案1
得分: 0
以下是有关如何居中HTML元素的几种方法:
- 书中最古老的诀窍:
.form {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%)
}
这通过将表单元素向左和向上移动50%,然后将其宽度和高度的50%返回来实现。
- 使用flex:
.form-container {
display: flex;
align-items: center;
justify-content: center;
}
将表单的容器设置为flex,并在垂直和水平方向上将其居中对齐。
- 使用grid:
.form-container {
display: grid;
place-items: center;
}
与flex相同,但将justify-content
和align-items
合并到同一行。
您可以使用tailwindcss为您的表单提供相同的CSS:
<form class="absolute top-1/2 left-1/2 translate-x-1/2 translate-y-1/2">
<div class="bg-gray-600 flex items-center justify-center">
<div class="bg-gray-600 grid place-items-center">
希望这有所帮助!
英文:
Here are a few methods on how to center an HTML element:
- The oldest trick in the book:
.form {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%)
}
This works by moving the form element 50% to the left and 50% to the top of the container and move's it back 50% of its width and height.
- flex
.form-container {
display: flex;
align-items: center;
justify-content: center;
}
Set the container of the form to flex and align it to the center vertically and horizontally.
- grid
.form-container {
display: grid;
place-items: center;
}
Same thing as flex, but both justify-content
and align-items
are merged into the same line.
You can give the same CSS to your form using tailwindcss:
<form class="absolute top-1/2 left-1/2 translate-x-1/2 translate-y-1/2">
2.
<div class="bg-gray-600 flex items-center justify-center">
3.
<div class="bg-gray-600 grid place-items-center">
Hope this helps!
答案2
得分: -1
将此CSS应用于您表单的父元素,这种情况下可能是bg-gray-600
:
.bg-gray-600 {
display:flex;
justify-content:center; /*水平居中*/
align-items:center; /*垂直居中*/
}
您可能需要定义父元素的宽度或高度以使其起作用。这取决于您的CSS。
您说您不理解的*
用于选择整个页面内的每个元素,包括bg-gray-600
。在这种情况下,它的任何值都不会影响表单的居中对齐。
英文:
Apply this css on the parent of your form, in this case, probably the bg-gray-600
.bg-gray-600 {
display:flex;
justify-content:center; /*horizontal center*/
align-items:center; /*vertical center*/
}
You might need to define width or height of the parent in order this to work. Depends on your CSS.
The *
that you said you dont understand is used to select every element within the whole page, so the bg-gray-600
as well. In this case, none of its values effect the centering of your form.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论