CSS3实现盒子水平居中的效果有几种方式
Admin 2022-05-20 群英技术资讯 673 次浏览
css3让盒子水平居中的方法:1、使用margin属性,给盒子元素添加“margin: 0 auto;”样式即可水平居中;2、利用flex弹性布局来实现水平居中;3、利用position和transform属性实现水平居中。
本教程操作环境:windows7系统、CSS3&&HTML5版、Dell G3电脑。
在CSS中如何让盒子水平居中是很常见的面试题,盒子居中是相对于父元素来说的,因此我们让盒子居中时,往往采用嵌套的方式,让父盒子套着子盒子 。
在父子盒子嵌套下,让子盒子居中的方式:
第一种方法:margin: 0 auto,使用边框,但是margin使用会影响其他盒子的使用,不太推荐使用;
第二种方法:position, 使用定位,子绝父相,再left:50%,margin-left:负的盒子宽度的一半,这是最常用的方法;
第三种方法:flex,弹性布局,让子盒子居中,但是样式要写在父盒子中,display:flex,just-content:center;
第四种方法:在position基础上,把margin-left换成CSS3中的transform:translate(-50px);
第五种方法:在position的基础上,只保留子绝父相,然后在子盒子中加上margin:auto、left:0、right:0;
补充:在第五种方法上,加上top:0,bottom:0,可以实现垂直和水平都居中
<div id="father"> <div id="son"></div> </div>
<style> #father{ width: 400px; height: 200px; border: 3px solid pink; } #son{ width: 100px; height: 100px; border: 2px solid red; } </style>
使用margin实现水平居中:
<style> #father{ width: 400px; height: 200px; border: 3px solid pink; margin: 30px auto; /* 让父元素相对于body居中 */ } #son{ width: 100px; height: 100px; border: 2px solid red; margin: 0 auto;/* 让子元素相对于father居中 */ } </style>
使用定位,子绝父相,再left:50%,margin-left:负的盒子宽度的一半:
<style> #father{ width: 400px; height: 200px; border: 3px solid pink; margin: 0 auto; position: relative; } #son{ width: 100px; height: 100px; border: 2px solid red; position: absolute; left: 50%; margin-left: -50px; } </style>
flex,弹性布局,让子盒子居中,但是样式要写在父盒子中:
<style> #father{ width: 400px; height: 200px; border: 3px solid pink; margin: 0 auto; display: flex; justify-content: center; } #son{ width: 100px; height: 100px; border: 2px solid red; } </style>
在position的基础上,只保留子绝父相,然后在子盒子中加上margin:auto、left:0、right:0:
<style> #father{ width: 400px; height: 200px; border: 3px solid pink; margin: 0 auto; position: relative; } #son{ width: 100px; height: 100px; border: 2px solid red; position: absolute; margin: auto; left: 0; right: 0; } </style>
以上几种方法都可以实现盒子的水平居中,如果大家有其它优(奇)秀(葩)方法,欢迎交流鸭!
第五种方法补充:再加上top:0,bottom:0可以实现水平和垂直都居中 :
<style> #father{ width: 400px; height: 200px; border: 3px solid pink; margin: 0 auto; position: relative; } #son{ width: 100px; height: 100px; border: 2px solid red; position: absolute; margin: auto; left: 0; right: 0; top: 0; bottom: 0; } </style>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了详解纯css实现瀑布流(multi-column多列及flex布局)的相关资料,用multi-column多列布局及flex布局实现瀑布流,感兴趣的小伙伴们可以参考一下
css清除浮动的原因:1、清除浮动是为了清除使用浮动元素的影响。高度塌陷的父元素。2、使用浮动后,子元素会脱离标准文档流,也就是说,父级元素中没有内容可以打开其高度,所以父级元素的height会被忽略。
这篇文章主要介绍了详解css粘性定位position:sticky问题采坑的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
本文介绍的是一个使用伪元素来实现边框逐渐发光的代码,主要用到scale和opacity这两个属性。下面来看看详细的介绍:效果图如下:HTML代码:代码如下复制代码<divclass="light"><imgsrc="http://tva2.sinaimg.cn/crop.0.0.180.180.180/6830a53bjw8f2qo4xzc2zj20500500t0.jpg"/><divclass="light-inner"><p>前端开发博客</p> ...
这篇文章主要介绍了css3实现可拖动的魔方3d效果 ,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008