我遇到了在垂直居中这个表单的问题。

huangapple go评论75阅读模式
英文:

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.

This is what I currently have

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.

&lt;template&gt;
    &lt;div class=&quot;bg-gray-600&quot;&gt;
        &lt;form action=&quot;&quot;&gt;
            &lt;div&gt;
                &lt;input type=&quot;email&quot; name=&quot;email&quot; id=&quot;email&quot;/&gt;
                &lt;label for=&quot;email&quot;&gt;Email&lt;/label&gt;
            &lt;/div&gt;
            &lt;div&gt;
                &lt;input type=&quot;password&quot; name=&quot;password&quot; id=&quot;password&quot;/&gt;
                &lt;label for=&quot;password&quot;&gt;Password&lt;/label&gt;
            &lt;/div&gt;
        &lt;/form&gt;
    &lt;/div&gt;
&lt;/template&gt;

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, &#39;Segoe UI&#39;, Roboto, Oxygen, Ubuntu,
    Cantarell, &#39;Fira Sans&#39;, &#39;Droid Sans&#39;, &#39;Helvetica Neue&#39;, 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元素的几种方法:

  1. 书中最古老的诀窍:
.form {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%)
}

这通过将表单元素向左和向上移动50%,然后将其宽度和高度的50%返回来实现。

  1. 使用flex:
.form-container {
    display: flex;
    align-items: center;
    justify-content: center;
}

将表单的容器设置为flex,并在垂直和水平方向上将其居中对齐。

  1. 使用grid:
.form-container {
   display: grid;
   place-items: center;
}

与flex相同,但将justify-contentalign-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:

  1. 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.

  1. 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.

  1. 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:

&lt;form class=&quot;absolute top-1/2 left-1/2 translate-x-1/2 translate-y-1/2&quot;&gt;

2.

&lt;div class=&quot;bg-gray-600 flex items-center justify-center&quot;&gt;

3.

&lt;div class=&quot;bg-gray-600 grid place-items-center&quot;&gt;

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.

huangapple
  • 本文由 发表于 2023年1月9日 03:00:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050511.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定