Sealyu

          --- 博客已遷移至: http://www.sealyu.com/blog

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            618 隨筆 :: 87 文章 :: 225 評(píng)論 :: 0 Trackbacks
          <2010年1月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          常用鏈接

          留言簿(14)

          隨筆分類(lèi)

          隨筆檔案

          友情鏈接

          搜索

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          一,用CSS使DIV層水平居中

          對(duì)需要水平居中的DIV層添加以下屬性:
             margin-left: auto;
             margin-right: auto;

          這樣在FF中已經(jīng)居中了,可是在IE中看還是沒(méi)有居中!

          問(wèn)題并不在CSS而在XHTML網(wǎng)頁(yè)本身.

          需要加上這樣的代碼才能使得上述設(shè)置有效果:

          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

          如何使DIV居中

          主要的樣式定義如下:

          body { TEXT-ALIGN: center; }

          #center { MARGIN-RIGHT: auto; MARGIN-LEFT: auto; }

          說(shuō)明:

          首先在父級(jí)元素定義TEXT-ALIGN: center;這個(gè)的意思就是在父級(jí)元素內(nèi)的內(nèi)容居中;對(duì)于IE這樣設(shè)定就已經(jīng)可以了。但在mozilla中不能居中。解決辦法就是在子元素定義時(shí)候設(shè)定 時(shí)再加上“MARGIN-RIGHT: auto;MARGIN-LEFT: auto; ”

          需要說(shuō)明的是,如果你想用這個(gè)方法使整個(gè)頁(yè)面要居中,建議不要套在一個(gè)DIV里,你可以依次拆出多個(gè)div,只

          要在每個(gè)拆出的div里定義MARGIN-RIGHT: auto;MARGIN-LEFT: auto; 就可以了。

          如何使圖片在DIV 中垂直居中

          用背景的方法。舉例:

          body{ BACKGROUND: url(http://www.w3cn.org/style/001/logo_w ... )#FFFno-repeatcenter; }

          關(guān)鍵就是最后的center,這個(gè)參數(shù)定義圖片的位置。還可以寫(xiě)成“top left”(左上角)或者"bottom right"等,也可以直接寫(xiě)數(shù)值"50 30"

          如何使文本在DIV中垂直居中

          果是文字,便不能用背景方法,可以用增高行距的辦法變通實(shí)現(xiàn)垂直居中,完整代碼如下:

          <html>

          <head>

          <style>

          body{ TEXT-ALIGN: center; }

          #center{ MARGIN-RIGHT: auto;

          MARGIN-LEFT: auto;

          height:200px;

          background:#F00;

          width:400px;

          vertical-align:middle;

          line-height:200px;

          }

          </style>

          </head>

          <body>

          <div id="center"><p>test content</p></div>

          </body>

          </html>

          說(shuō)明:

          vertical-align:middle;表示行內(nèi)垂直居中,我們將行距增加到和整個(gè)DIV一樣高line-height:200px;然后插入文字,就垂直居中了。

          CSS+DIV控制頁(yè)面中元素垂直居中代碼 全局和區(qū)域垂直居中

          <style type="text/css" media=screen>

          body

          {

          text-align: center;

          }

          #a

          {

          width: 200px;

          height: 400px;

          background: #000;

          }

          #b

          {

          margin-top: expression((a.clientHeight-50)/2);

          width: 50px;

          height: 50px;

          background: #FFF;

          }

          #c

          {

          position: absolute;

          left: expression((body.clientWidth-50)/2);

          top: expression((body.clientHeight-50)/2);

          width: 50px;

          height: 50px;

          background: #F00;

          }

          </style>

          <div id="a">

          <div id="b"></div>

          </div>

          <div id="c"></div>

          另一方法:

          <div style="background:blue;position:absolute;left:expression((body.clientWidth-50)/2);top:expression((body.clientHeight-50)/2);width:50;height:50"></div>

          ***************************************

          二,用JS使層居中

          把如下代碼加入<body>區(qū)域中

          <DIV id=L1 style="HEIGHT: 107px; LEFT: 341px; POSITION: absolute; TOP: 585px; WIDTH: 205px; Z-INDEX: 1">

          <TABLE bgColor=#ffffff border=1 borderColorDark=#ffffff borderColorLight=#000000 cellPadding=0 cellSpacing=0 width="100%">

          <TR>

          <TD height=1 width=178>□::BEST4U論壇::</TD>

          <TD align=center onclick="L1.style.visibility='hidden'"

          style="CURSOR: hand" width=21>х</TD></TR>

          <TR vAlign=top>

          <TD colSpan=2

          height=91>歡迎光臨本站!<BR><BR>本站域名:<BR>http://webform.cn

          </TD></TR></TABLE></DIV>

          <SCRIPT LANGUAGE="JavaScript">

          function moveit() //這個(gè)函數(shù)用于把層放到瀏覽器中間

          {

          L1.style.left=((document.body.offsetWidth-parseFloat (L1.style.width))/2)+document.body.scrollLeft;

          L1.style.top=((document.body.offsetHeight-parseFloat (L1.style.height))/2)+document.body.scrollTop;

          }

          window.onload=moveit; //網(wǎng)面打開(kāi)時(shí)時(shí)執(zhí)行moveit()

          window.onresize=moveit; //當(dāng)調(diào)整瀏覽器大小時(shí)執(zhí)行moveit()

          window.onscroll=moveit; //當(dāng)拉動(dòng)滾動(dòng)條時(shí)執(zhí)行moveit()

          </SCRIPT>

          posted on 2010-01-14 20:49 seal 閱讀(242) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): CSS
          主站蜘蛛池模板: 临泽县| 临沂市| 三河市| 民丰县| 朔州市| 攀枝花市| 安泽县| 大厂| 辛集市| 上林县| 应城市| 贡山| 新泰市| 工布江达县| 定州市| 阿克苏市| 大兴区| 驻马店市| 长顺县| 碌曲县| 旺苍县| 额济纳旗| 葵青区| 客服| 沐川县| 四川省| 望江县| 鞍山市| 河曲县| 定兴县| 将乐县| 阿鲁科尔沁旗| 石狮市| 年辖:市辖区| 洛浦县| 肃北| 新邵县| 永昌县| 顺昌县| 南城县| 玉田县|