美文网首页让前端飞
CSS盒模型的几种居中方式

CSS盒模型的几种居中方式

作者: 前端王祖蓝 | 来源:发表于2021-01-05 14:07 被阅读0次

css实现盒子模型居中方式

<div class="parent">
    <div class="child"></div>
</div>

1.绝对定位 margin负值

.parent{
    width:600px;
    height:600px;
    margin:auto;
    position: relative;
    background-color: #000;
 }
.child{
    width:200px;
    height:200px;
    background-color: red;
    position: absolute;
    top:50%;
    left:50%;
    margin:-100px 0 0 -100px;
}

2.绝对定位 四个方位为0

.parent{
    width:600px;
    height:600px;
    margin:auto;
    position: relative;
    background-color: #000;
}
.child{
    width:200px;
    height:200px;
    background-color: red;
    position: absolute;
    top:0;
    left:0;
    right:0;
    bottom:0;
    margin:auto;
}

3.绝对定位 translate

.parent{
    width:600px;
    height:600px;
    margin:auto;
    position: relative;
    background-color: #000;
}
.child{
    width:200px;
    height:200px;
    background-color: red;
    position: absolute;
    top:50%;
    left:50%;
    transform:translate(-50%,-50%)
}

4.margin属性居中

.parent{
    width:600px;
    height:600px;
    margin:auto;
        overflow: hidden;
    background-color: #000;
}
.child{
    width:200px;
    height:200px;
    background-color: red;
    margin: 150px auto 0;
}

5.table-cell

.parent{
    width:600px;
    height:600px;
        background-color: #000;
        display: table-cell;
        vertical-align: middle;
}
.child{
    width:200px;
    height:200px;
    background-color: red;
    margin: 0 auto;
}

6.flex 居中

.parent{
    width:600px;
    height:600px;
        display: flex;
        justify-content: center;
        align-items: center;
    background-color: #000;
    margin:0 auto;
}
.child{
    width:200px;
    height:200px;
    background-color: red;

}

相关文章

  • CSS盒模型的几种居中方式

    css实现盒子模型居中方式 1.绝对定位 margin负值 2.绝对定位 四个方位为0 3.绝对定位 transl...

  • Css

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

  • Good Luck

    概念 HTML以及CSS篇,集中在CSS 说下你常用的几种布局方式,集中往盒模型、flex布局说(至于grid布局...

  • CSS常见布局技巧

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

  • 2022css面试题总结

    H5 的新特性 css3 新特性 div 盒子居中 css 的弹性盒模型和怪异盒模型 css 实现三角形 浏览器兼...

  • CSS居中小结

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

  • CSS3Flex和圣杯布局

    一、css3盒模型 css3增加了盒模型属性box-sizing,能够事先定义盒模型的尺寸解析方式。box-siz...

  • CSS

    Q1.简单描述CSS的盒模型 1.盒模型种类:包括标准盒模型和IE盒模型(怪异盒模型) 2.盒模型设置方式:通过b...

  • css居中几种方式

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

  • CSS布局(不完全)总结

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

网友评论

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

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