css水平垂直居中块整理

1、绝对定位+负margin

兼容性很好,但需要指定子块的高度和宽度,以及负margin

.wp{
    position: relative;
    width: 200px;
    height: 200px;
    background-color: #ccc;
}
.test{
    height: 100px;
    position: absolute;
    top:50%;
    left: 50%;
    margin-top: -50px;
    margin-left: -50px;
    background-color: #edd;
    width: 100px;
}
<div class="wp">
    <div class="test"></div>
</div>

2、绝对定位加margin:auto

缺点:IE6\7不支持,需要指定子块的宽度和高度

.wp{
    position: relative;
    width: 200px;
    height: 200px;
    background-color: #ccc;
}
.test {
    position:absolute;
    top:0;
    bottom:0;
    left:0;
    right:0;
    margin:auto;
    height:100px;
    width: 100px;
    background-color: #edd;
}
<div class="wp">
    <div class="test"></div>
</div>

3、绝对定位+translate

不需要指定子块的高宽,当子块中无内容且未设置高宽,子块不可见。

IE6/7/8不支持

  .wp {
      width:200px;
      height:200px;
      background-color:yellow;
      position:relative;
  }
  .test {
      left:50%; top:50%;
      transform:translate(-50%,-50%);
      -webkit-transform:translate(-50%,-50%);
      background-color:gray;
      color:white;
      position:absolute;
  }
<div class="wp">
    <div class="test">内容</div>
</div>

4、弹性盒

不需要指定子块的高宽,当子块中无内容且未设置高宽,子块不可见。

IE6/7/8/9不支持

.wp{
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-box-align: center;
     -moz-box-align: center;
     -ms-flex-align: center;
  -webkit-align-items: center;
          align-items: center;
  -webkit-box-pack: center;
     -moz-box-pack: center;
     -ms-flex-pack: center;
  -webkit-justify-content: center;
          justify-content: center;
    height: 200px;
    width: 200px;
    background-color: #ccc;

}
.test{
background-color: #edd;
}
<div class="wp">
    <div class="test">内容</div>
</div>

5、table-cell

IE6/7不支持

.wp{
    width: 200px;
    height: 200px;
    display: table;
    background-color: #ccc;
}
.test {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}
<div class="wp">
    <div class="test">内容</div>
</div>

时间: 2024-07-31 05:40:38

css水平垂直居中块整理的相关文章

常见的几种 CSS 水平垂直居中解决办法

用CSS实现元素的水平居中,比较简单,可以设置text-align center,或者设置 margin-left:auto; margin-right:auto 之类的即可. 主要麻烦的地方还是在垂直居中的处理上,所以接下来主要考虑垂直方向上的居中实现. 水平垂直居中主要包括三类:基本文本类,图像类,其他元素类 但,也是由一些方法可以实现的,下面就来谈谈了解到的10中方法. 方法一.使用 line-height 这种方式更多地用在 单行文字的情况,其中使用overflow:hidden的设置是

css 水平垂直居中那些事

本文是在参考众大神文章基础上,整理的几个常用方案,另外也掺杂个人的一些猜想,如有不妥,请不吝指出下面开始正题,为了方便验证+展示,下面的案例我会直接附上个人验证的源码+截图 1. <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="t

CSS水平垂直居中方法总结

部分HTML代码如下: <div class="wrap block"> <div class="block-center">块儿居中</div> </div> <div class="wrap inline"> <span class="inline-center">内联居中</span> </div> 一.absolute 拔河

CSS水平垂直居中常见方法总结(转)

行内元素: 父级元素是块级元素:父元素设置text-align:center 1.元素水平居中 margin: 0 auto;谁居中,谁设置 居中不好使的原因: 1.元素没有设置宽度,没有宽度怎么居中嘛! 2.设置了宽度依然不好使,你设置的是行内元素吧 实例1: <div class="box"> <div class="content"> 哇!居中了 </div> </div> <style type=&quo

CSS水平垂直居中的几种方法

直接进入主题! 一.脱离文档流元素的居中 方法一:margin:auto法 CSS代码: div{ width: 400px; height: 400px; position: relative; border: 1px solid #465468; } img{ position: absolute; margin: auto; top: 0; left: 0; right: 0; bottom: 0; } HTML代码: <div> <img src="mm.jpg&quo

CSS水平垂直居中的几种方法2

直接进入主题! 一.脱离文档流元素的居中 方法一:margin:auto法 CSS代码: div{ width: 400px; height: 400px; position: relative; border: 1px solid #465468; } img{ position: absolute; margin: auto; top: 0; left: 0; right: 0; bottom: 0; } HTML代码: <div> <img src="mm.jpg&quo

介绍一种css水平垂直居中的方法(非常好用!)

这次介绍一下一个水平垂直居中的css方法,这个方法可以说是百试百灵,废话不多说,直接附上代码: html,body{ width:100%; height:100%; } 你需要居中的元素{ position: fixed;(absolute)      left:50%;      top:50%;      -webkit-transform: translate(-50%,-50%); } 他的父元素{ position: relative; width:100%; height:100%

聊聊css水平垂直居中那些事

1.水平居中实现方法 <div class="demo">     我是一个水平居中的div </div> /*方法1*/ .demo{width: 960px;margin: 0 auto;} /*方法2*/ .demo{width: 960px;height: 400px;background: #f00;position: relative;left: 50%;margin-left: -480px;} 2.水平垂直居中实现方法 <div class

css - 水平垂直居中几种方式

水平垂直居中 水平居中 定宽: margin: 0 auto; 不定宽: 参考例子中不定宽高例子. 垂直居中 position: absolute设置left.top.margin-left.margin-to(定高): position: fixed设置margin: auto(定高): display: table-cell: transform: translate(x, y): flex(不定高,不定宽): grid(不定高,不定宽),兼容性相对比较差: 例子 右键f12 覆盖html后