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. jQuery九宫格图片拉伸变大代码

    之前看到网上有jQuery九宫格图片拉伸变大代码只可以动六张图片,我改了改做了九张图片都可以做的 图片的布局 成品就是每一个图片都可以动看到大图 css样式 <style> /*九宫格*/ ...

  2. 安装并解决purcell/emacs.d网络问题

    安装: $ git clone https://github.com/purcell/emacs.d.git ~/.emacs.d 不使用https访问lisp库: $ sed -i 's/https ...

  3. 【SQL】事务

    1.事务的开始结束: START TRANSACTION :标记事务开始 COMMIT :标记事务成功结束 ROLLBACK :标记事务夭折 2.设定事务只读.读写性质: SET TRANSACTIO ...

  4. nodejs使用fetch获取WebAPI

    在上一篇<Nodejs获取Azure Active Directory AccessToken>中,已经获取到了accessToken,现时需要获取WebAPI的数据,选择了node-fe ...

  5. [thinkphp] 是如何输出一个页面的

    表面上看,TP输出一个页面很简单:$this->display(); 实际上是怎么回事呢?$this->display(); 这个display()方法是定义在ThinkPHP/Libra ...

  6. golang笔记:cookie

    在同一个问题上栽了两次,以后碰到cookie出问题多半都是因为这个. Request.Cookie(name)取Cookie的时候,返回值只有name和value cookie.go cookies ...

  7. Spark1.4远程调试

    1)首先,我们是在使用spark-submit提交作业时,使用 --driver-java-options ”-Xdebug -Xrunjdwp:transport=dt_socket,server= ...

  8. Linux下CURL设置请求超时时间

    使用CURL时,有两个超时时间:一个是连接超时时间,另一个是数据传输的最大允许时间. 连接超时时间用--connect-timeout参数来指定,数据传输的最大允许时间用-m参数来指定. 例如: cu ...

  9. Oracle RAC 环境下的 v$log v$logfile

    通常情况下,在Oracle RAC 环境中,v$视图可查询到你所连接实例的相关信息,而gv$视图则包含所有实例的信息.然而在RAC环境中,当我们查询v$log视图时说按照常理的话,v$log视图应当看 ...

  10. vue假全家桶升级方式,形成类似于小程序的路径管理(新增require-css与require-text)

    1.路径布局大致就是这样,完全模拟小程序,主要是靠require来做到的 2.首先index.html是这样的(配置js和css没有用requireJs主要是方便而且载入什么组件比较清晰) <! ...