盒子没有垂直对齐我在顶部添加了一个导航栏

时间:2016-11-17 14:41:49

标签: html css responsive

UPD 请看下面的代码

我正在尝试创建一个主页,顶部有一个导航栏,主页屏幕中间有一个框。我从模板中提出了这个想法。

我的问题是我不能让盒子看起来在中心。

启发我的原始剧本:

<div id="wrapper">
<div id="info">
<div id="info-content">
<h1>Multi level <strong>CSS-only push menu</strong></h1>
</div>
</div>
<div class="menu">
<div class="menu-toggle">Toggle
<ul>
<li><a href="#">Menu-1</a></li>
<li><a href="#">Menu-2</a></li>
<li>
<div class="label">Menu-3
<div class="menu">
<div class="menu-toggle">Toggle
<ul>
<li><a href="#">Menu-3-1</a></li>
<li>
<div class="label">Menu-3-2
<div class="menu">
<div class="menu-toggle">Toggle
<ul>
<li><a href="#">Menu-3-2-1</a></li>
<li><a href="#">Menu-3-2-2</a></li>
<li>
<div class="label">Menu-3-2-3
<div class="menu">
<div class="menu-toggle">Toggle
<ul>
<li><a href="#">Menu-3-2-3-1</a></li>
<li><a href="#">Menu-3-2-3-2</a></li>
<li><a href="#">Menu-3-2-3-3</a></li>
<li><a href="#">Menu-3-2-3-4</a></li>
</ul>
</div>
</div>
</div>
</li>
<li><a href="#">Menu-3-2-4</a></li>
</ul>
</div>
</div>
</div>
</li>
<li><a href="#">Menu-3-3</a></li>
<li><a href="#">Menu-3-4</a></li>
</ul>
</div>
</div>
</div>
</li>
<li>
<div class="label">Menu-4
<div class="menu">
<div class="menu-toggle">Toggle
<ul>
<li><a href="#">Menu-4-1</a></li>
<li><a href="#">Menu-4-2</a></li>
<li><a href="#">Menu-4-3</a></li>
<li><a href="#">Menu-4-4</a></li>
</ul>
</div>
</div>
</div>
</li>
<li>
<div class="label">Menu-5
<div class="menu">
<div class="menu-toggle">Toggle
<ul>
<li><a href="#">Menu-5-1</a></li>
<li><a href="#">Menu-5-2</a></li>
<li><a href="#">Menu-5-3</a></li>
<li><a href="#">Menu-5-4</a></li>
</ul>
</div>
</div>
</div>
</li>
<li><a href="#">Menu-6</a></li>
</ul>
</div>
</div>
<!-- #wrapper --></div>
* {
  margin: 0;
  padding: 0;
 }

html, body{
  height: 100%;
 }
 
body {
   overflow:hidden;
  background: url(http://37.media.tumblr.com/f6c67ec2821a91051e4175f8a102e1e2/tumblr_n6rzpcsMk41st5lhmo1_1280.jpg) 50% 50% / cover
}

#wrapper {
  position: absolute;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

#menu,
#menu ul,
#menu ul li,
#menu ul li a {
  margin: 0;
  padding: 0;
  border: 0;
  list-style: none;
  display: block;
  position: relative;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
#menu:after,
#menu > ul:after {
  content: ".";
  display: block;
  clear: both;
  visibility: hidden;
  line-height: 0;
  height: 0;
}

#menu {
  width: 20px;
  background-color: transparent;
  
}

#menu > ul > li {
  float: left;
}

#menu > ul > li > a {
  padding: 20px;
  font-size: 12px;
  text-decoration: none;
  text-transform: uppercase;
  color: #FFF;
  -webkit-transition: color .2s ease;
  -moz-transition: color .2s ease;
  -ms-transition: color .2s ease;
  -o-transition: color .2s ease;
  transition: color .2s ease;
}
#menu > ul > li:hover > a,
#menu > ul > li.active > a {
  color: #46545d;
  background: #15A6DD;
}

