温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

css中常用的几种居中方法介绍

发布时间:2021-09-10 20:54:41 来源:亿速云 阅读:156 作者:chen 栏目:web开发

本篇内容主要讲解“css中常用的几种居中方法介绍”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“css中常用的几种居中方法介绍”吧!

今天我们就细数一下几种方法:

1,使用position:absolute,设置left、top、margin-left、margin-top的属性

CSS Code复制内容到剪贴板

  1. .one{   

  2.            position:absolute;   

  3.            width:200px;   

  4.            height:200px;   

  5.            top:50%;   

  6.            left:50%;   

  7.            margin-top:-100px;   

  8.            margin-left:-100px;   

  9.            background:red;    

  10. }  

这种方法基本浏览器都能够兼容,不足之处就是需要固定宽高。

2,使用position:fixed,同样设置left、top、margin-left、margin-top的属性

CSS Code复制内容到剪贴板

  1. .two{   

  2.             position:fixed;   

  3.             width:180px;   

  4.             height:180px;   

  5.             top:50%;   

  6.             left:50%;   

  7.             margin-top:-90px;   

  8.             margin-left:-90px;   

  9.             background:orange;   

  10. }  

大家都知道的position:fixed,IE是不支持这个属性的

3,利用position:fixed属性,margin:auto这个必须不要忘记了。

CSS Code复制内容到剪贴板

  1. .three{   

  2.             position:fixed;   

  3.             width:160px;   

  4.             height:160px;   

  5.             top:0;   

  6.             rightright:0;   

  7.             bottombottom:0;   

  8.             left:0;   

  9.             margin:auto;   

  10.             background:pink;   

  11. }  

4,利用position:absolute属性,设置top/bottom/right/left

CSS Code复制内容到剪贴板

  1. .four{   

  2.             position:absolute;   

  3.             width:140px;   

  4.             height:140px;   

  5.             top:0;   

  6.             rightright:0;   

  7.             bottombottom:0;   

  8.             left:0;   

  9.             margin:auto;   

  10.             background:black;   

  11. }  

5,利用display:table-cell属性使内容垂直居中

CSS Code复制内容到剪贴板

  1. .five{   

  2.             display:table-cell;   

  3.             vertical-align:middle;   

  4.             text-align:center;   

  5.             width:120px;   

  6.             height:120px;   

  7.             background:purple;   

  8. }  

6,最简单的一种使行内元素居中的方法,使用line-height属性

CSS Code复制内容到剪贴板

  1. .six{   

  2.             width:100px;   

  3.             height:100px;   

  4.             line-height:100px;   

  5.             text-align:center;   

  6.             background:gray;   

  7.  }  

这种方法也很实用,比如使文字垂直居中对齐

7,使用css3的display:-webkit-box属性,再设置-webkit-box-pack:center/-webkit-box-align:center

CSS Code复制内容到剪贴板

  1. .seven{   

  2.             width:90px;   

  3.             height:90px;   

  4.             display:-webkit-box;   

  5.             -webkit-box-pack:center;   

  6.             -webkit-box-align:center;   

  7.             background:yellow;   

  8.             color:black;   

  9. }  

8,使用css3的新属性transform:translate(x,y)属性

CSS Code复制内容到剪贴板

  1. .eight{   

  2.             position:absolute;   

  3.             width:80px;   

  4.             height:80px;   

  5.             top:50%;   

  6.             left:50%;   

  7.             transform:translate(-50%,-50%);   

  8.             -webkit-transform:translate(-50%,-50%);   

  9.             -moz-transform:translate(-50%,-50%);   

  10.             -ms-transform:translate(-50%,-50%);   

  11.             background:green;   

  12. }  

这个方法可以不需要设定固定的宽高,在移动端用的会比较多,在移动端css3兼容的比较好

9、最高大上的一种,使用:before元素

CSS Code复制内容到剪贴板

  1. .nine{   

  2.             position:fixed;   

  3.             display:block;   

  4.             top:0;   

  5.             rightright:0;   

  6.             bottombottom:0;   

  7.             left:0;   

  8.             text-align:center;   

  9.             background:rgba(0,0,0,.5);   

  10. }   

  11. .nine:before{   

  12.             content:'';   

  13.             display:inline-block;   

  14.             vertical-align:middle;   

  15.             height:100%;   

  16. }   

  17.  .nine .content{   

  18.             display:inline-block;   

  19.             vertical-align:middle;   

  20.             width:60px;   

  21.             height:60px;   

  22.             line-height:60px;   

  23.             color:red;   

  24.             background:yellow;   

  25. }  

到此,相信大家对“css中常用的几种居中方法介绍”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

css
AI