ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

CSS超全居中显示

2021-07-03 22:01:47  阅读:145  来源: 互联网

标签:居中 超全 parent color 100px height red background CSS


关于CSS居中的那些事

关于CSS居中的那些事

行内元素

DOM结构

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

水平居中

text-align

.parent {
  background-color: red;
  text-align: center;
}

fit-content

.parent {
  background-color: red;
  width: fit-content;
  margin: auto;
}

垂直居中

line-height

只适用于单行文本

.parent {
  height: 200px;
  line-height: 200px;
  background-color: red;
}

块级元素

DOM结构

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

水平居中

margin: 0 auto;

.parent {
  background-color: red;
}
.child {
  width: 100px;
  margin: 0 auto;
  text-align: center;
  background-color: blue;
}

水平垂直居中

定位

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

或者

.parent {
  position: relative;
  height: 200px;
  background-color: red;
}
.child {
  width: 100px;
  height: 100px;
  position: absolute;
  background: blue;
  left: calc(50% - 50px);
  top: calc(50% - 50px);
}

定位 + transform

.parent {
  position: relative;
  height: 200px;
  background-color: red;
}
.child {
  position: absolute;
  background: blue;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

定位 + margin

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

Padding

.parent {
  padding: 20px;
  background-color: red;
}
.child {
  height: 200px;
  background: blue;
}

flex

.parent {
  height: 200px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: red;
}
.child {
  width: 100px;
  height: 100px;
  background-color: blue;
}

伪元素

.parent {
  height: 200px;
  text-align: center;
  background-color: red;
}
.child {
  width: 100px;
  height: 100px;
  display: inline-block;
  vertical-align: middle;
  background-color: blue;
}
.parent::before {
  content: "";
  width: 20px;
  height: 200px;
  display: inline-block;
  vertical-align: middle;
}

calc(宽高相等)

.parent {
  width: 600px;
  height: 600px;
  background-color: red;
}
.child {
  width: 100px;
  height: 100px;
  padding: calc((100% - 100px) / 2);
  background-clip: content-box;
  background-color: blue;
}

标签:居中,超全,parent,color,100px,height,red,background,CSS
来源: https://blog.csdn.net/qq_41617901/article/details/118445675

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有