实现div内容水平居中
实现方案一:margin:0 auto;
- div{ height:100px; width:100px; background:red; margin:0 auto; }
登录后复制
- nbsp;html>
div水平居中
登录后复制
实现div水平居中方案二:position:absolute;绝对定位
立即学习“前端免费学习笔记(深入)”;
- div{height:100px;width:100px;background:red;position:absolute; left:50%; right:50%; margin: auto;}
登录后复制
实现div水平垂直居中
实现方案一:position:fixed;固定定位
- div{height:100px;width:100px;background:red;position:fixed;left:0;top:0;bottom:0;right:0;margin:auto; }
登录后复制
实现方案二:position:absolute;绝对定位
- div{height:100px;width:100px;background:red;position:absolute;left:0;top:0;bottom:0;right:0;margin:auto; }
登录后复制
实现方案二:css3+position;
- div{height:100px;width:100px;background:red;position:absolute;left:50%;top:50%;transform:translate(-50%,-50%); }
登录后复制
transform为css3的新增属性,因此需要加上前缀,兼容手机和其他的浏览器。如
- -ms-transform:translate(-50%,-50%); /* IE 9 */-moz-transform:translate(-50%,-50%); /* Firefox */-webkit-transform:translate(-50%,-50%);/* Safari and Chrome */-o-transform:translate(-50%,-50%); /* Opera */
登录后复制
以上就是css+div水平居中的实例代码的详细内容,更多请关注【创想鸟】其它相关文章!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。