CSS的居中问题

居中

1. 单行文本居中

单反文本居中效果
单行文本居中
<style>
    /** 方式一
    .box {
        height: 150px;
        width: 150px;
        background-color: #e3e3e3;
        text-align: center;
        line-height: 150px;
    }
    */
    /** 第二种方式
    .box {
        height: 150px;
        width: 150px;
        background-color: #e3e3e3;
        display: flex;
        justify-content: center;
        align-items: center;
    }
    */
    /* 第三种方式
    .box {
        height: 150px;
        width: 150px;
        background-color: #e3e3e3;
        display: flex;
        justify-content: center;
        flex-wrap: wrap;
        align-content: center;
    }
     */
</style>
<div class="box">
   这是一行文字
</div>

2. 多行文本居中

多放文本居中
多行文本居中
<style>
    /** 第一种方式 
    .box {
        height: 150px;
        width: 150px;
        background-color: #e3e3e3;
        display: flex;
        justify-content: center;
        align-items: center;
    }   
     */
    /* 第二种方式 
    .box {
        height: 150px;
        width: 150px;
        background-color: #e3e3e3;
        display: flex;
        justify-content: center;
        flex-wrap: wrap;
        align-content: center;
    } 
    */
    .box {
        height: 150px;
        width: 150px;
        background-color: #e3e3e3;
        display:table-cell;
        vertical-align: middle;
    } 
</style>
<div class="box">
    这是多行文字这是多行文字这是多行文字这是多行文字这是多行文字这是多行文字
</div>

3. 块级元素居中

块级元素居中效果
块级元素居中
<style>
    /** 方式一
    .box {
        height: 200px;
        width: 200px;
        background-color: skyblue;
        display: flex;
        justify-content: center;
        align-items: center;
    } 
    .box .inner {
        height: 100px;
        width: 100px;
        background-color: purple;
    }
    */
    /** 方式二
    .box {
        height: 200px;
        width: 200px;
        background-color: skyblue;
        position: relative;
    } 
    .box .inner {
        height: 100px;
        width: 100px;
        background-color: purple;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
    }
    */
    // 第三种方式算是一种奇淫技巧吧
    .box {
        height: 200px;
        width: 200px;
        background-color: skyblue;
        position: relative;
    } 
    .box .inner {
        height: 100px;
        width: 100px;
        background-color: purple;
        position: absolute;
        top: 0;
        left: 0;
        bottom: 0;
        right: 0;
        margin: auto;
    }
</style>
<div class="box">
    <div class="inner"></div>
</div>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在CSS中,居中分为水平居中和垂直居中,在单行文本,多行文本和块级元素上,所使用的方法又不同 做一个小归纳,若您看...
    来颗糖不阅读 1,475评论 0 3
  • 在css布局中,经常会有样式居中的需求。现在列举以下常见的布局方法。首先我们要明确,居中一定有一个参照物。所以会涉...
    David三人行阅读 3,514评论 0 0
  • CSS居中主要分为两类 水平居中 (水平居中分为行内元素居中和块状元素居中 块状元素居中又分为 定宽元素 和 不...
    f3d003f8d312阅读 2,047评论 0 1
  • 通过上篇css 的规律摸索之路(一)css之三角形的规律及变化,我们已经知道css三角形是怎么回事,现在我们来看看...
    侬姝沁儿阅读 3,770评论 0 5
  • 常用居中方法 居中在布局中很常见,我们假设DOM文档结构如下,子元素要在父元素中居中: 水平居中 子元素为行内元素...
    xiao_333阅读 2,600评论 0 0