.has-sub {
background: #fff;
}

.has-sub img {
max-width:34px;
max-height:34px;
padding:20px;
}


#menu > ul > li.has-sub > a {
  padding-right: 25px;
}
#menu > ul > li.has-sub > a::after {
  position: absolute;
  top: 21px;
  right: 10px;
  width: 4px;
  height: 4px;
  border-bottom: 1px solid #000000;
  border-right: 1px solid #000000;
  content: "";
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  -o-transform: rotate(45deg);
  transform: rotate(45deg);
  -webkit-transition: border-color 0.2s ease;
  -moz-transition: border-color 0.2s ease;
  -ms-transition: border-color 0.2s ease;
  -o-transition: border-color 0.2s ease;
  transition: border-color 0.2s ease;
}
#menu > ul > li.has-sub:hover > a::after {
  border-color: #8fc647;
}
#menu ul ul {
  position: absolute;
  left: -9999px;
}
#menu li:hover > ul {
  left: auto;
}
#menu ul ul li {
  height: 0;
  -webkit-transition: height .2s ease;
  -moz-transition: height .2s ease;
  -ms-transition: height .2s ease;
  -o-transition: height .2s ease;
  transition: height .2s ease;
}
#menu ul li:hover > ul > li {
  height: 32px;
}
#menu ul ul li a {
  padding: 10px 20px;
  width: 500px;
  font-size: 12px;
  background: #333333;
  text-decoration: none;
  color: #dddddd;
  -webkit-transition: color .2s ease;
  -moz-transition: color .2s ease;
  -ms-transition: color .2s ease;
  -o-transition: color .2s ease;
  transition: color .2s ease;
}
#menu ul ul li:hover > a,
#menu ul ul li a:hover {
  color: #ffffff;
}

#info{
  display: table;
  height: 100%;
  width: 100%;
}
  #info-content{
    display: table-cell;
    vertical-align: middle;
    text-align: center;
    text-transform: uppercase;
    color: #fff;
    font-size: 12px;
  }
  
      h1     { 
      color: #fff;
      border: 3px solid #fff;
      text-align: center;
      font-size: 22px;
      font-weight: normal;
      padding: 20px;
      margin: 10px;
	  margin-top:-54px;
      display: inline-block   ;
    }
      strong{
        display: block;
        font-size: 26px;
      }

与导航栏完美配合的那个但是盒子没有在内容的中心垂直对齐:

<div id="wrapper">
<div id="menu">
<ul>
<li class="has-sub"><img src="https://cdn4.iconfinder.com/data/icons/wirecons-free-vector-icons/32/menu-alt-512.png" alt="" />
<ul>
<li><a href="#" target="_blank">Subfield 1</a></li>
<li><a href="#" target="_blank">SubField</a></li>
</ul>
</li>
</ul>
</div>
<div id="info">
<div id="info-content">
<h1>All Channels <strong>DISPLAY PPC SOCIAL</strong></h1>
</div>
</div>
</div>
margin-top:-54px;

我尝试为h1添加.menu{ position: absolute; top: 0; left: 0; background: #fff; width: 240px; height: 100%; transform: translate3d(-240px, 0, 0); transition: transform 0.35s; } 因为导航按钮正好是54px的高度。

下面我发布了原始图片的样子以及我看起来如何(在添加margin-top之后:-54px)

enter image description here

我想问题依赖于此:

原文:

#menu {
  width: 20px;
  background-color: transparent;
}

MINE:

* {
  margin: 0;
  padding: 0;
 }

html, body{
  height: 100%;
 }
 
body {
   overflow:hidden;
  background: url(http://37.media.tumblr.com/f6c67ec2821a91051e4175f8a102e1e2/tumblr_n6rzpcsMk41st5lhmo1_1280.jpg) 50% 50% / cover
}

#wrapper {
  position: absolute;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

#menu,
#menu ul,
#menu ul li,
#menu ul li a {
  margin: 0;
  padding: 0;
  border: 0;
  list-style: none;
  display: block;
  position: relative;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
#menu:after,
#menu > ul:after {
  content: ".";
  display: block;
  clear: both;
  visibility: hidden;
  line-height: 0;
  height: 0;
}

