博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
元素水平垂直居中(transform,margin,table-cell,jQuery)
阅读量:4349 次
发布时间:2019-06-07

本文共 1396 字,大约阅读时间需要 4 分钟。

1.水平居中

.div{
margin:0 auto; (或者 margin:auto;) width:500px; height:300px; }

2.使用margin水平垂直居中

方式一:

.div {
text-align: center; line-height: 200px; border: 2px pink solid; width: 300px; height: 200px; position: absolute; left: 50%; top: 50%; margin: -100px 0 0 -150px;}

方式二:

            块级元素水平,垂直居中        

3.jquery实现DIV水平垂直居中

.div {
text-align: center; line-height: 200px; border: 2px pink solid; width: 300px; height: 200px; }
< script >$(window).resize(function(){     $(".div").css({         position: "absolute",         left: ($(window).width() - $(".div").outerWidth())/2,         top: ($(window).height() - $(".div").outerHeight())/2     });   }); $(function(){     $(window).resize(); }); < /script >

4.使用css3 tansform属性

            块级元素水平,垂直居中        
adfagagafajkfhla

效果如下:

单独设置垂直居中可使用:

top:50%;tansfrom:translateY(-50%);

单独使用水平居中可使用:

left:50%;tramsform:translateX(-50%);

5.table-cell

注意:可能会破坏页面整体布局

            块级元素水平,垂直居中        
adfagagafajkfhla

效果如下:

6.使用示例:DIV创建水平垂直居中遮罩层

Div层居中

效果:

 

 

 

转载于:https://www.cnblogs.com/wishyouhappy/p/3681037.html

你可能感兴趣的文章
vector
查看>>
Redis学习之set类型总结
查看>>
栈和队列
查看>>
CSS2-3常见的demo列子总结一
查看>>
第522篇--DataTable to Excel C#
查看>>
C++\virtual 虚函数、纯虚函数
查看>>
asp.net mvc 4多级area实现技巧
查看>>
Solr
查看>>
MySQL binlog数据库同步技术总结
查看>>
静态方法
查看>>
保护HTTP的安全
查看>>
js 选取子节点时去除非IE浏览器的换行符
查看>>
javascript是一朵奇葩
查看>>
Mycat入门教程
查看>>
关于"无法解析的外部符号"问题的解决
查看>>
【JavaScript】【译】编写高性能JavaScript
查看>>
【随笔】入行必读:互联网行业薪酬等级
查看>>
Android使用开源框架加载图片
查看>>
CLR是怎么加载到内存的?
查看>>
fckeditor
查看>>