块元素、内联元素、内联块元素
2、块元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>块元素</title>
<style type="text/css">
.box1{
width: 500px;
height: 300px;
margin: 50px auto 0;
border: 1px solid gold;
}
.box1 div{
width: 100px;
height: 100px;
margin: 10px;
background-color: pink;
}
.box2{
font-size: 0px;
width: 600px;
height: 300px;
margin: 50px auto 0;
border: black 1px solid;
/*解决内联元素间隙的方法
将内联元素的父级设置font-size为0,内联元素自身再设置font-size*/
}
.box2 a{
/*text-decoration: none; */
background-color: #5bc0de;
/*width: 300px;*/
/*height: 300px;*/
/*margin: 0 20px;*/
/*margin: 100px 10px; !*margin上下没有作用*!*/
padding: 0 10px;
/*padding: 0 30px; !*padding上下也不支持*!*/
font-size: 16px; /*解决内联元素间隙的方法
将内联元素的父级设置font-size为0,内联元素自身再设置font-size*/
}
.box3{
width: 300px;
height: 300px;
margin: 20px auto 0;
border: 1px black solid;
text-align: center; /*子元素是内联块元素,父元素可以用text-align属性设置子元素水平对齐方式*/
}
</style>
</head>
<body>
<div class="box1">
<div></div>
<div></div>
</div>
<div class="box2">
<a href="#">连接文字一</a>
<a href="#">连接文字二</a>
<a href="#">连接文字三</a>
<a href="#">连接文字二</a>
<a href="#">连接文字三</a>
</div>
<div class="box3">
<a href="#">连接文字</a>
</div>
</body>
</html>
显示效果如下所示:
2、内联块元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>将内联元素转化为内联块元素</title>
<style type="text/css">
.box0{
width: 300px;
height: 300px;
border: 1px solid black;
margin: 30px auto;
font-size: 0px; /*去除掉内联块间隙*/
}
.box0 a{
width: 50px;
height: 50px;
background-color: pink;
padding: 0 5px;
font-size: 16px; /*去除掉内联块间隙*/
display: inline-block; /*转化为内联块元素*/
text-align: center;
line-height: 48px;
}
</style>
</head>
<body>
<div class="box0">
<a href="#">连接1</a>
<a href="#">连接2</a>
<a href="#">连接3</a>
<a href="#">连接4</a>
<a href="#">连接5</a>
</div>
</body>
</html>
3、元素类型转换
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>元素类型转换</title>
<style type="text/css">
.menu{
width: 694px;
height: 50px;
/*background-color: #5bc0de;*/
margin: 50px auto;
font-size: 0px; /*去除掉间隙*/
}
.menu a{
width: 97px;
height: 48px;
background-color: pink;
/*将内联元素转化为内联块元素*/
display: inline-block;
border: 1px solid goldenrod;
font-size: 16px; /*去除掉间隙*/
margin-left: -1px; /*实现合并*/
text-align: center; /*水平居中*/
line-height: 48px; /*垂直居中*/
text-decoration: none; /*去除下划线操作*/
color: magenta;
font-family: "Microsoft YaHei"; /*设置字体样式*/
}
.menu a:hover{
background-color:gold;
color:#fff;
}
</style>
</head>
<body>
<div class="menu">
<a href="#">首页</a>
<a href="#">公司简介</a>
<a href="#">公司简介</a>
<a href="#">公司简介</a>
<a href="#">公司简介</a>
<a href="#">公司简介</a>
<a href="#">公司简介</a>
</div>
</body>
</html>
显示效果如下所示:
示例2:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>元素类型转换_display属性</title>
<style type="text/css">
body{
font-size: 0px;
}
.box{
width: 200px;
height: 200px;
font-size: 16px;
background-color: #5bc0de;
/*display: inline; !*转为内联元素,不支持宽高*!*/
display: inline-block; /*转为内联块元素*/
}
</style>
</head>
<body>
<div class="box">div元素</div>
<div class="box">div元素</div>
</body>
</html>
演示效果入校所示:
4、display属性演示(悬停文字效果):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>display属性</title>
<style type="text/css">
.box{
width: 200px;
}
.box .box2{
width: 200px;
height: 200px;
background-color: #5bc0de;
display: none; /*此div默认是隐藏*/
}
.box:hover .box2{
display: block; /*进行显示块*/
}
</style>
</head>
<body>
<div class="box">
<h3>悬停文字效果</h3>
<div class="box2">说明文字</div>
</div>
</body>
</html>
演示效果如下所示:
练习:使用列表制作菜单
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用列表制作表单</title>
<style type="text/css">
.menu{
width: 694px;
height: 50px;
/*background-color: #5bc0de;*/
/*去除掉小圆点的操作*/
list-style: none;
margin: 50px auto 0;
padding: 0;
font-size: 0;
text-align: center; /*使其内联块居中显示*/
}
.menu li{
width: 97px;
height: 48px;
display: inline-block;
border: 1px solid black;
margin-left: -2px;
font-size: 16px;
line-height: 48px;
font-family: "Microsoft YaHei UI";
}
.menu li a{
width: 97px;
height: 48px;
text-decoration: none;
display: block;
color: pink;
}
.menu li :hover{
background-color: goldenrod;
color: white;
}
.menu li a:hover{
color: white;
}
</style>
</head>
<body>
<!-- ul.menu>(li>a{#})*7-->
<ul class="menu">
<li><a href="#">公司简介</a></li>
<li><a href="#">公司简介</a></li>
<li><a href="#">公司简介</a></li>
<li><a href="#">公司简介</a></li>
<li><a href="#">公司简介</a></li>
<li><a href="#">公司简介</a></li>
<li><a href="#">公司简介</a></li>
</ul>
</body>
</html>