1、单行垂直居中

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

Example Source Code [www.52css.com]
<div style="line-height:500px;height:500;"></div>

2、层水平居中

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

Example Source Code [www.52css.com]
#parentdiv
{
width: 500px;
}
#childdiv {
width: 200px;
margin:0 auto;
}

3、层中的文字水平居中

  在childdiv的css加上text-align:center;

Example Source Code [www.52css.com]
#parentdiv
{
width: 500px;
}
#childdiv {
width: 200px;
margin:0 auto;
text-align:center;
}

4、div层垂直居中

Example Source Code [www.52css.com]
<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%;">
www.52css.com
    </div>
   </div>
  </div>
</div>

5、div层垂直水平居中,英文超长换行

Example Source Code [www.52css.com]
<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;">
      www.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.com
  </div>
</div>
</div>
</div>
</div>

6、div垂直滚动

Example Source Code [www.52css.com]
<div style="width: 160px; height: 260px; overflow-y: scroll; border: 1px solid;">
</div>

7、垂直居中和使用text-align水平居中

Example Source Code [www.52css.com]
<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;">
          www.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.com
     </div>
  </div>
</div>
    </div>
  </div>
</div>

8、垂直居中和使用margin水平居中

Example Source Code [www.52css.com]
<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;">
     www.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.comwww.52css.com
</div>
</div>
</div>
</div>
</div>
 

CSS实例:水平居中和垂直居中的多种解决方案的更多相关文章

  1. CSS的水平居中和垂直居中解决方案

    在写CSS样式的时候,有时为了美观,会添加水平居中和垂直居中,这时候你有可能会遇到很棘手的问题,有些水平居中和垂直居中的属性添加上去完全没反应,下面给大家列举一些CSS水平居中和垂直居中的终极解决方案 ...

  2. 主流 CSS 布局(水平居中、垂直居中、居中 )

    什么是布局 html 页面的整体结构或骨架 布局不是某个技术内容 而是一种设计思想 [ 布局方式 ] 水平居中布局 垂直居中布局 居中布局( 水平 + 垂直 ) 什么是水平居中布局 水平居中布局 元素 ...

  3. 【CSS】水平居中和垂直居中

    水平居中和垂直居中 2019-11-12  15:35:37  by冲冲 1.水平居中 (1)父级元素是行内元素,子级元素是行内元素,子级元素水平居中 ① 设置父级元素为块级元素 display:bl ...

  4. css的水平居中和垂直居中总结

    Html代码: <div class="md-warp"> <div class="md-main">块级元素</div> ...

  5. css:元素水平垂直居中的多种方式

    CSS元素(文本.图片)水平垂直居中方法   1.text-align:center; 2.margin:0 auto; 3.display:inline-block; + text-align:ce ...

  6. CSS:水平居中与垂直居中

    CSS居中算是一个比较基础的问题,在实际运用中,需要考虑到的一般是两种情况,一种是主要是表现为文字,图片等行内元素的居中,一种是指 div 等块级标签元素的居中. 水平居中 1.行内元素 行内元素(主 ...

  7. css元素水平居中和垂直居中的方式

    关于居中的问题,一直处于疑惑不解的状态,知道的几种方法好像也不是每一次都会起到作用,所以更加迷惑.主要是不清楚该 在什么情况下采用哪种解决方法,所以,整理了一些方法,梳理一下思路,做一个总结. 1. ...

  8. CSS元素水平居中和垂直居中的方法大全

    水平居方法: 1.最熟悉的是给元素定义一个宽度,然后使用margin: body{ width:960px; margin:0 auto;}这个是当我们的定义元素的宽度时显现的,如果我们不能定义宽度时 ...

  9. 在CSS中水平居中和垂直居中:完整的指南

    这篇文章将会按照如下思路展开: 一.水平居中 1. 行内元素水平居中 2. block元素水平居中 3. 多个块级元素水平居中 二.垂直居中 1. 行内元素水平居中 2. block元素水平居中 3. ...

随机推荐

  1. apache的keepalive和keepalivetimeout

    在APACHE的httpd.conf中,KeepAlive指的是保持连接活跃,类似于Mysql的永久连接.换一句话说,如果将KeepAlive设置为On,那么来自同一客户端的请求就不需要再一次连接,避 ...

  2. DWZ在APS.NET WebForm中的使用(二)

    任何框架由于个人理解不到位或者框架自身的局限性,在项目实施中,大家或多或少都会遇到一些问题,下面我就讲述下我在使用DWZ开发过程中的几个问题.如有一点能帮助到你,这篇文章也算有存在的意义了. 1.树菜 ...

  3. C语言中宏定义(#define)时do{}while(0)的价值(转)

    C语言中宏定义(#define)时do{}while(0)的价值 最近在新公司的代码中发现到处用到do{...}while(0),google了一下,发现Stack Overflow上早有很多讨论,总 ...

  4. HDU 1194 - Beat the Spread!

    给两数之和和两数之差,求两数,两数还必须同奇偶 #include <iostream> using namespace std; int main() { int a,b,t; cin&g ...

  5. 垃圾回收器 Dispose 和 Finalize 的互补作用

    假如我们程序有两个窗口 Form1.Form2; 当我们关闭一个窗口的时候,会发出一个 终止响应,并将该窗口对象送入终止队列,公共语言运行库的垃圾回收器跟踪着这个对象的生存期,此时就会调用此对象的基类 ...

  6. Sicily 1790. Single Round Match

    高进度求余 或者 将一个数奇位上的数字与偶位上的数字分别加起来,再求它们的差,如果这个差是11的倍数(包括0),那么,原来这个数就一定能被11整除. #include <iostream> ...

  7. jQuery.access源码分析

    基本理解 jQuery.attr是jQuery.attr,jQuery.prop,jQuery.css提供底层支持,jQuery里一个比较有特色的地方就是函数的重载, 比如attr,有如下几种重载 $ ...

  8. mysql的四种隔离级别

    一.READ UNCOMMITTED(未提交读) 在READ UNCOMMITTED级别,事务中的修改,即使未提交,对其他事务也都是可见的.事务可以读取未提交的数据,这也被称为脏读( Dirty RE ...

  9. 常用的PHP正则表达式汇总

    PHP中的常用正则表达式集锦: 匹配中文字符的正则表达式: [\u4e00-\u9fa5] 评注:匹配中文还真是个头疼的事,有了这个表达式就好办了 匹配双字节字符(包括汉字在内):[^\x00-\xf ...

  10. gnome-ssh-askpass:No such file or directory && unable to read askpass response

    1.使用git的时候,gnome-ssh-askpass出现“error: cannot run /usr/libexec/openssh/gnome-ssh-askpass: No such fil ...