为什么flexbox不能像div中的其他元素那样使我的图像居中?

时间:2019-02-22 01:47:39

标签: html css css3 flexbox alignment

我正在尝试使用flexbox将<table> <thead> <tr> <th>Col 1</th> <th>Col 2</th> <th>Col 3</th> <th>Col 4</th> </tr> </thead> <tbody> <tr> <td>R1C1</td> <td>R1C2</td> <td>R1C3</td> <td>R1C4</td> </tr> <tr> <td>R2C1</td> <td>R2C2</td> <td>R2C3</td> <td>R2C4</td> </tr> </tbody> </table> div中的#login-logo居中,但是图像是唯一没有居中的东西。我以为#login-content会照顾好它。

同一div中的表单,按钮和段落都完全按照我想要的样式进行了样式-彼此堆叠并水平和垂直居中。

text-align:center
* {
  margin: 0;
  padding: 0;
}

html,
body {
  box-sizing: border-box;
  overflow: hidden;
  height: 100%;
}

body {
  min-height: 100%;
  min-width: 100%;
  background: url("images/newnewgirls.jpg");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  background-position: center center;
  position: relative;
}

.container {
  height: 100%;
  width: 100%;
  overflow: hidden;
}

.container2 {
  width: 80%;
  margin: auto;
  text-align: center;
}

header {
  padding: 1em;
  margin: 0;
}

header #branding {
  float: left;
}

header #branding img {
  width: 55%;
}

header nav {
  float: right;
  margin-top: 0.5em;
}

header nav li {
  display: inline;
  padding: 1em;
}

header nav li a {
  text-decoration: none;
}

#login-modal {
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  position: absolute;
  top: 0;
  left: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
}

#login-content {
  height: 80%;
  width: 25%;
  position: relative;
  background-color: white;
  border-radius: 2%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  text-align: center;
}

.close {
  position: absolute;
  top: 0;
  right: 5%;
  cursor: pointer;
  font-size: 30px;
}

.login-input {
  display: block;
  margin: 0 auto 1.5rem auto;
}

#login-logo {
  height: 3rem;
  width: 3rem;
}

5 个答案:

答案 0 :(得分:2)

只需将margin属性设置为:

#login-logo {
    height: 3rem;
    width: 3rem;
    margin: 0 auto;
}

答案 1 :(得分:2)

只需将align-items: center;添加到#login-content

#login-content {
  height: 80%;
  width: 25%;
  position: relative;
  background-color: white;
  border-radius: 2%;
  display: flex;
  align-items: center;
  flex-direction: column;
  justify-content: center;
  text-align: center;
}

答案 2 :(得分:1)

flexbox中,将水平对齐,您可以在align-items: center中将{em> logo {1}}容器-参见下面的演示:

#login-content
* {
  margin: 0;
  padding: 0;
}

html,
body {
  box-sizing: border-box;
  overflow: hidden;
  height: 100%;
}

body {
  min-height: 100%;
  min-width: 100%;
  background: url("images/newnewgirls.jpg");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  background-position: center center;
  position: relative;
}

.container {
  height: 100%;
  width: 100%;
  overflow: hidden;
}

.container2 {
  width: 80%;
  margin: auto;
  text-align: center;
}

header {
  padding: 1em;
  margin: 0;
}

header #branding {
  float: left;
}

header #branding img {
  width: 55%;
}

header nav {
  float: right;
  margin-top: 0.5em;
}

header nav li {
  display: inline;
  padding: 1em;
}

header nav li a {
  text-decoration: none;
}

#login-modal {
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  position: absolute;
  top: 0;
  left: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
}

#login-content {
  height: 80%;
  width: 25%;
  position: relative;
  background-color: white;
  border-radius: 2%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  text-align: center;
  align-items: center; /* ADDED */
}

.close {
  position: absolute;
  top: 0;
  right: 5%;
  cursor: pointer;
  font-size: 30px;
}

.login-input {
  display: block;
  margin: 0 auto 1.5rem auto;
}

#login-logo {
  height: 3rem;
  width: 3rem;
}

答案 3 :(得分:1)

