CSS上下左右居中的办法-实践中

  <div class="container">
    <div class="content">
      <div class="son">
        <span>111111124</span>
        <span>111111124</span>
      </div>
    </div>
  </div>

CSS样式

方法1:定位+transform: translate

前提:父元素为屏幕大小(或固定宽高),子元素固定宽度
父元素:宽、高100%;绝对定位且top,left0
子元素:固定宽度;相对定位且left、top 50%,使用transform: translate相对自身宽度进行移动达到上下居中,左右居中的效果。
缺点:
1、需要具体的高度和宽度——
父元素尺寸固定,子元素高度超过父元素时,子元素上方部分看不到,下方父元素空白。

.container {
  background-color: pink;
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
}
.content {
  background-color: brown;
  position: relative;
  width: 500px;
  left: 50%;
  top: 50%;
  -webkit-transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}

方法2:父元素padding值

前提:父元素设置padding值,子元素宽高不固定
父元素:设置padding值,
子元素:宽高不固定
缺点:父元素高度随子元素变化,不适用于登录,弹框这种固定背景的页面

.container {
  padding: 30%;
  background-color: pink;
}
.content {
  background-color: brown;
}

方法3:line-height+ display: inline-block;

前提:子元素固定宽高
行高如果设置为当前父div的高度(400px)的话,
有固定高度的子div并不会居中显示的,问题出在浏览器默认将其当做文本居中的,
即把它当做了一段文本(chrome默认font-size:16px;hight:21px)进行居中,
没把它当做高度100px进行居中。所以需要对父div的line-height进行调整。
以font-size:0(对应的字体高度为0)为例子,
则需要line-height增加一个子div的高度(400px + 200px;)。

.container3 {
  background-color: pink;
  line-height: 600px;
  height: 400px;
  text-align: center;
  /* width: 100%; */
  font-size: 0;
  /* 注:  */
}
.content3 {
  background-color: brown;
  display: inline-block;
  width: 200px;
  height: 200px;
  font-size: 14px;
  line-height: 200px;
}

方法4:定位(top:50%;left:50%)+margin

前提:
父元素有固定宽高,子元素有固定宽高
子div上下居中:top:50%;margin-top:-h/2; 或是 bottom:50%;margin-bottom:-h/2;
子div左右居中: left:50%;margin-left:-w/2 或是 right:50%;margin-right:-w/2;

.container {
  background-color: pink;
  width: 100%;
  height: 400px;
  position: relative;
}
.content {
  background-color: brown;
  position: absolute;
  bottom: 50%;
  margin-bottom: -50px;
  left: 50%;
  margin-left: -50px;
  width: 100px;
  height: 100px;
}

方法5:定位(top: 0;bottom: 0;left: 0 right: 0;)+margin: auto;

定位属性top和bottom(或是left和right)值分别设置为0,但子div有固定高度(宽度),
并不能达到上下(左右)间距为0,此时给子div设置margin:auto会使它居中显示。
父元素有固定宽高,子元素有固定宽高
父div标记下定位(position:relative|absolute|fixed|sticky);
子div绝对定位(position:absolute)
子div上下居中:top:0;bottom:0;margin-top:auto;margin-bottom:auto
子div左右居中:left:0;right:0;margin-left:auto;margin-right:auto

.container {
  background-color: pink;
  width: 100%;
  height: 400px;
  position: relative;
  box-sizing: border-box;
}
.content {
  background-color: brown;
  width: 100px;
  height: 100px;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
}

方法6:弹性盒子display: flex;

前提:浏览器兼容性支持
弹性盒子,自带的一个居中功能

.container {
  background-color: pink;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 400px;
}
.content {
  background-color: brown;
}

方法7:利用表格单元格的垂直居中属性vertical-align。

父div外层配置一个div,同时设置为表格元素 (display: table),宽度为100%
父div配置为表格单元格元素 (display: table-cell)
父div配置居中属性(vertical-align: middle),使子div上下居中
子div通过margin配置左右居中(margin-left:auto; margin-right:auto)

.container {
  background-color: pink;
  display: table;
  width: 100%;
}
.content {
  background-color: brown;
  /*width: 100%; 见备注1,默认会是100%*/
  height: 400px;
  display: table-cell;
  vertical-align: middle;
}
.son {
  width: 100px;
  height: 100px;
  margin: auto;
  background-color: orange;
}

备注:
1、表格单元格比较特殊,如果只有一个单元格时,它的宽度默认会占父级(table|tr)宽度的100%;
2、table默认宽度不会撑开,需要手动配置width:100%;

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 方法一(varticle-align) 理念 利用表格单元格的居中属性。 步骤 父div外层配置一个div,同时设...
    强哥科技兴阅读 1,074评论 0 0
  • 一、CSS入门 1、css选择器 选择器的作用是“用于确定(选定)要进行样式设定的标签(元素)”。 有若干种形式的...
    宠辱不惊丶岁月静好阅读 1,619评论 0 6
  • 一 外部式css样式 (也可称为外联式)就是把css代码写一个单独的外部文件中,这个css样式文件以“.css...
    KunMitnic阅读 962评论 0 1
  • CSS 是什么 css(Cascading Style Sheets),层叠样式表,选择器{属性:值;属性:值}h...
    崔敏嫣阅读 1,508评论 0 5
  • 我喜欢寂寞的滋味。 不是震慑全身的孤独,不是彻透心窝的凄凉,不是孤立无助的绝望,不是黯然徘徊的怅惘。寂寞,是一种清...
    一生一梦里_f6ed阅读 685评论 0 2