文章导航PC6首页软件下载单机游戏安卓资源苹果资源

pc软件新闻网络操作系统办公工具编程服务器软件评测

安卓新闻资讯应用教程刷机教程安卓游戏攻略tv资讯深度阅读综合安卓评测

苹果ios资讯苹果手机越狱备份教程美化教程ios软件教程mac教程

单机游戏角色扮演即时战略动作射击棋牌游戏体育竞技模拟经营其它游戏游戏工具

网游cf活动dnf活动lol周免英雄lol礼包

手游最新动态手游评测手游活动新游预告手游问答

您的位置:首页网页设计HTML/CSS  → CSS水平和垂直居中的几种实现方法

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

时间:2010/2/11 10:59:00来源:本站整理作者:我要评论(0)

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> 

相关视频

    没有数据

相关阅读 个性化jQuery和CSS3菜单源码CSS实现Tab技巧CSS复位-CSS Reset代码举例IE6绝对定位的bug及其解决办法CSS样式表创建美妙绝伦的网站几个最常用和实用的CSS技巧将网站(主页)变成黑白DW设计网页时组织CSS的建议

文章评论
发表评论

热门文章 没有查询到任何记录。

最新文章 页面进入效果表单 XHTML校验需要注意的6个细节CSS样式表创建美妙绝伦的网站了解XHTML DHTML SHTML之间的区别HTML小技巧的一些小技巧

人气排行 容易被误解的overflow:hiddenCSS中dashed和dotted的区别CSS水平和垂直居中的几种实现方法DIV+CSS自适应高度兼容IE6,ie7,ie8和FF的解css优先级问题如何用CSS实现图片圆角化处理超链接target=_blank的使用方法Ctrl+H键的用法