只需添加align-items: center,它将使弹性项目居中(您的flex-directioncolumn

* {
  margin: 0;
  padding: 0;
}

html,
body {
  box-sizing: border-box;
  overflow: hidden;
  height: 100%;
}

body {
  min-height: 100%;
  min-width: 100%;
  background: url("images/newnewgirls.jpg");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  background-position: center center;
  position: relative;
}

.container {
  height: 100%;
  width: 100%;
  overflow: hidden;
}

.container2 {
  width: 80%;
  margin: auto;
  text-align: center;
}

header {
  padding: 1em;
  margin: 0;
}

header #branding {
  float: left;
}

header #branding img {
  width: 55%;
}

header nav {
  float: right;
  margin-top: 0.5em;
}

header nav li {
  display: inline;
  padding: 1em;
}

header nav li a {
  text-decoration: none;
}

#login-modal {
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  position: absolute;
  top: 0;
  left: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
}

#login-content {
  height: 80%;
  width: 25%;
  position: relative;
  background-color: white;
  border-radius: 2%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  text-align: center;
  align-items: center
}

.close {
  position: absolute;
  top: 0;
  right: 5%;
  cursor: pointer;
  font-size: 30px;
}

.login-input {
  display: block;
  margin: 0 auto 1.5rem auto;
}

#login-logo {
  height: 3rem;
  width: 3rem;
}
<!DOCTYPE HTML>
<html>

<head>
  <link rel="apple-touch-icon" sizes="180x180" href="images\free_horizontal_on_white_by_logaster.jpg">
  <link rel="icon" type="image/jpg" sizes="32x32" href="images\free_horizontal_on_white_by_logaster.jpg">
  <link rel="icon" type="image/jpg" sizes="16x16" href="images\free_horizontal_on_white_by_logaster.jpg">
  <meta name="msapplication-TileColor" content="#da532c">
  <meta name="theme-color" content="#ffffff">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="resolve.css">
  <title>Resolve - Real Women, Real Feedback</title>
</head>

<body>
  <header>
    <div class="container">
      <div id="branding">
        <a href="indexresolve.html"><img src="images/lasttry.png" alt="resolvelogo"></a>
      </div>
      <nav>
        <li><a href="indexresolve.html">Home</a></li>
        < <li><a href="howitworks.html">How It Works</a></li>
          <li><a href="contact.html">Contact</a></li>
          <li><a href="faq.html">FAQ</a></li>
          <li><button id="login" class="button">Log In</button></li>
          <div id="login-modal">
            <div id="login-content">
              <span class="close">&times;</span>
              <img id="login-logo" src="images\free_horizontal_on_white_by_logaster.jpg">
              <form>
                <input class="login-input" type="text" placeholder="username">
                <input class="login-input" type="password" placeholder="password">
                <button>Log In</button>
              </form>
              <p>By clicking log in, you agree to our <a href="terms.html">Terms</a>, <a href="privacy.html">Privacy Policy</a>, and our <a href="cookie.html">Cookie Policy</a>.</p>
            </div>
          </div>
      </nav>
  </header>
  <section>
    <div class="container2">
      <div>
        <h1>Guys</h1>
        <h2>fajfsda klfsdajfodisjflkd oisdjfklewjf oisdjfsakfj akfjfslkdja;fj sd;akfjdkfjsdakfj saifjsdakfs.</h2>
        <button>Get Started</button>
      </div>
      <div>
        <h1>Ladies</h1>
        <h2>dklasdjfs kdsjdlk jfsalkjf las;fjdaa fdaksjdk skjfsidjf akldfjskl fjsdlkfjskdlfjsdifjdkf dkfjsdijf s </h2>
        <button id="login">Get Started</button>
      </div>
      <div class="appad">
        <h2>App Coming Soon!</h2>
      </div>
    </div>
    <script src="resolve.js"></script>
</body>

</html>

答案 4 :(得分:0)

您需要将图像包装在div中,以使flex可以像下面的代码一样正确地应用于图像。

还可以添加一个替代方法:

align-self: center 

图像本身

*{
	margin:0;
	padding:0;
}

