谈一谈你对CSS中绝对定位和相对定位的认识
一般而言,父采用相对定位,子采用绝对定位
绝对定位是相对于元素最近的已定位的祖先元素(即是设置了绝对定位或者相对定位的祖先元素)。如果元素没有已定位的祖先元素,那么它的位置则是相对于最初的包含块(body)。
绝对定位本身与文档流无关,因此不占空间,普通文档流中的元素的布局就当绝对定位的元素不存时一样,所以它们可以覆盖页面上其他的元素,且可以通过z-index属性来控制这些层的对方顺序。
相对定位是相对于元素在文档中的初始位置——首先它出现在它所在的位置上(即不设置position时的位置,然后通过设置垂直或水平位置,让这个元素“相对于”它的原始起点进行移动;在使用相对定位时,无论是否进行移动,元素仍然占据原来的空间。因此,移动元素会导致它覆盖其它框。
2、纯CSS三级菜单
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{padding: 0px;margin: 0px;}
header{background:#ee82ee;width:100%;height:100px;margin-bottom: 2px;position: relative;}
/*article{background: #123456;width:32%;height:600px;float:left; }*/
.c01{background:#eee35a;width:37%;height:150px;float:left;margin-right:10px;margin-bottom: 2px;}
.c02{background:#f2e68c;width:37%;height:150px;float:left;margin-bottom: 2px;}
.b01{background:#f08380;width:24%;height:150px; float:right;margin-bottom: 2px;}
.c03{background:#eee35a;width:37%;height:150px;float:left;margin-right:10px;margin-bottom: 2px;}
.c04{background:#f2e68c;width:37%;height:150px;float:left;margin-bottom: 2px;}
.b02{background:#f08380;width:24%;height:150px; float:right;margin-bottom: 2px;}
.c05{background:#eee35a;width:37%;height:150px;float:left;margin-right:10px;margin-bottom: 2px;}
.c06{background:#f2e68c;width:37%;height:150px;float:left;margin-bottom: 2px;}
.b03{background:#f08380;width:24%;height:150px; float:right;margin-bottom: 2px;}
.c07{background:#eee35a;width:37%;height:150px;float:left;margin-right:10px;}
.c08{background:#f2e68c;width:37%;height:150px;float:left;margin-top:2px;}
.b04{background:#f08380;width:24%;height:150px; float:right;margin-bottom: 2px;}
section{height:600px;}
footer{background:#cc9900;width:100%;height:100px;clear:both;margin-top:10px;}
nav{position:absolute;left:250px;bottom:5px;color:blue;font-weight: bold}
ul{list-style: none;}
ul li{float: left;}
ul li a{text-decoration: none;
color:#fff;
background: #00f;
height: 40px;
display: block;
line-height: 40px;
margin-right: 2px;
padding: 0px 10px;
}
ul li a:hover{background: green;}
ul li ul li{position: relative;float:none;width: 150px;border-top:1px solid white;}
ul li ul{position:absolute;width: 190px;display: none;}
ul li:hover ul{display: block;}
ul li:hover ul li ul{display: none;}/*一级菜单下需隐藏三级菜单*/
ul li ul li ul{position:absolute;left:150px;top: 0px;display: none;}/*三级菜单设置*/
ul li ul li:hover ul{display: block;}/*鼠标放在二级菜单上出现三级菜单*/
</style>
</head>
<body>
<header>
<nav>
<ul>
<li><a href="#">学校概况</a></li>
<li><a href="#">管理机构</a></li>
<li><a href="#">学院设置</a>
<ul>
<li><a>电器学院</a></li>
<li><a>能源学院</a></li>
<li><a>计算机科学与技术学院</a>
<ul>
<li><a href="#">信息</a></li>
<li><a href="#">优化</a></li>
<li><a href="#">自然</a></li>
</ul>
</li>
<li><a>矿业学院</a></li>
<li><a>化工学院</a></li>
</ul>
</li>
<li><a href="#">招生就业</a></li>
<li><a href="#">科学研究</a></li>
</ul>
</nav>
</header>
<section>
<article class=c01></article>
<article class=c02></article>
<aside class=b01></aside>
<article class=c03></article>
<article class=c04></article>
<aside class=b02></aside>
<article class=c05></article>
<article class=c06></article>
<aside class=b03></aside>
<article class=c07></article>
<article class=c08></article>
<aside class=b04></aside>
</section>
<footer></footer>
</body>
</html>
3、纯js三级菜单
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="shortcut icon" href="hpu.png">
<style>
*{padding:0px;margin:0px;}
header{background:#8f0;
width:100%;
height:100px;
margin-bottom:3px;
position:relative;
}
section{
width:100%;
height:600px;
/*margin-bottom:5px;*/
}
#a1{background:cyan;
width:30%;
height:600px;
float:left;
margin-right:5px;
}
#a2{background:#d45;
width:39.5%;
height:600px;
float:left;
}
aside{background: purple;
width:29.5%;
height:600px;
float:right;
}
footer{background:#a50;
width:100%;
height:100px;
clear:both;
margin-top:5px;
}
nav{position:absolute;
left:200px;
top:55px;
}
ul{list-style: none;}
ul li{float:left;}
ul li a{display:block;
text-decoration: none;
background:#05f;
color:#fff;
font-weight: bold;
height:40px;
line-height:40px;
margin-right: 1px;
padding:0px 10px;
}
ul li a:hover{background:#f90;}
ul li ul{margin-top:1px;display:none;}
ul li ul li{position: relative;float:none;}
ul li ul li a{border-bottom:1px solid white;}
/*ul li:hover ul{display:block;}*/
ul li ul li ul{position:absolute;left:86px;top: 0px;width: 100px;margin-top:0px;display:none;} /*三级菜单设置*/
ul li ul li ul li{float:none;}
ul li ul li ul li a{border-bottom:1px solid white;}
</style>
</head>
<body>
<header>
<nav>
<ul>
<li><a href="#">学校概况</a></li>
<li><a href="#">管理机构</a></li>
<li onmouseover=showmenu(this) onmouseout=hidemenu(this)><a href="#">院系设置</a>
<ul >
<li><a href="#">电气学院</a></li>
<li><a href="#">安全学院</a></li>
<li onmouseover=showmenu(this) onmouseout=hidemenu(this)><a href="#">材料学院</a>
<ul>
<li><a href="#">信息</a></li>
<li><a href="#">优化</a></li>
<li><a href="#">自然</a></li>
</ul>
</li>
<li><a href="#">能源学院</a></li>
<li><a href="#">资环学院</a></li>
</ul>
</li>
<li><a href="#">招生就业</a></li>
<li><a href="#">科学研究</a></li>
</ul>
</nav>
</header>
<section>
<article id="a1"></article>
<article id="a2"></article>
<aside></aside>
</section>
<footer></footer>
<script >
function showmenu(obj){
var submenu=obj.getElementsByTagName("ul")[0];
submenu.style.display="block";
}
function hidemenu(obj){
var submenu=obj.getElementsByTagName("ul")[0];
submenu.style.display="none";
}
function showmenu(obj){ /*显示三级菜单函数*/
var submenu=obj.getElementsByTagName("ul")[0];
submenu.style.display="block";
}
function hidemenu(obj){ /*隐藏三级菜单函数*/
var submenu=obj.getElementsByTagName("ul")[0];
submenu.style.display="none";
}
</script>
</body>
</html>
4、中英文菜单切换(span是行标签,放在a标签内部使用)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CHS-ENG菜单切换</title>
<style>
*{padding:0px;margin:0px;}
html{background: #ccc;}
div{margin:40px 160px;}
ul{list-style: none;}
ul li{float:left;overflow:auto; /*overflow溢出隐藏掉*/border-bottom: 5px /*红色下划线*/solid red;}
ul li a{display:block;
background: #00f;
color:white;
font-weight: bold;
//height:40px; /*高度得去掉*/
line-height: 40px;
text-align: center;
margin-right:1px;
padding:0px 10px;
text-decoration: none;
}
ul li a:hover{background: #f90;margin-top:-40px;} /*magin-top作用是使中文上移*/
ul li a span{display:none;}
ul li a:hover span{display:block;}
</style>
</head>
<body>
<div>
<ul>
<li><a href="#">学校概况<span>About hpu</span></a></li>
<li><a href="#">管理机构<span>Departments</span></a></li>
<li><a href="#">院系设置<span>Schools</span></a></li>
<li><a href="#">招生就业<span>Enrollment</span></a></li>
<li><a href="#">科学研究<span>Research</span></a></li>
</ul>
</div>
</body>
</html>
5、子菜单鼠标下滑不显示原因:HTML里面那个z-index,默认是0,调大就可以显示出来了
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>vertical-submenu</title>
<style>
*{padding:0px;margin:0px;}
html{background: #ccc;}
div{margin:30px 50px;float:left;}
ul{list-style: none;}
ul li a{display:block;
text-decoration: none;
background: #a00;
width:180px;
color:white;
/*font-weight: bold;*/
height:40px;
line-height: 40px;
border-bottom: 1px solid white;
text-align: center;
}
ul li a:hover{background: #f90;}
ul li{position: relative;}
ul li ul{position:absolute;left:181px;top:0px;display:none;z-index:1;}
ul li:hover ul{display:block;}
#hpu{width:100px;height:100px;background: cyan;float:right;display: none;}
</style>
</head>
<body>
<div>
<ul>
<li><a href="#">学校概况</a></li>
<li><a href="#">管理机构</a></li>
<li><a href="#">院系设置</a>
<ul>
<li><a>电气学院</a></li>
<li><a>计算机科学与技术学院</a></li>
<li><a>安全学院</a></li>
<li><a>材料学院</a></li>
<li><a>化工学院</a></li>
</ul>
</li>
<li><a href="#">招生就业</a></li>
<li><a href="#">科学研究</a>
<!-- <ul>
<li><a>基础研究</a></li>
<li><a>应用研究</a></li>
<li><a>国际合作</a></li>
<li><a>校内基金</a></li>
<li><a>杰出贡献</a></li>
</ul> -->
</li>
</ul>
</div>
<div id="hpu"></div>
</body>
</html>