【问题标题】:Items in Flexbox parent DIV not centeringFlexbox 父 DIV 中的项目未居中
【发布时间】:2018-09-25 01:32:36
【问题描述】:

我在标题(flex-parent)中的弹性项目拒绝垂直和水平居中。这很奇怪,因为我是 flex-box 的狂热用户,而且我以前从未遇到过这个问题。我认为这与绝对定位有关,但即使在删除它之后 - 项目仍然没有居中。我已经尝试了一切,希望能得到一些帮助。谢谢。

编辑:好的,看起来内容是真正居中的,实际上是我的列表项中字母的分布使它看起来不居中。不过还是很烦人,不顺眼。

代码:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="style.css">
    <link href="https://fonts.googleapis.com/css?family=Raleway:700" rel="stylesheet">
    <title>Page Title</title>
  </head>
  <body>            
    <header>
      <h2>LOGO</h2>
        <nav>
          <ul>
              <li>HOME</li>
              <li>EVENTS</li>
              <li>CONTACT</li>
          </ul>
        </nav>
    </header>
  <section id="section1">
      <h1>INDIGO NIGHTS</h1>
  </section>   
</body>

CSS:

* {
  margin:0;
  padding: 0;
  box-sizing: border-box;
}

/* HEADER */
header {
  position: absolute;
  width: 100%;
  color:white;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin-top:50px;
}

nav {
  display: flex;
  justify-content: center;
  align-items: center;
}

 ul li {
   display: inline;
   font-family: 'Raleway', sans-serif;
   list-style-type:none;
   padding-left:0.9em;
   padding-right:0.9em;
   font-size: 0.9em;
 }

 /* SECTION 1 */
#section1 {
  height:100vh;
  background-image: url("billboard.jpg");
  background-size: cover;
  background-position: center;
  display: flex;
  justify-content: center;
  align-items: center;
}

h1 {
  font-size:5em;
  color:white;
  text-align: center;
}



@media only screen and (max-width: 795px) {
  #section1 h1 { 
    font-size: 3.5em; 
  }
}

@media only screen and (max-width: 630px) {
  #section1 h1 { 
    font-size: 3em; 
  }
}

Demo link

【问题讨论】:

  • 我已经包含了一个演示链接。标题内容似乎没问题。请添加更多详细信息,例如您希望元素如何定位。
  • 我认为问题在于您希望 li 项目居中,但 ul 不是 flex 容器

标签: html css flexbox absolute


【解决方案1】:

您可以通过为列表项添加最小宽度并将文本居中对齐来解决此问题:

ul li {
    min-width: 100px;
    text-align: center;
}

或者您可以将 flex-basis 指定为 15%(将允许 100/15 = 6 个菜单项在一行中)并将其容器宽度设置为 100% 的列表项:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  outline: 1px solid red;
}


/* HEADER */

header {
  position: absolute;
  width: 100%;
  color: black;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin-top: 50px;
}

nav {
  width: 100%;
  text-align: center;
}

nav>ul {
  display: flex;
  justify-content: center;
  width: 100%;
}

ul li {
  font-family: 'Raleway', sans-serif;
  list-style-type: none;
  font-size: 0.9em;
  flex: 0 0 15%;
  text-align: center;
}


/* SECTION 1 */

#section1 {
  height: 100vh;
  background-image: url("billboard.jpg");
  background-size: cover;
  background-position: center;
  display: flex;
  justify-content: center;
  align-items: center;
}

h1 {
  font-size: 5em;
  color: white;
  text-align: center;
}

@media only screen and (max-width: 795px) {
  #section1 h1 {
    font-size: 3.5em;
  }
}

@media only screen and (max-width: 630px) {
  #section1 h1 {
    font-size: 3em;
  }
}
<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" type="text/css" href="style.css">
  <link href="https://fonts.googleapis.com/css?family=Raleway:700" rel="stylesheet">
  <title>Page Title</title>
</head>

<body>
  <header>
    <h2>LOGO</h2>
    <nav>
      <ul>
        <li>HOME</li>
        <li>EVENTS</li>
        <li>CONTACT</li>
      </ul>
    </nav>
  </header>
  <section id="section1">
    <h1>INDIGO NIGHTS</h1>
  </section>
</body>

【讨论】:

    猜你喜欢
    • 2014-10-21
    • 2020-04-19
    • 2013-03-21
    • 1970-01-01
    • 2019-07-02
    • 2015-02-28
    • 2017-12-11
    • 1970-01-01
    • 2019-11-07
    相关资源
    最近更新 更多