html, body{
	box-sizing:border-box;
	overflow:hidden;
	height:100%;
}

body{
	min-height:100%;
	min-width:100%;
	background: url("images/newnewgirls.jpg");
	background-size:100% 100%;
	background-repeat: no-repeat;
	background-position:center center;
	position:relative;
 
}
.container{
	height:100%;
	width:100%;
	overflow:hidden;
}

.container2{
	width:80%;
	margin:auto;
	text-align:center;
}

header{
	padding:1em;
	margin:0;
}
header #branding{
	float:left;
}

header #branding img{
	width:55%;
}


header nav{
	float:right;
	margin-top:0.5em;
}

header nav li{
	display:inline;
	padding:1em;
}

header nav li a{
	text-decoration:none;
}

#login-modal{
	width:100%;
	height:100%;
	background-color:rgba(0, 0, 0, 0.5);
	position: absolute;
	top:0;
	left:0;
	display:flex;
	justify-content:center;
	align-items:center;
	text-align:center;

}

#login-content{
	height:80%;
	width:25%;
	position:relative;
	background-color:white;
	border-radius:2%;
	display:flex;
	flex-direction: column;
	justify-content:center;
	text-align:center;
}

.close{
	position:absolute;
	top:0;
	right:5%;
	cursor:pointer;
	font-size: 30px;
}

.login-input{
	display:block;
	margin: 0 auto 1.5rem auto;
}

#login-logo{
	height:3rem;
	width:3rem;
}
<!DOCTYPE HTML>
<html>
<head>
	<link rel="apple-touch-icon" sizes="180x180" href="images\free_horizontal_on_white_by_logaster.jpg">
	<link rel="icon" type="image/jpg" sizes="32x32" href="images\free_horizontal_on_white_by_logaster.jpg">
	<link rel="icon" type="image/jpg" sizes="16x16" href="images\free_horizontal_on_white_by_logaster.jpg">
	<meta name="msapplication-TileColor" content="#da532c">
	<meta name="theme-color" content="#ffffff">
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<link rel="stylesheet" href="resolve.css">
	<title>Resolve - Real Women, Real Feedback</title>
</head>
<body>
	<header>
		<div class="container">
			<div  id="branding">
				<a href="indexresolve.html"><img src="images/lasttry.png" alt="resolvelogo"></a>
			</div>
			<nav>
				<li><a href="indexresolve.html">Home</a></li><
				<li><a href="howitworks.html">How It Works</a></li>
				<li><a href="contact.html">Contact</a></li>
				<li><a href="faq.html">FAQ</a></li>
				<li><button id="login" class="button">Log In</button></li>
				<div id="login-modal">
					<div id="login-content">
						<span class="close">&times;</span>
            <div id="login-logo-container">
						<img id="login-logo" src="https://www.thegamecrafter.com/overlays/smallsquaretile.png">
            </div>
						<form>
							<input class ="login-input" type="text" placeholder="username" >
							<input class ="login-input" type="password" placeholder="password">
							<button>Log In</button>
						</form>
						<p>By clicking log in, you agree to our <a href="terms.html">Terms</a>, <a href="privacy.html">Privacy Policy</a>, and
						our <a href="cookie.html">Cookie Policy</a>.</p>
					</div>
				</div>
			</nav> 
		</header>
	<section>
		<div class="container2">
			<div>
				<h1>Guys</h1>
				<h2>fajfsda klfsdajfodisjflkd oisdjfklewjf oisdjfsakfj akfjfslkdja;fj sd;akfjdkfjsdakfj saifjsdakfs.</h2>
				<button>Get Started</button>
			</div>
			<div>
				<h1>Ladies</h1>
				<h2>dklasdjfs kdsjdlk jfsalkjf las;fjdaa fdaksjdk skjfsidjf akldfjskl fjsdlkfjskdlfjsdifjdkf dkfjsdijf s  </h2>
				<button id="login">Get Started</button>
			</div>
			<div class="appad">
				<h2>App Coming Soon!</h2>
			</div>
		</div>
		<script src="resolve.js"></script>
</body>
</html>