您现在的位置是:网站首页> 编程资料编程资料

CSS中的垂直和水平居中完全指南浅析CSS实现水平垂直同时居中的5种思路CSS解决页面图片水平垂直居中问题的方法全面总结使用CSS实现水平垂直居中效果的方法CSS定位“十字架”之水平垂直居中CSS设置未知大小图片在已知大小容器水平垂直居中Flexbox制作CSS布局实现水平垂直居中的简单实例

2021-09-05 1149人已围观

简介 这篇文章主要介绍了CSS中的垂直和水平居中,几乎囊括了各种板式居中的需求,非常推荐!需要的朋友可以参考下

CSS Code复制内容到剪贴板
  1. .link {   
  2.   padding-top30px;   
  3.   padding-bottom30px;   
  4. }  

居中一直是CSS中被抱怨的典型。为什么实现起来这么辛苦?所以有人被嘲笑。我觉得问题不是没有办法做到,只是视情况而定,有很多不同方式,但是很难弄清楚应该用何种方式。因此我写了这篇文章,希望能把他变得容易点。
 水平居中
  内联元素(inline or inline-*)居中?

  你可以让他相对父级块级元素居中对齐
 

CSS Code复制内容到剪贴板
  1. .center-children {   
  2.   text-aligncenter;   
  3. }   

  块级元素(block level)居中?

  你可以通过设置margin-left和margin-right为auto让它居中(同时还要设置width,否则它就会承满整个容器,无法看出居中效果),如。
 

CSS Code复制内容到剪贴板
  1. .center-me {   
  2.   margin: 0 "width: auto; height: auto; float: none;" id="8_nwp">"text-decoration: none;" mpid="8" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=f3e358418dc3f42b&k=auto&k0=auto&kdi0=0&luki=3&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=2bf4c38d4158e3f3&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F5106%2Ehtml&urlid=0" id="8_nwl">"color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">auto;   
  3. }   

  如果有很多块级元素呢?

  如果你有很匀块级元素需要水平居中成一行,你最好使用一个不同的display类型。这是一个使用inline-block和flex的例子。

 

CSS Code复制内容到剪贴板
  1. "inline-block-center">   
  2.   
      
  3.     I'm an element that is block-like with my siblings and we're centered in a row.   
  4.   
  
  •   
      
  •     I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.   
  •   
  •   
  •   
      
  •     I'm an element that is block-like with my siblings and we're centered in a row.   
  •   
  •   
  •   
  •     
  • "flex-center">   
  •   
      
  •     I'm an element that is block-like with my siblings and we're centered in a row.   
  •   
  •   
  •   
      
  •     I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.   
  •   
  •   
  •   
      
  •     I'm an element that is block-like with my siblings and we're centered in a row.   
  •      
  •   
  •     
  • body {   
  •   background#f06d06;   
  •   font-size: 80%;   
  • }   
  • main {   
  •   backgroundwhite;   
  •   margin20px 0;   
  •   padding10px;   
  • }   
  • main div {   
  •   backgroundblack;   
  •   colorwhite;   
  •   padding15px;   
  •   max-width125px;   
  •   margin5px;   
  • }   
  • .inline-"width: auto; height: auto; float: none;" id="5_nwp">"text-decoration: none;" mpid="5" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=f3e358418dc3f42b&k=block&k0=block&kdi0=0&luki=2&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=2bf4c38d4158e3f3&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F5106%2Ehtml&urlid=0" id="5_nwl">"color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">block-center {   
  •   text-aligncenter;   
  • }   
  • .inline-block-center div {   
  •   displayinline-block;   
  •   text-alignleft;   
  • }   
  • .flex-center {   
  •   display: flex;   
  •   justify-contentcenter;   
  • }  
  •  垂直居中
      垂直居中在CSS中有点棘手

      内联元素(inline or inline-*)居中,像文本和链接那样的?
      它是一行的吗?

      有时侯元素可以表现像垂直居中,只是因为它们有相等的上下padding
     
      如果padding因为某些原因不能用,而且文本不会换行的情况下,你可以使用line-height,让其与height相等去对齐文本。
     

    CSS Code复制内容到剪贴板
    1. .center-text-trick {   
    2.   height100px;   
    3.   line-height100px;   
    4.   whitewhite-spacenowrap;   
    5. }  

      它是多行的?

      上下等padding的方式也可以让多行居中,但是如果这方法没用,你可以让这些文字的容器按table cell模式显示,然后设置文字的vertical-align属性对齐,就像talbe那样

     

    CSS Code复制内容到剪贴板
    1.   
    2.   
    3.   
    4.     
    5.  
    6.   
    7.  
    8.   
    9.       I'm vertically centered multiple lines of text in a real table cell.  
    10.     
    11.  
    12.  
    13.   

      I'm vertically centered multiple lines of text in a CSS-created table "width: auto; height: auto; float: none;" id="4_nwp">"text-decoration: none;" mpid="4" target="_blank" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=0&is_app=0&jk=f3e358418dc3f42b&k=layout&k0=layout&kdi0=0&luki=1&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=2bf4c38d4158e3f3&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F5106%2Ehtml&urlid=0" id="4_nwl">"color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">layout.

        
      
  •     
  • body {   
  •   background#f06d06;   
  •   font-size: 80%;   
  • }   
  • table {   
  •   backgroundwhite;   
  •   width240px;   
  •   border-collapseseparate;   
  •   margin20px;   
  •   height提示: 本文由整理自网络,如有侵权请联系本站删除!
    本站声明:
    1、本站所有资源均来源于互联网,不保证100%完整、不提供任何技术支持;
    2、本站所发布的文章以及附件仅限用于学习和研究目的;不得将用于商业或者非法用途;否则由此产生的法律后果,本站概不负责!

  • 相关内容

    -六神源码网