初识html+css 5
1. 相对定位
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box1{
width: 200px;
height: 200px;
background-color: red;
/* position: static; */
}
.box2{
width: 200px;
height: 200px;
background-color: blue;
position: relative;
left: 200px;
/* top: 200px; */
/* margin-left: 200px;
margin-top: 200px; */
/* margin-top: ; */
}
.box3{
width: 200px;
height: 200px;
background-color: green;
/* margin-top: -200px; */
}
/*
定位:默认值 static 没有定位
相对定位 relative 相对于自身的位置进行定位 设置偏移量是使用 top left right bottom
不脱离文档流
不影响其他的元素
绝对定位 absolute
固定定位 fixed
*/
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</body>
一般到后期盒子未定义大小里面的字体直接通过padding 来进行水平垂直居中对齐
2. 绝对定位
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box1 {
width: 200px;
height: 200px;
background-color: red;
}
.box2 {
width: 200px;
height: 200px;
background-color: green;
position: absolute;
/* left: 0px;
top: 0px; */
/* top: 200px;
left: 200px; */
}
.box3 {
width: 100px;
height: 100px;
background-color: blue;
position: absolute;
top: 20px;
left: 20px;
}
.box4 {
width: 300px;
height: 300px;
background-color: pink;
position: relative;
top: 100px;
left: 100px;
}
.box5 {
width: 500px;
height: 600px;
background-color: skyblue;
position: relative;
top: 100px;
left: 100px;
}
</style>
</head>
<!-- 绝对定位 absolute 相对于 最近的已经定位的祖先元素进行定位,
如果所有的祖先元素都没有定位的话,就相对于浏览器进行定位
设置了绝对定位的元素脱离标准流,同时也无法继承父元素的宽高100%
-->
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box5">
<div class="box4">
<div class="box3"></div>
</div>
</div>
</body>
3. fixed
一般用在至顶部还有一些功能键
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin:0;
padding:0;
list-style:none;
}
.box{
width: 1200px;
height: 2000px;
margin:0 auto;
border:2px solid #000;
position: relative;
}
.box1{
width: 100px;
height: 100px;
margin:20px ;
background-color: pink;
position: fixed;
}
</style>
</head>
<body>
<div class="box">
<div class="box1">
</div>
</div>
</body>
4. hover
hover是鼠标移动到某一位置出现什么现象,之前a链接标签伪类有提到过,这主要介绍父子之间的hover现象,以及兄弟之间的hover现象
- 父子hover
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.fa{
width: 500px;
height: 500px;
background-color: royalblue;
display: none;
}
.son{
width: 100px;
height: 100px;
background-color: red;
display: none;
}
.fa:hover .son{
display: block;
}
</style>
</head>
<body>
<div class="fa">
<div class="son">
</div>
</div>
</body>
- 兄弟hover
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box1{
width: 200px;
height: 200px;
background-color: rebeccapurple;
}
.box2{
width: 200px;
height: 200px;
background-color: red;
/* display: inline-block; */
display: none;
/* opacity: 0; 改变盒子的透明度 还在文档流当中*/
/* opacity: 0; */
}
.box3{
width: 200px;
height: 200px;
background-color: skyblue;
display: none;
}
.box1:hover+.box2{
/* 只能对紧挨这的后面的兄弟元素设置hover */
display: block;
}
.box1:hover~.box3{
/*对后面的兄弟元素设置hover */
display: block;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</body>