一、移入效果
hover
给当前属性加上 hover
鼠标移入 .box2
的内容 背景颜色 和文字颜色 都会改
.box2:hover{
background-color: rgb(85, 219, 61);
color: #fff;
}
<div class="box2">1</div>
二、定位 position
位置:
- top:距离上面的位置
- bottom:距离下面的位置
- left:距离左边的位置
- right: 距离右边的位置
1. 相对定位 relative
.box{
width: 200px;
height: 200px;
background-color: red;
position: relative;
}
<div class="box"></div>
2. 绝对定位 absolute
一般来使用就是子绝父相
.box1{
width: 80px;
height: 80px;
background-color: red;
position: absolute;
bottom: 10px; // 距离父盒子底部10px
left: 20px; // 距离父盒子左边10px
}
<div class="box">
<div class="box1">
</div>
</div>
3. 固定定位 fixed
相对于浏览器
固定在一个位置
<style>
.box {
height: 100px;
width: 100%;
position: fixed;
top: 0px; // 离浏览器上面是0px
left: 0px; // 离浏览器左边是0px
background-color: red;
}
.box1 {
height: 3000px;
background-color: rgb(167, 167, 96);
}
</style>
<body>
<div class="box">
这是一个固定定位的元素
</div>
<div class="box1">
wq
</div>
</body>
三、flex布局
1.前言
2009年,W3C提出了一种新的方案—-Flex布局,可以简便、完整、响应式地实现各种页面布局。目前,它已经得到了所有浏览器的支持,这意味着,现在就能很安全地使用这项功能。
2.何为Flex布局
”弹性布局”,用来为盒状模型提供最大的灵活性
任何一个容器都可以指定为Flex布局。
3.使用flex
父盒子box 设置display: flex; 使box里面指定为 flex 布局
<style>
.box{
display: flex;
}
.box_hz{
padding: 20px;
border: 1px solid #000;
}
</style>
<div class="box">
<div class="box_hz">1</div>
<div class="box_hz">2</div>
</div>
4.flex
设置子盒子为响应式
.box_hz{
padding: 20px;
border: 1px solid #000;
flex:1
}
四、容器的属性
1. justify-content 相对x轴
flex-start 默认
- center 移动到父盒子中间
- flex-end 移动到父盒子左边
2. align-items 相对y轴
- flex-start 移动到父盒子上方
- center 移动到父盒子中间
- flex-end 移动到父盒子下方
.box1 {
width: 100%;
height: 100vh;
background-color:pink;
display: flex;
justify-content: center;
align-items: center;
}
.box2 {
height: 30px;
width: 50px;
border: 1px solid #000;
}
<div class="box1">
<div class="box2">1</div>
<div class="box2">2</div>
</div>