#menu {
  position: absolute;
  top: 0;
  right: 75%;
  width: 240px;
  height: 100%;
  transform: translate3d(-240px, 0, 0);
  transition: transform 0.35s;
}

#menu > ul > li {
  float: left;
}

#menu > ul > li > a {
  padding: 20px;
  font-size: 12px;
  text-decoration: none;
  text-transform: uppercase;
  color: #FFF;
  -webkit-transition: color .2s ease;
  -moz-transition: color .2s ease;
  -ms-transition: color .2s ease;
  -o-transition: color .2s ease;
  transition: color .2s ease;
}
#menu > ul > li:hover > a,
#menu > ul > li.active > a {
  color: #46545d;
  background: #15A6DD;
}

.has-sub {
background: #fff;
}

.has-sub img {
max-width:34px;
max-height:34px;
padding:20px;
}


#menu > ul > li.has-sub > a {
  padding-right: 25px;
}
#menu > ul > li.has-sub > a::after {
  position: absolute;
  top: 21px;
  right: 10px;
  width: 4px;
  height: 4px;
  border-bottom: 1px solid #000000;
  border-right: 1px solid #000000;
  content: "";
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  -o-transform: rotate(45deg);
  transform: rotate(45deg);
  -webkit-transition: border-color 0.2s ease;
  -moz-transition: border-color 0.2s ease;
  -ms-transition: border-color 0.2s ease;
  -o-transition: border-color 0.2s ease;
  transition: border-color 0.2s ease;
}
#menu > ul > li.has-sub:hover > a::after {
  border-color: #8fc647;
}
#menu ul ul {
  position: absolute;
  left: -9999px;
}
#menu li:hover > ul {
  left: auto;
}
#menu ul ul li {
  height: 0;
  -webkit-transition: height .2s ease;
  -moz-transition: height .2s ease;
  -ms-transition: height .2s ease;
  -o-transition: height .2s ease;
  transition: height .2s ease;
}
#menu ul li:hover > ul > li {
  height: 32px;
}
#menu ul ul li a {
  padding: 10px 20px;
  width: 500px;
  font-size: 12px;
  background: #333333;
  text-decoration: none;
  color: #dddddd;
  -webkit-transition: color .2s ease;
  -moz-transition: color .2s ease;
  -ms-transition: color .2s ease;
  -o-transition: color .2s ease;
  transition: color .2s ease;
}
#menu ul ul li:hover > a,
#menu ul ul li a:hover {
  color: #ffffff;
}

#info{
  display: table;
  height: 100%;
  width: 100%;
}
  #info-content{
    display: table-cell;
    vertical-align: middle;
    text-align: center;
    text-transform: uppercase;
    color: #fff;
    font-size: 12px;
  }
  
      h1     { 
      color: #fff;
      border: 3px solid #fff;
      text-align: center;
      font-size: 22px;
      font-weight: normal;
      padding: 20px;
      margin: 10px;
      display: inline-block   ;
    }
      strong{
        display: block;
        font-size: 26px;
      }

SNIPPET 在全屏模式下有效,但如果我更改屏幕尺寸,按钮会消失:

<div id="wrapper">
<div id="menu">
<ul>
<li class="has-sub"><img src="https://cdn4.iconfinder.com/data/icons/wirecons-free-vector-icons/32/menu-alt-512.png" alt="" />
<ul>
<li><a href="#" target="_blank">Subfield 1</a></li>
<li><a href="#" target="_blank">SubField</a></li>
</ul>
</li>
</ul>
</div>
<div id="info">
<div id="info-content">
<h1>All Channels <strong>DISPLAY PPC SOCIAL</strong></h1>
</div>
</div>
</div>
#menu {
  right: 75%;
}

