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

CSS中几种居中方式

作者: August007_0751 | 来源:发表于2018-11-07 19:46 被阅读0次

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

(用于子元素前提是不受float影响)<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">

        ![](img1.jpg)

    </div>

</body>

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

img的display:block;类似一样在不受float影响下进行,是在父元素上添加效果让它进行水平居中

<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>

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>

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>

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%);

    }

 6.水平垂直居中(四)diplay:table-cell

<style>

    .box{

            width: 300px;

            height: 300px;

            background:#e9dfc7;

            border:1px solid red;

            display: table-cell;

            vertical-align: middle;

            text-align: center;

        }

        img{

            width: 100px;

            height: 150px;

            /*margin: 0 auto;*/  这个也行

        }

</style>

7.水平垂直居中(五)flexBox居中这个用了C3新特性flex,非常方便快捷,在移动端使用完美,pc端有兼容性问题,以后会成为主流的

<style>

    .box{

            width: 300px;

            height: 300px;

            background:#e9dfc7;

            border:1px solid red;

            display: flex;

            justify-content: center;

            align-items:center;

        }

        img{

            width: 150px;

            height: 100px;

        }

</style>

相关文章

  • CSS常见布局技巧

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

  • CSS居中小结

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

  • CSS中几种居中方式

    1.水平居中的margin:0 auto; (用于子元素前提是不受float影响) *{ paddin...

  • Css

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

  • css居中几种方式

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

  • CSS布局(不完全)总结

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

  • CSS居中的几种方式

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

  • 初识CSS布局

    本文将简单学习几种基本的CSS布局方式:左右布局,左中右布局,水平居中,垂直居中。 左右布局 本文只介绍两种最基础...

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

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

  • CSS居中各种情况的解决情况

    CSS居中的几种情况 这里参考How to center anything with css 1. 元素水平居中 ...

网友评论

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

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