CSS水平和垂直居中的几种实现方法:

1、单行垂直居中 
文字在层中垂直居中vertical-align 属性是做不到的.我们这里有个比较巧妙的方法就是:设置height的高度与line-height的高度相同!

<div style="line-height:500px;height:500;">

2、层水平居中 
设置div的宽度小于父div的宽度,设置 margin:0 auto;,即可让div居中。

#parentdiv 

  width: 500px; 

#childdiv { 
  width: 200px; 
  margin:0 auto; 
}

3、层中的文字水平居中 
在childdiv的css加上text-align:center; 
  
#parentdiv 

  width: 500px; 

#childdiv { 
  width: 200px; 
  margin:0 auto; 
  text-align:center; 
}

4、div层垂直居中 
<div style="width:275px;height:375px;border: solid red;"> 
  <div style="background:green;height: 375px; width: 275px; position: relative; display: table-cell; vertical-align: middle;"> 
    <div style="background:red;position:static;position:absolute\9; top: 50%;"> 
      <div style="background:blue;position: relative; top: -50%;"> 
       book.chinaz.com  
      </div> 
    </div> 
  </div> 
</div>

5、div层垂直水平居中,英文超长换行 
<div style="float:left;width:275px;height:375px;border: solid red;"> 
  <div style="height: 375px; width: 275px; position: relative; display: table-cell; vertical-align: middle;"> 
    <div style="position:static;position:absolute\9; top: 50%;"> 
      <div style="position: relative; top: -50%; text-align: center;"> 
        <div style="width: 85px;WORD-WRAP: break-word;TABLE-LAYOUT: fixed;word-break:break-all;margin:0 auto;"> 
          book.chinaz.com book.chinaz.com book.chinaz.com book.chinaz.com book.chinaz.com book.chinaz.com book.chinaz.com
        </div> 
      </div> 
    </div> 
  </div> 
</div>

6、div垂直滚动 
<div style="width: 160px; height: 260px; overflow-y: scroll; border: 1px solid;"> 
  book.chinaz.com  
</div>

7、垂直居中和使用text-align水平居中 
<div style="float:left;width:275px;height:375px;border: solid red;"> 
  <div style=" height: 375px; width: 275px; position: relative; display: table-cell; vertical-align: middle;"> 
    <div style="position:static;position:absolute\9;top: 50%;"> 
      <div style="position: relative; top: -50%; text-align:center;"> 
        <div style="width: 275px;"> 
          <div style="width: 160px;WORD-WRAP: break-word;TABLE-LAYOUT: fixed;word-break:break-all;text-align:left;"> 
            book.chinaz.com book.chinaz.com book.chinaz.com book.chinaz.com book.chinaz.com book.chinaz.com book.chinaz.com 
          </div> 
        </div> 
      </div> 
    </div> 
  </div> 
</div>

8、垂直居中和使用margin水平居中 
<div style="float:left;width:275px;height:375px;border: solid red;"> 
  <div style=" height: 375px; width: 275px; position: relative; display: table-cell; vertical-align: middle;"> 
    <div style="position:static;position:absolute\9; top: 50%;"> 
      <div style="position: relative; top: -50%; "> 
        <div style="margin:0 auto;width: 160px;WORD-WRAP: break-word;TABLE-LAYOUT: fixed;word-break:break-all;"> 
          book.chinaz.com book.chinaz.com book.chinaz.com book.chinaz.com book.chinaz.com book.chinaz.com book.chinaz.com book.chinaz.com 
        </div> 
      </div> 
    </div> 
  </div> 
</div>

http://www.pc6.com/infoview/Article_45869.html

