实现页面垂直居中

1.垂直居中absolute+transform

<!DOCTYPE html>
<html lang="en">
<head>    
<meta charset="UTF-8">   
 <title>垂直居中absolute+transform</title> 
   <style>    
    .parent     
   {            
       width: 100px;      
      height: 300px;        
      background: rgba(47, 67, 255, 0.26);    
     /************************注意以下关键代码**************************/      
      display: flex;    
      align-items: center;  
      }      
 
 .child     
   {       
     background: rgba(225, 255, 43, 0.26);  
      }   
 </style>
</head>
<body>
<div class="parent">   
   <div class="child">MM</div>
</div>
</body>
</html>

2.垂直居中flex+align-items

<!DOCTYPE html>
<html lang="en">
<head>   
<meta charset="UTF-8"> 
<title>垂直居中flex+align-items</title>
<style>
 .parent 
 {            
   width: 100px; 
   height: 300px; 
   background: rgba(47, 67, 255, 0.26);    
    /************************注意以下关键代码**************************/      
 position: relative; 
 }       
 .child 
 {            
   width: 100px;  
   height: 100px;            
   background: rgba(225, 255, 43, 0.26);     
     /************************注意以下关键代码**************************/      
   position: absolute;
   top:50%;
   transform: translateY(-50%);
   }    
</style>
</head>
<body>
<div class="parent">
    <div class="child">MM</div>
</div>
</body>
</html>

3.垂直居中table-cell+vertical-align

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>垂直居中table-cell+vertical-align</title>
<style>
.parent
 {
   width: 100px;       
   height: 300px;            
   background: rgba(47, 67, 255, 0.26);                
/************************注意以下关键代码**************************/      
           /*兼容ie6、7要改结构为table*/       
   display: table-cell; 
   vertical-align: middle; 
 }        
.child       
 {            
   width: 100px;            
   height: 100px;            
   background: rgba(225, 255, 43, 0.26);        
}     
</style>
</head>
<body>
<div class="parent">
    <div class="child">MM</div>
</div>
</body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 收听音频,戳链接,旧号itclan已暂停使用,欢迎关注微信itclanCoder公众号可收听更多音频 前言 关于网...
    itclanCoder阅读 12,529评论 3 30
  • 移动开发基本知识点 一.使用rem作为单位 html { font-size: 100px; } @media(m...
    横冲直撞666阅读 8,879评论 0 6
  • H5移动端知识点总结 阅读目录 移动开发基本知识点 calc基本用法 box-sizing的理解及使用 理解dis...
    Mx勇阅读 10,109评论 0 26
  • display:设置元素的显示方式 display:block(块级元素) 默认为父元素宽高,可设置宽高相对前序换...
    bluishwhiteC阅读 3,836评论 0 0
  • 今天罗晋和唐嫣公布恋情了,又被撒了狗粮(๑•ั็ω•็ั๑)﹉ 从小就在幻想自己的爱情是什么样子的,可是等到真正谈...
    你好宋佳阅读 1,670评论 0 0