CSS居中

从三个方面来考虑居中

  • 水平居中
  • 垂直居中
  • 水平及垂直居中

公共样式

* {
  box-sizing: border-box;
  margin: 0;
}

body {
  background: #f06d06;
  margin: 10px;
}

main {
  background: white;
  margin: 20px 0;
  padding: 10px;
}
复制代码

1、水平居中

1.1 inlineinline-*元素水平居中

关键代码

text-align: center;
复制代码

样例代码及效果图:

HTML

<main class="inline-X-center">
  <span>i'm centered!</span>
</main>
复制代码

CSS

.inline-X-center {
  text-align: center;
}

.inline-X-center span {
  display: inline-block;
  background: black;
  color: white;
  padding: 10px;
}
复制代码

效果图

1.2 block元素水平居中

关键代码

margin: 0 auto;
复制代码

样例代码及效果图:

HTML

<main class="block-X-center">
  <div>i'm centered!</div>
</main>
复制代码

CSS

.block-X-center div {
  margin: 0 auto;
  max-width: 200px;
  
  background: black;
  color: white;
  padding: 10px;
}
复制代码

效果图

1.3 flex布局实现水平居中

关键代码

display: flex;
justify-content: center;
复制代码

样例代码及效果图:

HTML

<main class="flex-X-center">
  <div>i'm centered!</div>
</main>
复制代码

CSS

.flex-X-center {
  display: flex;
  justify-content: center;
}

.flex-X-center div {
  background: black;
  color: white;
  padding: 10px;
}
复制代码

效果图

2、垂直居中

2.1 inlineinline-*元素垂直居中

关键代码(设置上下内边距相等,模拟垂直居中效果)

padding-top: 30px;
padding-bottom: 30px;
复制代码

样例代码及效果图:

HTML

<main class="inline-Y-center">
  <span>i'm a centered line.</span>
</main>
复制代码

CSS

.inline-Y-center span {
  display: inline-block;
  padding-top: 30px;
  padding-bottom: 30px;

  background: black;
  color: white;
  padding-left: 10px;
  padding-right: 10px;
}
复制代码

效果图

2.2 line-height实现垂直居中

关键代码(只考虑单行文本,不换行)

line-height: 100px;
white-space: nowrap;
复制代码

样例代码及效果图:

HTML

<main class="line-height-Y-center">
  <div>i'm a centered line.</div>
</main>
复制代码

CSS

.line-height-Y-center div {
  line-height: 100px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;

  background: black;
  color: white;
  padding: 10px;
}
复制代码

效果图

2.3 display: table实现垂直居中

关键代码

父元素

display: table;
复制代码

子元素

display: table-cell;
vertical-align: middle;
复制代码

样例代码及效果图:

HTML

<main class="table-Y-center">
  <p>I'm vertically centered text in a CSS-created table layout.</p>
</main>
复制代码

CSS

.table-Y-center {
  display: table;
  height: 200px;
}

.table-Y-center p {
  display: table-cell;
  vertical-align: middle;

  background: black;
  color: white;
  padding: 10px;
}
复制代码

效果图

2.4 flex实现垂直居中

关键代码

display: flex;
flex-direction: column;
justify-content: center;
复制代码

样例代码及效果图:

HTML

<main class="flex-Y-center">
  <p>I'm vertically centered of text in a flexbox container.</p>
</main>
复制代码

CSS

.flex-Y-center {
  display: flex;
  flex-direction: column;
  justify-content: center;
  height: 200px;
}

.flex-Y-center p {
  background: black;
  color: white;
  padding: 10px;
}
复制代码

效果图

2.5 position实现垂直居中

关键代码

position: relative;
top: 50%;
transform: translateY(-50%);
复制代码

样例代码及效果图:

HTML

<main class="position-Y-center">
  <div>I'm a block-level element, centered vertically within my parent.</div>
</main>
复制代码

CSS

.position-Y-center {
  height: 200px;
}

.position-Y-center div {
  position: relative;
  top: 50%;
  transform: translateY(-50%);

  background: black;
  color: white;
  padding: 10px;
}
复制代码

效果图

3、水平及垂直居中

3.1 position实现水平及垂直居中

关键代码

position: relative;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
display: inline-block;
复制代码

样例代码及效果图:

HTML

<main class="position-XY-center">
  <div>I'm a block-level element, centered within my parent.</div>
</main>
复制代码

CSS

.position-XY-center {
  height: 200px;
}

.position-XY-center div {
  position: relative;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  display: inline-block;

  background: black;
  color: white;
  padding: 10px;
}
复制代码

效果图

3.2 flex实现水平及垂直居中

关键代码

display: flex;
justify-content: center;
align-items: center;
复制代码

样例代码及效果图:

HTML

<main class="flex-XY-center">
  <div>I'm a block-level element, centered within my parent.</div>
</main>
复制代码

CSS

.flex-XY-center {
  display: flex;
  justify-content: center;
  align-items: center;

  height: 200px;
}

.flex-XY-center div {
  background: black;
  color: white;
  padding: 10px;
}
复制代码

效果图

3.3 grid实现水平及垂直居中

关键代码

父元素

display: grid;
复制代码

子元素

margin: auto;
复制代码

样例代码及效果图:

HTML

<main class="grid-XY-center">
  <div>I'm centered!</div>
</main>
复制代码

CSS

.grid-XY-center {
  display: grid;

  height: 200px;
}

.grid-XY-center div {
  margin: auto;

  background: black;
  color: white;
  padding: 10px;
}
复制代码

效果图

参考文档

转载于:https://juejin.im/post/5c94d839f265da61014de1e9

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值