元素在其父元素垂直水平都居中的方法——笔记

定位方法(1):必须设定宽高

.wrap{

width: 300px;

    height: 300px;

    background: yellow;/*加颜色是为了看界面效果*/

    position: relative;

}

.inner{

  width: 40px;

  height: 40px;

    position: absolute;

    left: 0;

    right: 0;

    top: 0;

    bottom: 0;

    margin:auto;

    background: orange;/*加颜色是为了看界面效果*/

}

定位方法(2)

.wrap{

    width: 300px;

    height: 300px;

    position: relative;

    background: yellow;/*加颜色是为了看界面效果*/

}

.inner{

    width: 40px;

    height: 40px;

    position: absolute;

    left: 50%;

    top: 50%;

    margin-left:-20px;

    margin-top: -20px;

    background: orange;/*加颜色是为了看界面效果*/

}

3. 改变父元素和子元素的display(不推荐使用)

.wrap{

        width: 300px;

        height: 300px;

        display: table-cell;

        text-align: center;

        vertical-align: middle;

        background: yellow;/*加颜色是为了看界面效果*/

    }

    .inner{

        width: 40px;

        height: 40px;

        display: inline-block;

        background: orange;/*加颜色是为了看界面效果*/

    }

4.css3的弹性盒方法

.wrap{

        width: 300px;

        height: 300px;

        display: flex;

        justify-content: center;

        align-items: center;

        background: yellow;

    }

    .inner{

        width: 40px;

        height: 40px;

        background: orange;

    }

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1. tab列表折叠效果 html: 能源系统事业部 岗位名称: 工作地点 岗位名...
    lilyping阅读 5,909评论 0 1
  • 我的公共CSS文件@import "normalize.css";/begin主页面//begin主页面/body...
    蓝色海洋_1982阅读 4,965评论 0 0
  • 1、垂直对齐 如果你用CSS,则你会有困惑:我该怎么垂直对齐容器中的元素?现在,利用CSS3的Transform,...
    kiddings阅读 8,412评论 0 11
  • 外婆!您走了! 可是!我却不在?我昨天晚上还在梦见您!您说您想吃西瓜?如果我在您身边!无论你吃不吃得进...
    待花落阅读 1,643评论 0 0
  • 小城市里的一间小酒馆,老旧木头的味道里洋溢着酒香,灯光昏沉,让墙上原本暗色调的彩绘更加的模糊,整个酒馆里的空角落和...
    sunday08阅读 1,760评论 0 0

友情链接更多精彩内容