css水平竖直居中方式的更多相关文章

  1. 水平/竖直居中在旧版Safari上的bug

    今天调了两个出现在旧版Safari上的layout bug. 它们最初是在同事的iPad上被发现的, 我在自己桌面上安装的Safari 5.1.7上也能够复现. Bug1: .vertical-cen ...

  2. html 水平竖直居中

    line-height:容器高度 <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

  3. CSS -- 文字竖直居中

    元素的height 和 lineheight 设置为一样即可.

  4. css水平居中,竖直居中技巧(二)

    css水平居中,竖直居中技巧(二)===### 1.效果 ### 2.代码#### 2.1.index.html <!DOCTYPE html> <html lang="z ...

  5. css水平居中,竖直居中技巧(一)

    css水平居中,竖直居中技巧(一)===### 1.效果 ### 2.代码#### 2.1.index.html <!DOCTYPE html> <html lang="z ...

  6. 常用布局,div竖直居中

    常用两列布局,多列布局和div竖直居中 body { margin:; padding:; } .w200 { width: 200px; } .mar-left200 { margin-left: ...

  7. 讨论CSS中的各类居中方式

    今天主要谈一谈CSS中的各种居中的办法. 首先是水平居中,最简单的办法当然就是 margin:0 auto; 也就是将margin-left和margin-right属性设置为auto,从而达到水平居 ...

  8. css控制竖直文字显示

    假如有一天,你把水平文字看腻味了...... 我建议你不妨试试垂直文字显示,就像这样: 我 是 竖 直 显 示 的 : 哈哈! 言归正传,怎么把一段话,垂直显示呢? 方法1: //把下面代码另存为ht ...

  9. css中两种居中方式text-align:center和margin:0 auto 的使用场景

    关于使用text-align:center和margin:0 auto 两种居中方式的比较 前言:最近由于要学习后端,需要提前学习一部分前端知识,补了补css知识,发现狂神在讲这一部分讲的不是特别清楚 ...

随机推荐

  1. flask的插件

    详情参考官方文档 组件一:flask-session 安装: pip  install  flask-session 使用方法:先导入   from flask_session import Sess ...

  2. 云平台服务运行情况检测脚本V0.1

    1.准备Python3环境 yum groupinstall "Development tools" -y yum install zlib-devel bzip2-devel o ...

  3. Ajaxterm + nginx 实现一个简单的堡垒机

    https://blog.csdn.net/zhang19771105/article/details/50497581 http://wuliangxx.iteye.com/blog/600113

  4. Android日志打印类LogUtils,能够定位到类名,方法名以及出现错误的行数并保存日志文件

    Android日志打印类LogUtils,能够定位到类名,方法名以及出现错误的行数并保存日志文件 在开发中,我们常常用打印log的方式来调试我们的应用.在Java中我们常常使用方法System.out ...

  5. 【cocos2d-js官方文档】十七、事件分发机制

    简介http://blog.csdn.net/qinning199/article/details/41951517 游戏开发中一个很重要的功能就是交互,如果没有与用户的交互,那么游戏将变成动画,而处 ...

  6. CF 816B Karen and Coffee【前缀和/差分】

    To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...

  7. Python与数据结构[4] -> 散列表[2] -> 开放定址法与再散列的 Python 实现

     开放定址散列法和再散列 目录 开放定址法 再散列 代码实现 1 开放定址散列法 前面利用分离链接法解决了散列表插入冲突的问题,而除了分离链接法外,还可以使用开放定址法来解决散列表的冲突问题. 开放定 ...

  8. amq笔记:记一个关于PooledConnectionFactory的问题

    替人排查一个关于amq连接数的问题,使用PooledConnectionFactory进行连接池管理,设置了连接数上限为3,但部署到服务器之后,瞬间建立了几百个连接,用netstat -an 查看,发 ...

  9. 线段树维护矩阵【CF718C】 Sasha and Array

    Description 有一个长为\(n\)的数列\(a_{1},a_{2}...a_{n}\),你需要对这个数列维护如下两种操作: \(1\space l \space r\space x\) 表示 ...

  10. oracle to_char FM099999

    to_char(column,'FM099999') The FM in the format removes leading and trailing blanks.