div居中的几种方式

#pp{

position: absolute;

display: flex;

height: 200px;

width: 200px;

margin: auto;

top: 0;

left: 0;

right: 0;

bottom: 0;

/*text-align: center;*/

background-color: pink;

}

1、绝对定位加top: 0;left: 0;right: 0;bottom: 0;可居中

display: flex;

margin: 0 auto;

align-items: center;

align-self: center;

justify-content: center;

height: 100px;

width: 100px;

background-color: green;

2、使用伸缩盒子(display: flex;)的align-self: center;垂直居中,margin: 0 auto;实现水平居中

3、其中align-items: center;justify-content: center可使子元素居中。

#pp div div div{

position: absolute;

display: table;

height: 50px;

width: 50px;

background-color: black;

top: 50%;

left: 50%;

transform: translate(-50%,-50%);

}

4、position: absolute;top: 50%;left: 50%;transform: translate(-50%,-50%);利用这几个css也可实现居中

#pp div div div div{

display: table-cell;

height: 20px;

width: 20px;

background-color: blue;

}

5、父元素使用display:table,子元素display:table-cell也可实现居中;

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

相关阅读更多精彩内容

  • 各种纯css图标 CSS3可以实现很多漂亮的图形,我收集了32种图形,在下面列出。直接用CSS3画出这些图形,要比...
    剑残阅读 13,297评论 0 8
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,693评论 1 92
  • div水平居中的N种方法 一、单行垂直居中 如果一个容器中只有一行文字,对它实现居中相对比较简单,我们只需要设置它...
    fredah阅读 11,178评论 0 1
  • 聚散 缘份来掌握 缘份 它又是什么 世间业力的结果 从天上忽然降落 一直都让人迷惑 心动 是最美一刻 迷恋 是跳进...
    酒求阅读 2,338评论 0 0
  • HTTP Network Overview HTTP Request and Response URL: unif...
    mlya阅读 3,153评论 0 2

友情链接更多精彩内容