如何在Reactjs中让Bootstrap卡片保持在屏幕中央

5
我试图让我的卡片停留在屏幕中央,但是没有成功。我使用了align-items和justify-content属性。我查看了不同的网站。

//Login.js

import React, { Component } from 'react';
import './App.css';

class Login extends Component {
  render() {
    return (
      <div className="App">
        <div className="card cardalign w-50">
          <div className="card-body">
          <img className="logo" src="https://cdn.macrumors.com/article-new/2018/05/apple-music-note-800x420.jpg"/>
                <h1 className="signintext">MusicFlo Signin</h1>
                <form className="logininputs">
                <input className="emailinput" placeholder="Email"/>
                <input className="passwordinput" placeholder="Password"/>
                <button className="btn btn-primary btn-md m-2">Log In</button>
            </form>
          </div>
        </div>
      </div>
    );
  }
}

export default Login;

以下是CSS

//App.css

.App {
  text-align: center;
}

.App-logo {
  animation: App-logo-spin infinite 20s linear;
  height: 40vmin;
  pointer-events: none;
}

.App-header {
  background-color: #282c34;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: calc(10px + 2vmin);
  color: white;
}

.App-link {
  color: #61dafb;
}

.logo {
  height:120px;
  width: 220px;
  border-radius: 50%;
  margin-top: 150px
}

.signintext {
  margin-top: 50px;
  font-style: italic
}

.logininputs {
  display:flex;
  flex-direction: column;
  align-items: center
}

.emailinput {
  width: 30vw;
  height: 5vh;
  margin-bottom: 10px;
  border: solid 2px indigo
}

.passwordinput {
  width: 30vw;
  height: 5vh;
  border: solid 2px indigo
}

.card {
  align-items: center;
  justify-content: center;
  background-color: lightgrey;
  border: 5px solid black

}
@keyframes App-logo-spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

我查看了ReactJS的文档,但仍然无法居中显示。请参考图片。谢谢。

enter image description here

任何信息都很好。

上传照片时,它没有显示存在的空格。但基本上,它位于我屏幕的左上角。 - Kenny Quach
可能是 https://dev59.com/e1kT5IYBdhLWcg3wIsEd 的重复问题。 - errorau
可能是如何在Bootstrap 4中居中显示卡片?的重复问题。 - DevJem
2个回答

9
将此添加到应用程序CSS类中。
margin-left: auto;
margin-right: auto;

居中div


1
或者 - margin: 0 auto; - 4ndy

3

这些人:

  align-items: center;
  justify-content: center;

只有在与display: flex一起设置时才起作用。

此外,它们只在弹性容器的子元素上起作用,因此我认为将card设置为弹性容器(假设需要居中的是card)是行不通的。所以应该在card父元素中设置这些属性:

最初的回答:

display: flex;
align-items: center;
justify-content: center;
width: 100%; // make sure the parent is full screen 
height: 100%; // so that the content will center correctly

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接