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. 解决Eclipse明明有错误,却不能显示错误红叉的方法,eclipse不能显示错误

    出现这情况的原因是因为java文件的错误太多,eclipse停止编译.解决方法如下 1.勾选自动编译功能 2.clean工程 3.取消“abort build when build path erro ...

  2. ATM+购物车系统

    ATM目录 start.py import os,sys from core import src base_path=os.path.dirname(os.path.dirname(__file__ ...

  3. 通过文件配置:firewalld.service(5)

    firewalld.service Name firewalld.service — firewalld service configuration files Synopsis /etc/firew ...

  4. LUA脚本中O(2)级素数查询

    --================================================================================================== ...

  5. mysql union用法代码示例

    MYSQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时 ...

  6. C# 日期格式化{0:yyyy-MM-dd HH:mm:ss.fff}和{0:yyyy-MM-dd hh:mm:ss.fff}的区别(转)

    日期格式化{0:yyyy-MM-dd HH:mm:ss.fff}和{0:yyyy-MM-dd hh:mm:ss.fff}的区别http://www.cnblogs.com/jhxk/articles/ ...

  7. windows下tortoiseGit安装和使用

    一.安装git for windows 首先下载git for windows客户端http://msysgit.github.io/ 安装过程没什么特别的,不停next就ok了     图太多就不继 ...

  8. (20)python pycharm

    使用GitHub 一·登录GitHub 1. 2. 3. 4. 二. 登录成功后再配置git 1 2.创建项目到github 3.下载github

  9. (寒假开黑gym)2018 USP Try-outs

    layout: post title: (寒假开黑gym)2018 USP Try-outs author: "luowentaoaa" catalog: true tags: m ...

  10. hdu6155

    hdu6155 题意 给出一个只由 \(01\) 组成的字符串 \(s\),有两种操作: 翻转区间 \([l, r]\) 查询区间 \([l, r]\) 有多少不同的子串 分析 首先考虑怎么统计区间有 ...