CSS总结div中的内容垂直居中的五种方法

摘要:
线条高度:隐藏;divide=“单元格”>桌子红色中间的垂直居中{position:relative;divclass=“Center”>divclass=”text“>我是一个多行文本<200px;1pxsolid#ccc;-o-box;box;divclass=“flex”><flex;

一、行高(line-height)法

如果要垂直居中的只有一行或几个文字,那它的制作最为简单,只要让文字的行高和容器的高度相同即可,比如:

p { height:30px; line-height:30px; width:100px; overflow:hidden; }

这段代码可以达到让文字在段落中垂直居中的效果。

二、内边距(padding)法

另一种方法和行高法很相似,它同样适合一行或几行文字垂直居中,原理就是利用padding将内容垂直居中,比如:

p { padding:20px 0; }

这段代码的效果和line-height法差不多。

三、模拟表格法

将容器设置为display:table,然后将子元素也就是要垂直居中显示的元素设置为display:table-cell,然后加上vertical-align:middle来实现。

html结构如下:

<div id="wrapper">
    <div id="cell">
        <p>测试垂直居中效果测试垂直居中效果</p>
        <p>测试垂直居中效果测试垂直居中效果</p>
    </div>
</div>

css代码:

#wrapper {display:table;width:300px;height:300px;background:#000;margin:0 auto;color:red;}
#cell{display:table-cell; vertical-align:middle;}

实现如图所示:
CSS总结div中的内容垂直居中的五种方法第1张

遗憾的是IE7及以下不支持。

四、CSS3的transform来实现

css代码如下:

.center-vertical{
  position: relative;
  top:50%;
  transform:translateY(-50%);
}.center-horizontal{
  position: relative;
  left:50%;
  transform:translateX(-50%); 
}

五:css3的box方法实现水平垂直居中

html代码:

<div class="center">
  <div class="text">
    <p>我是多行文字</p>
    <p>我是多行文字</p>
    <p>我是多行文字</p>
  </div>
</div>

css代码:

.center {
  width: 300px;
  height: 200px;
  padding: 10px;
  border: 1px solid #ccc;
  background:#000;
  color:#fff;
  margin: 20px auto;

display: -webkit-box; -webkit-box-orient: horizontal; -webkit-box-pack: center; -webkit-box-align: center; display: -moz-box; -moz-box-orient: horizontal; -moz-box-pack: center; -moz-box-align: center; display: -o-box; -o-box-orient: horizontal; -o-box-pack: center; -o-box-align: center; display: -ms-box; -ms-box-orient: horizontal; -ms-box-pack: center; -ms-box-align: center; display: box; box-orient: horizontal; box-pack: center; box-align: center; }

 结果如图:

CSS总结div中的内容垂直居中的五种方法第2张

 六:flex布局(2018/04/17补充

html代码:

<div class="flex">
    <div>
       <p>我是多行文字我是多行文字我是多行文字我是多行文字</p>
      <p>我是多行文字我是多行文字我是多行文字我是多行文字</p>
    </div>
</div>

CSS代码:

.flex{
    /*flex 布局*/
    display: flex;
    /*实现垂直居中*/
    align-items: center;
    /*实现水平居中*/
    justify-content: center;
    
    text-align: justify;
    width:200px;
    height:200px;
    background: #000;
    margin:0 auto;
    color:#fff;
}

实现效果:

CSS总结div中的内容垂直居中的五种方法第3张

免责声明:文章转载自《CSS总结div中的内容垂直居中的五种方法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇scan chain的原理和实现——13.Report &amp;amp; Data Expors纯css实现鼠标感应弹出二级菜单下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Flink 写数据到MySql (JDBC Sink)

POM 文件 <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-scala_2.11</artifactId> <ver...

Web service 超过了最大请求长度

Web service的默认的请求长度是4M 当内容超过了4M会报错 System.Web.Services.Protocols.SoapException: 运行配置文件中指定的扩展时出现异常。 ---> System.Web.HttpException: 超过了最大请求长度。 在 System.Web.HttpRequest.GetEnti...

如何在ubuntu下安装xampp

安装前置工作,到这里下载xampp for Linux。下载后,把它放在你的主文件夹中,比如我的是“root@ubuntu:/home/roy”,也就是我的主文件夹。路径不对,执行下面程序时会提示你错误的哦,比如提示“没有那个文件或目录”什么的。大家都是明白人。1、首先主面板下搜索“终端”,或者你可以直接按下Ctrl+Alt+T,启动终端。 2、进入终端...

阻止Bootstrap 模态框点击背景空白处自动关闭

问题描述 模态框点击空白处,会自动关闭,怎么阻止关闭事件呢? 解决方法 在HTML页面中编写模态框时,在div初始化时添加属性 aria-hidden=”true” data-backdrop=”static”,即可。 <!-- 模态框(Modal) --> <div class="modal fade" id="myModal" t...

JS实现“隐藏与显示”功能(多种方法)

1,通过按钮实现隐藏与显示: 这个是通过按钮点击实现的隐藏与显示,具体代码如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47...

springboot整合dubbo的简单案例

使用框架:   jdk 1.8   springboot-2.1.3    dubbo-2.6   spring-data-jpa-2.1.5 一、开发dubbo服务接口: 按照Dubbo官方开发建议,创建一个接口项目,该项目只定义接口和model类; 1、创建springboot工程 spring-boot-demo-dubbo-interface 坐标...