【发布时间】: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;
}
}
【问题讨论】:
-
我已经包含了一个演示链接。标题内容似乎没问题。请添加更多详细信息,例如您希望元素如何定位。
-
我认为问题在于您希望 li 项目居中,但 ul 不是 flex 容器