首页 > 解决方案 > 为什么 flexbox 不像 div 中的其他元素那样居中我的图像?

问题描述

我正在尝试使用 flexbox#login-logo#login-contentdiv 中居中,但图像是唯一没有居中的东西。我以为text-align:center会照顾好它。

同一个 div 中的表单、按钮和段落的样式都完全符合我的要求 - 彼此堆叠并水平和垂直居中。

* {
  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>
              <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>

标签: htmlcssflexboxalignment

解决方案


您可以通过添加边距属性使徽标居中:

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

推荐阅读