使用CSS和HTML制作的图像重叠汉堡导航菜单

时间:2019-04-21 22:06:36

标签: html css

我的本​​地教会在这个新网站上遇到了问题。我按照本教程创建了仅包含HTML和CSS的汉堡菜单:https://www.youtube.com/watch?v=xMTs8tAapnQ&list=PLu-osytzWPuUXmu5BZTuHNRyMBpZS1bsG&index=4&t=0s

现在,在学习了如何做之后,我遇到的问题是我试图将图像放在导航菜单下的体内,但是,当我减小Web分辨率并单击burger菜单时,我会遇到这个问题。它在图像下方。

我非常感谢您提供的任何帮助,我尝试在各处寻找它,或者至少尝试过。我是编码新手。因此,感谢您花费时间为我提供帮助。我真的很感激。

 **HTML**

<!DOCTYPE html>
<html>
<head>
<title>Church of Christ</title>
<link rel="stylesheet" type="text/css" href="main.css">
<meta charset="utf-8">
<meta name="viewport" contet="width=device-width, initial-scale=1.0">
</head>
<body>
<h1> The Church of Christ </h1>
<div class="nav">
    <label for="toggle">&#9776;</label>
    <input type="checkbox" id="toggle"/>
    <div class="menu">
        <a href="#">Home</a>
        <a href="#">Contact</a>
        <a href="#">About</a>
        <a href="#"><span>Members</span></a>
    </div>
</div>
  <h2> location </h2>
     <img src="IMG_2597.jpg" alt="Church of Christ">
</body>
</html>

 **CSS**


html, body {width: 100%;
        height: 100%;
        margin: 0;
}
html {
font-family: "helvetica neue", "sans sherif";
}

.nav {
border-bottom: 1px solid gold;
text-align: right;
height: 70px;
line-height: 70px;
}

.menu {
margin: 0 30px 0 0;
}

.menu a {
text-decoration: none;
color: rgb(255, 255, 255);
margin: 0 10px;
line-height: 70px;
clear: right;
}

span {
color: gold;
}

label {
margin: 0 40px 0 0;
font-size: 26px;
line-height: 70px;
display: none;
width: 26px;
float: right;
color: gold;
}

#toggle {
display: none;
}

@media only screen and (max-width: 500px) {
label {
    display: block;
    cursor: pointer;
}
.menu {
    text-align: center;
    width: 100%;
    display: none;   
}
.menu a {
    display: block;
    border-bottom: 1px solid gold;
    margin: 0;
}
#toggle:checked + .menu {
    display: block;
}
}

h1 {
text-align: center;
font-size: 50px;
margin: 10px 0px -10px 0px;
color: gold;
}

 a {
font-size: 25px;
}

h2 {
text-align: center;
font-size: 45px;
color: rgb(255, 255, 255);
}

img {
width: 100%;
height: auto;
}

body {
background-color: rgb(0, 0, 0);
}

2 个答案:

答案 0 :(得分:1)

只需移除CSS clear:right并添加float:right

    .menu {
      margin: 0 30px 0 0;
      float: right;
    }
    .menu a {
      text-decoration: none;
      color: white;
      margin: 0 10px;
      line-height: 70px;
    }

答案 1 :(得分:1)

位置为:相对的元素;相对于其正常位置定位。

设置位置相对的元素的top,right,bottom和left属性将导致将其调整为偏离其正常位置。其他内容将不会进行调整以适合元素所留的任何空隙。

html {
    font-family: "helvetica neue", "sans sherif";
    }
    
    .nav {
    border-bottom: 1px solid gold;
    text-align: right;
    height: 70px;
    line-height: 70px;
    /* this line changed */
    position: relative;
    }
    
    .menu {
    margin: 0 30px 0 0;
    }
     /* i advice,that a tag will made bacgraund-color,because when dropdown menu covering image */
    .menu a {
    text-decoration: none;
    color: rgb(255, 255, 255);
    margin: 0 10px;
    line-height: 70px;
    clear: right;
    /* background-color: blue; */
    }
    
    span {
    color: gold;
    }
    
    label {
    margin: 0 40px 0 0;
    font-size: 26px;
    line-height: 70px;
    display: none;
    width: 26px;
    float: right;
    color: gold;
    }
    
    #toggle {
    display: none;
    }
    
    @media only screen and (max-width: 500px) {
   
    label {
        display: block;
        cursor: pointer;
    }
    .menu {
        text-align: center;
        width: 100%;
        display: none;   
    }
    .menu a {
        display: block;
        border-bottom: 1px solid gold;
        margin: 0;
    }
    #toggle:checked + .menu {
        display: block;
    }
    }
    
    h1 {
    text-align: center;
    font-size: 50px;
    margin: 10px 0px -10px 0px;
    color: gold;
    }
    
     a {
    font-size: 25px;
    }
    
    h2 {
    text-align: center;
    font-size: 45px;
    color: rgb(255, 255, 255);
    }
    
    img {
    width: 100%;
    height: auto;
    }
    
    body {
    background-color: rgb(0, 0, 0);
    }