Flexbox父DIV中的项目不居中

时间:2018-04-15 03:20:49

标签: html css css3 flexbox absolute

我在标题中的flex-items(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

1 个答案:

答案 0 :(得分:0)

您可以通过添加最小宽度列表项并将文本对齐到中心来修复它:

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

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

&#13;
&#13;
* {
  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;
  }
}
&#13;
<!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>
&#13;
&#13;
&#13;