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

问题描述 投票:1回答:5

我试图用qzxswpoi div在#login-logo中心使用flexbox,但图像是唯一没有居中的东西。我以为#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;
}
html css css3 flexbox alignment
5个回答
2
投票

您可以在中心添加徽标​​,只需添加保证金属性:

<!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>

2
投票

只需将#login-logo { height: 3rem; width: 3rem; margin: 0 auto; } 添加到align-items: center;

#login-content

1
投票

#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; } 列中水平对齐,您可以使用flexboxalign-items: center容器中的徽标 - 请参阅下面的演示:

#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;
}

1
投票

只需添加<!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>,它将灵活项目居中(你的align-items: centerflex-direction

column
* {
  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;
}

0
投票

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

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

<!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>

到图像本身

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;
}
© www.soinside.com 2019 - 2024. All rights reserved.