CSS垂直居中,用ul / li

时间:2017-12-03 12:55:55

标签: html css center centering

我想知道为什么我的id="subnavigation"不会垂直居中,我使用的是top: xxpx;,但由于大多数显示器都不同,我必须将它放在每个显示器的垂直中心。

我尝试使用:

  • justify-content: center
  • align-items: center
  • line-height: normal
  • vertical align-middle&还top: 50%,

但它不会进入正确的位置,任何建议?

HTML:

<!DOCTYPE html>
<html>

<head>
<title>FootDiver!</title>

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="style/style.css" type="text/css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro"
rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Shadows+Into+Light"
rel="stylesheet">

</head>

<body>

<header>
    <nav id="mainnavigation">
         <ul>
            <li><a href="">Startseite</a></li>
            <li><a href="">&Uuml;ber uns</a></li>
            <li><a href="">Join Us!</a>
                <ul>
                    <li><a href="">LogIn</a></li>
                    <li><a href="">Registrierung</a></li>
                </ul>
            </li>
            <li><a href="">Forum</a></li>
            <li><a href="">FAQ</a></li>
            <li><a href="">Newsletter</a></li>
         </ul>
    </nav>
</header>

<div id="subnavigation">
    <nav id="sub">
        <ul>
            <li><a href="">Olympia</a></li>
            <li><a href="">Beef&Beats</a></li>
            <li><a href="">Gantero</a></li>
            <li><a href="">La Roma</a></li>
            <li><a href="">Wok Paradis</a></li>
        </ul>
    </nav>
</div>

<footer>
    <nav id="foot">
        <ul>
            <li><a href="">Impressum</a></li>
            <li><a href="">Datenschutz</a></li>
            <li><a href="">AGB</a></li>
            <li><a href="">Kontakt</a></li>
            <li><a href="">Social Media</a></li>
            <li><a href="">Intern</a></li>
        </ul>
    </nav>
</footer>

<div id="main">
    <h1>Herzlich Wilkommen!</h1>
</div>

</body>
</html>

CSS :(从第95行到第116行)

body, html {
  background: #F4A460;
  margin: 0;
  padding: 0;
  font-size: 62.5%;
}

@media screen and (min-width: 1200px) {
  html {
    font-size: 63%;
  }
}

header {
  background: #8A4B08;
  position: fixed;
  top: 0;
  width: 100%;
  text-align: center;
  transition: 200ms all;
  height: 7rem;
}

header nav#normal {
  color: #000000;
  display: inline-block;
}

header nav#normal ul {
  margin: 0;
  padding: 0;
  float: left;
  list-style: none;
  position: relative;
  transition: all 300ms;
}

header nav#normal ul li {
  margin: 0;
  padding: 0;
  float: left;
  list-style: none;
  position: relative;
  transition: all 300ms;
}

header nav#normal ul li a {
  display: inline-block;
  padding: 2.17rem;
  float: left;
  min-width: 12rem;
  text-align: center;
  font-size: 2rem;
  color: #000000;
  margin: 0 1px 0 0;
  text-decoration: none;
  background: #deb887;
  font-weight: 300;
  transition: all 300ms;
  font-family: 'Source Sans Pro', sans-serif;
}

header nav#normal ul li:hover > a {
  background: #FFD39B;
}

header nav#normal ul li ul {
  overflow: hidden;
  position: absolute;
  top: 100%;
  left: 0;
  width: 100%;
  transition: 300ms all;
  max-height: 0;
}

header nav#normal ul li:hover > ul {
  max-height: 900px;
}

header nav#normal ul li ul li {
  border-bottom: 1px solid  #8A4B08;
}

div#subnavigation {
  background: #8A4B08;
  position: fixed;
  top: 0;
  width: 166px;
  text-align: center;
  transition: 200ms all;
  height: 1200px;
}

div#subnavigation nav#sub {
  display: inline-block;
}

div#subnavigation nav#sub ul {
  margin: auto;
  padding: 0;
  float: left;
  list-style: none;
  position: relative;
  top: 375px;
  transition: all 300ms;
}

div#subnavigation nav#sub ul li {
  margin: 0;
  padding: 0;
  float: left;
  list-style: none;
  position: relative;
  transition: all 300ms;
}

div#subnavigation nav#sub ul li a {
  display: inline-block;
  padding: 2.17rem;
  float: left;
  min-width: 12rem;
  text-align: center;
  font-size: 2rem;
  color: #000000;
  margin: 0 1px 0 0;
  text-decoration: none;
  background: #deb887;
  font-weight: 300;
  transition: all 300ms;
  font-family: 'Source Sans Pro', sans-serif;
  border-bottom: 1px solid #8A4B08;
  vertical-align: middle;
}

div#subnavigation nav#sub ul li:hover > a {
  background: #FFD39B;
}

footer {
  position: fixed;
  right: 0;
  bottom: 0;
  left: 0;
  height: 7rem;
  background-color: #8A4B08;
  text-align: center;
}

footer nav#foot {
  color: #000000;
  display: inline-block;
}

footer nav#foot ul {
  margin: 0;
  padding: 0;
  float: left;
  list-style: none;
  position: relative;
  transition: all 300ms;
}

footer nav#foot ul li {
  margin: 0;
  padding: 0;
  float: left;
  list-style: none;
  position: relative;
  transition: all 300ms;
}

footer nav#foot ul li a {
  display: inline-block;
  padding: 2.17rem;
  float: left;
  min-width: 12rem;
  text-align: center;
  font-size: 2rem;
  color: #000000;
  margin: 0 1px 0 0;
  text-decoration: none;
  background: #deb887;
  font-weight: 300;
  transition: all 300ms;
  font-family: 'Source Sans Pro', sans-serif;
}

footer nav#foot ul li:hover > a {
background: #FFD39B;
}

div#main {
  text-align: left;
  margin-left: 25rem;
  margin-top: 15rem;
}

div#main h1 {
  font-size: 6rem;
  font-family: 'Shadows Into Light', cursive;
}

1 个答案:

答案 0 :(得分:0)

尝试添加

display: flex;
align-items: center;

到要垂直居中的元素的父级。 justify-content: center仅在您使用flex

时有效

Example

相关问题