标签:居中 color 50% 300px height 垂直 width background 几种
html
<div class="box">
<p class="inner">hello word</p>
</div>
1flex布局
利用justify-content: center;
实现水平居中,aline-items:center
实现垂直居中;
.box{
width: 300px;
height: 300px;
background-color: aqua;
display: flex;
justify-content: center;
align-items: center;
}
.inner{
background-color: brown;
}
或则利用gruid
布局
.box{
width: 300px;
height: 300px;
background-color: aqua;
display: gruid;
}
.inner{
background-color: brown;
}
复制代码
或则利用flex
布局和margin:auto
.box{
width: 300px;
height: 300px;
background-color: aqua;
display: flex;
}
.inner{
background-color: brown;
margin:auto;
}
2、利用相对定位和绝对定位和margin:auto
.box{
width: 400px;
height: 400px;
background-color: aqua;
position: relative;
}
.inner{
width: 200px;
height: 200px;
background-color: brown;
position: absolute;
margin: auto;
left: 0;
top: 0;
bottom: 0;
right: 0;
}
3、利用相对定位和绝对定位,外边距平移
相对定位下,使用绝对定位,利用margin
偏移外容器的50%,再利用translate
平移回补自身宽高的50%
.box{
width: 400px;
height: 400px;
background-color: aqua;
position: relative;
}
.inner{
width:200px;
height:200px;
background-color: brown;
position: absolute;
margin: 0;
left: 50%;
top: 50%;
transform:translate(-50%,-50%);
}
css元素水平垂直居中的十种方法
标签:居中,color,50%,300px,height,垂直,width,background,几种 来源: https://blog.csdn.net/weixin_45441470/article/details/123191841
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。