没有合适的资源?快使用搜索试试~ 我知道了~
div基本布局 <div class=main> <div class=center></div> </div> css样式 1. 配合定位与margin:auto 父元素加相对定位,子元素加绝对定位 .main{ width: 300px; height: 300px; background-color: red; position: relative; } .center{ width: 100px; height: 100px; background-color: skyblue; pos
资源详情
资源评论
资源推荐
CSS实现子元素实现子元素div水平垂直居中的示例水平垂直居中的示例
div基本布局
<div class="main">
<div class="center"></div>
</div>
css样式
1. 配合定位与配合定位与margin::auto
父元素加相对定位,子元素加绝对定位
.main{
width: 300px;
height: 300px;
background-color: red;
position: relative;
}
.center{
width: 100px;
height: 100px;
background-color: skyblue;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
}
2.利用利用flex布局,设置水平与竖直方向的内容居中。布局,设置水平与竖直方向的内容居中。
.main{
width: 300px;
height: 300px;
background-color: red;
display: flex;
justify-content: center;
align-items: center;
}
.center{
width: 100px;
height: 100px;
background-color: greenyellow;
}
3.利用利用position:absolute与与transform
:这里需要记住的是transform中translate使用百分比时相对的是自己的长宽,不是父盒子的。
.main{
width: 300px;
height: 300px;
background-color: red;
position: relative;
}
.center{
width: 100px;
height: 100px;
background-color: pink;
position: absolute;
left: 50%;
top: 50%;
transform: translateX(-50%) translateY(-50%);
}
4.定位定位 与负与负margin配合配合
只适合子盒子长宽固定的情况
.main{
weixin_38692836
- 粉丝: 4
- 资源: 974
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0