关于水平垂直居中的问题

文字水平居中

  p{
       text-align: center;
   }
<p>啦啦啦德玛西亚</p>

文字水平垂直居中

设置padding高度自动撑开

    div{
        text-align: center;
    }
        <div>啦啦啦德玛西亚</div>

flex

   div{

        display: flex;
        justify-content: center;
        align-items: center;
    }
<div>啦啦啦德玛西亚</div>

子元素在父元素中水平垂直居中

方法一 position + margin: auto 实现

.red{
       width: 100px;
       height: 100px;
       background-color: red;
   }
   .blue{
       width: 500px;
       height: 500px;
       background-color: blue;
   }
   .out{
       position: relative;
   }
   .inner{
       position: absolute;
       left: 0;
       right: 0;
       top: 0;
       bottom: 0;
       margin: auto;
   }
<div class="out blue">
        <div class="inner red">
            
        </div>
    </div>

方法二 (子元素已知宽高) position + margin 负值 实现

 .red{
       width: 100px;
       height: 100px;
       background-color: red;
   }
   .blue{
       width: 500px;
       height: 500px;
       background-color: blue;
   }
   .out{
       position: relative;
   }
   .inner{
       position: absolute;
       left: 50%;
       top: 50%;
       margin: -50px 0 0 -50px;
   }
<div class="out blue">
        <div class="inner red">
            
        </div>
    </div>

方法三 (子元素已知宽高) position + transform负值 实现

.red{
       width: 100px;
       height: 100px;
       background-color: red;
   }
   .blue{
       width: 500px;
       height: 500px;
       background-color: blue;
   }
   .out{
       position: relative;
   }
   .inner{
       position: absolute;
       left: 50%;
       top: 50%;
       transform: translate( -50px ,-50px);
   }
 <div class="out blue">
        <div class="inner red">
            
        </div>
    </div>

方法四 flex 实现

.red{
       width: 100px;
       height: 100px;
       background-color: red;
   }
   .blue{
       width: 500px;
       height: 500px;
       background-color: blue;
   }
   .out{
       display: flex;
       justify-content: center;
       align-items: center;
   }
   .inner{
       
   }
<div class="out blue">
        <div class="inner red">
            
        </div>
    </div>

方法五 模拟table实现 子元素设置margin:auto

<style>
   .red{
       width: 100px;
       height: 100px;
       background-color: red;
   }
   .blue{
       width: 500px;
       height: 500px;
       background-color: blue;
   }
   .out{
        display: table-cell;
        vertical-align: middle;
   }
   .inner{
      margin: auto;
   }
</style>
<body>
    <div class="out blue">
        <div class="inner red">
            
        </div>
    </div>
</body>

方法六 模拟table实现 子元素设置inline-block

  <style>
   .red{
       width: 100px;
       height: 100px;
       background-color: red;
   }
   .blue{
       width: 500px;
       height: 500px;
       background-color: blue;
   }
   .out{
        display: table-cell;
        vertical-align: middle;
        text-align: center;
   }
   .inner{
      display: inline-block;
   }
</style>
<body>
    <div class="out blue">
        <div class="inner red">
            
        </div>
    </div>
</body>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,815评论 1 92
  • 收听音频,戳链接,旧号itclan已暂停使用,欢迎关注微信itclanCoder公众号可收听更多音频 前言 关于网...
    itclanCoder阅读 8,216评论 3 30
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 27,750评论 1 45
  • 水平居中方案 行内元素的水平居中对父元素设置 text-align: center; 我是行内元素 在父...
    FConfidence阅读 443评论 0 1
  • CSS 是什么 css(Cascading Style Sheets),层叠样式表,选择器{属性:值;属性:值}h...
    崔敏嫣阅读 1,512评论 0 5