我正在创建一个响应式菜单,并且仅使用html和css。
但是当我尝试向网站添加内容时,它会在菜单中启动div。我已经尝试使用溢出:隐藏在整个菜单上的div上但由于某种原因这不起作用。
我创建了另一个菜单,并使用了溢出:隐藏在菜单周围的div上,虽然有效,但很遗憾无法使用它,因为它没有下拉菜单。
我希望我能为您提供足够的信息来帮助我其他明智的问题! 感谢。
@font-face
{
font-family: 'Gautami';
src: url('gautami.ttf');
}
body {
padding: 0px;
margin: 0px;
}
#menu {
width: 100%;
}
/*Strip the ul of padding and list styling*/
ul {
list-style-type: none;
margin: 0;
padding: 0;
position: absolute;
}
/*Create a horizontal list with spacing*/
li {
display: inline-block;
float: left;
}
/*Style for menu links*/
li a {
display: block;
min-width: 140px;
height: 50px;
text-align: center;
line-height: 50px;
font-family: 'Gautami', Arial;
color: #2072ba;
text-decoration: none;
}
/*Hover state for top level links*/
.MenuLink:hover {
background: #e5e6e8;
}
/*Style for dropdown links*/
li:hover ul a {
background: #2072ba;
color: white;
height: 40px;
line-height: 40px;
border-bottom: 1px solid white;
}
/*Hover state for dropdown links*/
li:hover ul a:hover {
background: #e5e6e8;
color: #2072ba;
}
/*Hide dropdown links until they are needed*/
li ul {
display: none;
}
/*Make dropdown links vertical*/
li ul li {
display: block;
float: none;
}
/*Prevent text wrapping*/
li ul li a {
width: auto;
min-width: 100px;
padding: 0 20px;
}
/*Display the dropdown on hover*/
ul li a:hover + .hidden,
.hidden:hover {
display: block;
}
#right {
float: right;
}
/*Style 'show menu' label button and hide it by default*/
.show-menu {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
text-decoration: none;
color: #2072ba;
text-align: center;
padding: 25px 0px 25px 0px;
display: none;
width: 100%;
border-bottom: solid 1px #2072ba;
}
/*Hide checkbox*/
input[type=checkbox] {
display: none;
}
/*Show menu when invisible checkbox is checked*/
input[type=checkbox]:checked ~ #menu {
display: block;
}
.content {
background-color: grey;
bottom: 0px;
margin: 0% 10% 0% 10%;
}
/*Responsive Styles*/
@media screen and (min-width: 1201px) {
#MenuDiv {
overflow: hidden;
}
#menu {
border-bottom: 1px solid #2072ba;
}
}
@media screen and (max-width: 1200px)
/*increase your windows size to see my problem or decrease the max width and min width until your can see the large menu*/
{
#MenuLinkImg {
display: none;
}
/*Make dropdown links appear inline*/
ul {
position: static;
display: none;
padding: 0px;
overflow: hidden;
}
/*Create vertical spacing*/
li {
border-bottom: solid 1px #2072ba;
}
/*Make all menu links full width*/
ul li,
li a {
width: 100%;
}
/*Display 'show menu' link*/
.show-menu {
display: block;
}
#right {
float: none;
}
.content {
margin: 0px 5% 0% 5%;
}
}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Dropdown menu </title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="DD.css">
</head>
<body>
<div id="MenuDiv">
<label for="show-menu" class="show-menu">Show Menu</label>
<input type="checkbox" id="show-menu" role="button">
<ul id="menu">
<li><a class="MenuLink" href="#">Home</a></li>
<li><a class="MenuLink" href="#">Diensten</a></li>
<li><a class="MenuLink" href="#">Keurmerken</a></li>
<div id="right">
<li>
<a class="MenuLink" href="#">Mijn account ↓</a>
<ul class="hidden">
<li><a class="MenuLinkDD" href="#">Diensten</a></li>
<li><a class="MenuLinkDD" href="#">Account informatie</a></li>
</ul>
</li>
<li><a class="MenuLink" href="#">Inloggen</a></li>
</div>
</ul>
</div>
<div class="content">
<div id="text">
testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttestttesttest esttesttesttesttesttest testtesttesttesttesttesttesttesttesttesttesttesttesttestte sttesttesttesttesttesttesttestt esttesttesttest testtesttesttesttesttesttestt esttesttest testtesttesttesttesttesttesttesttesttesttesttestte sttesttes ttesttestt esttesttesttesttesttesttesttesttesttesttest testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttestte sttesttestte sttesttesttesttesttesttesttesttesttesttesttesttesttest
</div>
</div>
</body>
</html>
答案 0 :(得分:1)
你想要什么与overflow无关。
因为在1201px
以上的设备上,您的菜单中包含position:absolute
,因此您要将其从正常的文档流中删除。如果您不希望它与页面内容重叠,则需要在内容上设置顶部填充并将菜单放在提供的空间中:
@media (min-width: 1201px){
body {
padding-top: 51px;
}
#menu {
top: 0;
}
}
@font-face
{
font-family: 'Gautami';
src: url('gautami.ttf');
}
body {
padding: 0px;
margin: 0px;
}
#menu {
width: 100%;
}
/*Strip the ul of padding and list styling*/
ul {
list-style-type: none;
margin: 0;
padding: 0;
position: absolute;
}
/*Create a horizontal list with spacing*/
li {
display: inline-block;
float: left;
}
/*Style for menu links*/
li a {
display: block;
min-width: 140px;
height: 50px;
text-align: center;
line-height: 50px;
font-family: 'Gautami', Arial;
color: #2072ba;
text-decoration: none;
}
/*Hover state for top level links*/
.MenuLink:hover {
background: #e5e6e8;
}
/*Style for dropdown links*/
li:hover ul a {
background: #2072ba;
color: white;
height: 40px;
line-height: 40px;
border-bottom: 1px solid white;
}
/*Hover state for dropdown links*/
li:hover ul a:hover {
background: #e5e6e8;
color: #2072ba;
}
/*Hide dropdown links until they are needed*/
li ul {
display: none;
}
/*Make dropdown links vertical*/
li ul li {
display: block;
float: none;
}
/*Prevent text wrapping*/
li ul li a {
width: auto;
min-width: 100px;
padding: 0 20px;
}
/*Display the dropdown on hover*/
ul li a:hover + .hidden,
.hidden:hover {
display: block;
}
#right {
float: right;
}
/*Style 'show menu' label button and hide it by default*/
.show-menu {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
text-decoration: none;
color: #2072ba;
text-align: center;
padding: 25px 0px 25px 0px;
display: none;
width: 100%;
border-bottom: solid 1px #2072ba;
}
/*Hide checkbox*/
input[type=checkbox] {
display: none;
}
/*Show menu when invisible checkbox is checked*/
input[type=checkbox]:checked ~ #menu {
display: block;
}
.content {
background-color: grey;
bottom: 0px;
margin: 0% 10% 0% 10%;
}
/*Responsive Styles*/
@media screen and (min-width: 1201px) {
#MenuDiv {
overflow: hidden;
}
#menu {
border-bottom: 1px solid #2072ba;
top: 0;
}
body {
padding-top: 51px;
}
}
@media screen and (max-width: 1200px)
/*increase your windows size to see my problem or decrease the max width and min width until your can see the large menu*/
{
#MenuLinkImg {
display: none;
}
/*Make dropdown links appear inline*/
ul {
position: static;
display: none;
padding: 0px;
overflow: hidden;
}
/*Create vertical spacing*/
li {
border-bottom: solid 1px #2072ba;
}
/*Make all menu links full width*/
ul li,
li a {
width: 100%;
}
/*Display 'show menu' link*/
.show-menu {
display: block;
}
#right {
float: none;
}
.content {
margin: 0px 5% 0% 5%;
}
}
&#13;
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Dropdown menu </title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="DD.css">
</head>
<body>
<div id="MenuDiv">
<label for="show-menu" class="show-menu">Show Menu</label>
<input type="checkbox" id="show-menu" role="button">
<ul id="menu">
<li><a class="MenuLink" href="#">Home</a></li>
<li><a class="MenuLink" href="#">Diensten</a></li>
<li><a class="MenuLink" href="#">Keurmerken</a></li>
<div id="right">
<li>
<a class="MenuLink" href="#">Mijn account ↓</a>
<ul class="hidden">
<li><a class="MenuLinkDD" href="#">Diensten</a></li>
<li><a class="MenuLinkDD" href="#">Account informatie</a></li>
</ul>
</li>
<li><a class="MenuLink" href="#">Inloggen</a></li>
</div>
</ul>
</div>
<div class="content">
<div id="text">
testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttestttesttest esttesttesttesttesttest testtesttesttesttesttesttesttesttesttesttesttesttesttestte sttesttesttesttesttesttesttestt esttesttesttest testtesttesttesttesttesttestt esttesttest testtesttesttesttesttesttesttesttesttesttesttestte sttesttes ttesttestt esttesttesttesttesttesttesttesttesttesttest testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttestte sttesttestte sttesttesttesttesttesttesttesttesttesttesttesttesttest
</div>
</div>
</body>
</html>
&#13;
作为旁注,您的CSS 过于笼统 。对于初学者,您不希望网站中的所有无序列表都有position:absolute