【问题标题】:how to get responsive hamburger menu to not push down content如何让响应式汉堡菜单不下推内容
【发布时间】:2016-04-18 02:30:00
【问题描述】:

我有这个响应式汉堡导航,目前正在下推它下面的内容。有没有办法让它只悬停在内容上?我尝试了很多东西,我所做的一切都没有,但我不是专家。任何帮助深表感谢!谢谢!

jsfiddle here

<nav class="header">
      <input class="menu-btn" type="checkbox" id="menu-btn" />
      <label class="menu-icon" for="menu-btn"><span class="navicon"></span></label>
      <ul class="menu">
        <li><a href="#work">Our Work</a></li>
        <li><a href="#about">About</a></li>
        <li><a href="#careers">Careers</a></li>
        <li><a href="#contact">Contact</a></li>
      </ul>
    </nav>
<figure id="main-img"></figure>

figure#main-img {
    width: 100%;
    position: relative;
    overflow: hidden;
    border-top: 1px solid #000000;
    border-bottom: 1px solid #000000;
    z-index: 10;
  height: 200px;
  background-color: red;
}
nav.header {
    background-color: silver;
  width: 100%;
  float: left;
  overflow: hidden;
}
.table {
    display: table;   
    margin: 0 auto;
}
.header ul {
    list-style: none; 
    position: relative; 
    float: left; 
    display: block; 
    left: 50%;
    max-width: 1200px;
    margin: 0;
    padding: 0;
}
.header ul li {
    position: relative; 
    right: 50%;
    /*float: left; 
    display: inline-block;*/ 
}
.header li a {
    display: block;
    padding: 1em;
    text-decoration: none;
    color: #000000;
}
.header li a:hover {
    background-color: #B5B5B5;
}
.header .menu {
    clear: both;
    max-height: 0;
    transition: max-height .2s ease-out;
}
.header .menu-icon {
    cursor: pointer;
    display: inline-block;
    float: right;
    padding: 28px 20px;
    position: relative;
    user-select: none;
}
.header .menu-icon .navicon {
    background: #333;
    display: block;
    height: 2px;
    position: relative;
    transition: background .2s ease-out;
    width: 18px;
}
.header .menu-icon .navicon:before, .header .menu-icon .navicon:after {
    background: #333;
    content: '';
    display: block;
    height: 100%;
    position: absolute;
    transition: all .2s ease-out;
    width: 100%;
}
.header .menu-icon .navicon:before {
    top: 5px;
}
.header .menu-icon .navicon:after {
    top: -5px;
}
.header .menu-btn {
    display: none;
}
.header .menu-btn:checked ~ .menu {
    max-height: 240px;
}
.header .menu-btn:checked ~ .menu-icon .navicon {
    background: transparent;
}
.header .menu-btn:checked ~ .menu-icon .navicon:before {
    transform: rotate(-45deg);
}
.header .menu-btn:checked ~ .menu-icon .navicon:after {
    transform: rotate(45deg);
}
.header .menu-btn:checked ~ .menu-icon:not(.steps) .navicon:before, .header .menu-btn:checked ~ .menu-icon:not(.steps) .navicon:after {
    top: 0;
}

@media only screen and (min-width: 750px) {
.header li {
    float: left;
}
.header li a {
    padding: 20px 30px;
}
.header .menu {
    clear: none;
    /*float: right;*/
    max-height: none;
}
.header .menu-icon {
    display: none;
}
}

@media only screen and (max-width: 750px) {
.header li a {

    background-color: Silver;
    opacity: 0.9;
}
.header ul li {
    border-bottom: 1px solid #000000;
    color: #000;
}
.header ul li:first-child {
    border-top: 0px solid #000000;
}
.header ul {
    width: 100%;

}
}
@media only screen and (max-width: 480px) {
nav.header {

}
}

【问题讨论】:

标签: html css hamburger-menu


【解决方案1】:

JSFiddle 中的代码(只有相关的 css,html 没有改变)以防它停止工作:

body {
  width:100%;
  margin:0;
}

figure#main-img {
    width: 100%;
    position: relative;
    float:left;
    overflow: hidden;
    border-top: 1px solid #000000;
    border-bottom: 1px solid #000000;
    height: 300px;
    background-color: red;
    margin:58px 0 0 0;
}

nav.header {
  background-color: silver;
  opacity: 0.75;
  position: absolute;
  width: 100%;
  top:0;
  z-index:10;
  float: left;
  margin:0;
  overflow:hidden;
}
.header .menu-icon {
  padding: 28px 20px;
}

这是您可以使用的JSFiddle

nav.header {
  background-color: silver;
  position: absolute;
  width: 100%;
  top:0;
  z-index:10;
  float: left;
  margin:0;
  overflow:hidden;
}

这里你可以看到我设置的位置是绝对的,设置到顶部然后z-index是一个比默认更大的数字。

希望对您有所帮助,安德鲁

【讨论】:

  • 谢谢!现在发生的事情是导航栏与其下方的图形重叠。我怎样才能让数字低于导航而不是低于它? jsfiddle.net/kimwild/6obpjnzs/6
  • 太棒了!非常感谢您帮我解决这个问题!
  • 我很高兴它成功了,我在答案中添加了 jsfiddle 代码以确保它可供未来的访问者使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-30
  • 2021-06-21
  • 2022-12-18
  • 1970-01-01
  • 2021-08-04
相关资源
最近更新 更多