美文网首页
CSS居中的几种方式

CSS居中的几种方式

作者: 被遗忘的传说 | 来源:发表于2017-08-16 10:58 被阅读0次

1.水平居中的 margin:0 auto;

<style>
    *{
        padding: 0;
        margin: 0;
    }
        .box{
            width: 300px;
            height: 300px;
            border: 3px solid red;
            /*text-align: center;*/
        }
        img{
            display: block;
            width: 100px;
            height: 100px;
            margin: 0 auto;
        }
    </style>

<!--html-->
<body>
    <div class="box">
        ![](1.jpg)
    </div>
</body>

2.水平居中 text-align:center;

<style>
    *{
        padding: 0;
        margin: 0;
    }
        .box{
            width: 300px;
            height: 300px;
            border: 3px solid red;
            text-align: center;
        }
        img{
            display: inline-block;
            width: 100px;
            height: 100px;
            /*margin: 0 auto;*/
        }
    </style>

<!--html-->
<body>
    <div class="box">
        ![](1.jpg)
    </div>
</body>

3.水平垂直居中 定位和需要定位的元素的margin减去宽高的一半(需要知道宽高)

<style>
        *{
            padding: 0;
            margin: 0;
        }
        .box{
            width: 300px;
            height: 300px;
            background:#e9dfc7; 
            border:1px solid red;
            position: relative;
        }
        img{
            width: 100px;
            height: 150px;
            position: absolute;
            top: 50%;
            left: 50%;
            margin-top: -75px;
            margin-left: -50px;
        }
    </style>
<!--html -->
<body>
    <div class="box" >
        ![](1.jpg)
    </div>
</body>

4.水平垂直居中 定位和margin:auto;(不需要知道宽高)

<style>
        *{
            padding: 0;
            margin: 0;
        }
        .box{
            width: 300px;
            height: 300px;
            background:#e9dfc7; 
            border:1px solid red;
            position: relative;

        }
        img{
            width: 100px;
            height: 100px;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            margin: auto;
        }
    </style>
<!--html -->
<body>
    <div class="box" >
        ![](1.jpg)
    </div>
</body>

5.水平垂直居中绝对定位和transfrom(不需要知道宽高)

<style>
    *{
        padding: 0;
        margin: 0;
    }
    .box{
        width: 300px;
        height: 300px;
        background:#e9dfc7; 
        border:1px solid red;
        position: relative;

    }
    img{
        width: 100px;
        height: 100px;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
    }
</style>
<!--html -->
<body>
    <div class="box" >
        ![](1.jpg)
    </div>
</body>

相关文章

  • CSS常见布局技巧

    1.HTML中css水平居中的几种方式

  • CSS居中小结

    下面是CSS居中的几种方法: 水平居中元素: 通用方法,元素的宽高未知 方式一:CSS3 transform 方式...

  • Css

    1. 介绍一下 CSS 的盒子模型? 2. css 选择器优先级? 3. 垂直居中几种方式? 4. 水平居中几种方...

  • css居中几种方式

    前端经常遇到对div进行水平垂直居中问题,网上也有很多解决方式,但是我们需要根据不同的前提条件和兼容性等来选择合适...

  • CSS布局(不完全)总结

    CSS布局(不完全)总结 实现水平居中布局的几种方式 方法一: 通过以下CSS代码实现水平居中布局 方法二: 通过...

  • CSS居中的几种方式

    1.水平居中的 margin:0 auto; 2.水平居中 text-align:center; 3.水平垂直居中...

  • CSS居中的几种方式

    水平居中方式 1.margig:0 auto;(不能受到float影响) 2.text-align: center...

  • CSS居中的几种方式

    本文主要总结几种常见的CSS居中方式,下面我准备分为三个方向来写,分别是水平居中,垂直居中,水平垂直居中。水平居中...

  • CSS居中的几种方式

    1.水平居中的 margin: 0 auto; 这个用于子元素上的,前提是不受float影响。 2.水平居中的 t...

  • css水平、垂直居中的方法

    css居中常用的几种方式 行内元素水平、垂直居中 方案一(不设置居中元素宽高),代码如下:使用display: t...

网友评论

      本文标题:CSS居中的几种方式

      本文链接:https://www.haomeiwen.com/subject/uqxirxtx.html