问题依赖于此

ref

1 个答案:

答案 0 :(得分:1)

为菜单添加以下css:

#menu {
  position: absolute;
  left: 0;
  top: 0;
}

h1中删除负边距。

注意 position: absolute的样式也过多{.1}}。

#wrapper
* {
  margin: 0;
  padding: 0;
}

html, body{
  height: 100%;
}

body {
  overflow:hidden;
  background: url(http://37.media.tumblr.com/f6c67ec2821a91051e4175f8a102e1e2/tumblr_n6rzpcsMk41st5lhmo1_1280.jpg) 50% 50% / cover
}

#wrapper {
  width: 100%;
  height: 100%;
  overflow: hidden;
}

#menu,
#menu ul,
#menu ul li,
#menu ul li a {
  margin: 0;
  padding: 0;
  border: 0;
  list-style: none;
  display: block;
  position: relative;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
#menu:after,
#menu > ul:after {
  content: ".";
  display: block;
  clear: both;
  visibility: hidden;
  line-height: 0;
  height: 0;
}

#menu {
  width: 20px;
  background-color: transparent;
  position: absolute;
  left: 0;
  top: 0;
}

#menu > ul > li {
  float: left;
}

#menu > ul > li > a {
  padding: 20px;
  font-size: 12px;
  text-decoration: none;
  text-transform: uppercase;
  color: #FFF;
  -webkit-transition: color .2s ease;
  -moz-transition: color .2s ease;
  -ms-transition: color .2s ease;
  -o-transition: color .2s ease;
  transition: color .2s ease;
}
#menu > ul > li:hover > a,
#menu > ul > li.active > a {
  color: #46545d;
  background: #15A6DD;
}

.has-sub {
  background: #fff;
}

.has-sub img {
  max-width:34px;
  max-height:34px;
  padding:20px;
}


#menu > ul > li.has-sub > a {
  padding-right: 25px;
}
#menu > ul > li.has-sub > a::after {
  position: absolute;
  top: 21px;
  right: 10px;
  width: 4px;
  height: 4px;
  border-bottom: 1px solid #000000;
  border-right: 1px solid #000000;
  content: "";
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  -o-transform: rotate(45deg);
  transform: rotate(45deg);
  -webkit-transition: border-color 0.2s ease;
  -moz-transition: border-color 0.2s ease;
  -ms-transition: border-color 0.2s ease;
  -o-transition: border-color 0.2s ease;
  transition: border-color 0.2s ease;
}
#menu > ul > li.has-sub:hover > a::after {
  border-color: #8fc647;
}
#menu ul ul {
  position: absolute;
  left: -9999px;
}
#menu li:hover > ul {
  left: auto;
}
#menu ul ul li {
  height: 0;
  -webkit-transition: height .2s ease;
  -moz-transition: height .2s ease;
  -ms-transition: height .2s ease;
  -o-transition: height .2s ease;
  transition: height .2s ease;
}
#menu ul li:hover > ul > li {
  height: 32px;
}
#menu ul ul li a {
  padding: 10px 20px;
  width: 500px;
  font-size: 12px;
  background: #333333;
  text-decoration: none;
  color: #dddddd;
  -webkit-transition: color .2s ease;
  -moz-transition: color .2s ease;
  -ms-transition: color .2s ease;
  -o-transition: color .2s ease;
  transition: color .2s ease;
}
#menu ul ul li:hover > a,
#menu ul ul li a:hover {
  color: #ffffff;
}

#info{
  display: table;
  height: 100%;
  width: 100%;
}
#info-content{
  display: table-cell;
  vertical-align: middle;
  text-align: center;
  text-transform: uppercase;
  color: #fff;
  font-size: 12px;
}

h1     { 
  color: #fff;
  border: 3px solid #fff;
  text-align: center;
  font-size: 22px;
  font-weight: normal;
  padding: 20px;
  margin: 10px;
  display: inline-block   ;
}
strong{
  display: block;
  font-size: 26px;
}