CSS居中代码

来源:互联网 发布:python 定时循环执行 编辑:程序博客网 时间:2024/05/18 00:07
在设计网页模板时,经常要用CSS来控制块级元素的水平或是垂直位置,往往需要进行居中的操作。今天我就和大家一起来研究下如何用CSS来控制元素的居中。

1、单行垂直居中

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

<div style="line-height:500px;height:500;"></div>


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%;">
           </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;">
                 </div>
                  </div>
          </div>
         </div>
</div>


6、div垂直滚动
<div style="width: 160px; height: 260px; overflow-y: scroll; border: 1px solid;">http://www.51xuedeiannao.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;">
 </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;">
              </div>                              
              </div>
          </div>
         </div>
</div>

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 本科毕业有毕业证没有学位证怎么办 莫名收到平安一账通验证码怎么办 新车没带行驶证怎么办 深圳行驶证副本丢了怎么办 新车行驶证是怎么办的 新车怎么办牌和行驶证 b本扣12分了怎么办 车的绿本丢了怎么办 车辆落户查不到购车发票怎么办 扣车凭证丢了怎么办 三轮车驾驶证被扣了怎么办 违章12分不够扣怎么办 忘记带行驶证了怎么办 开车不带行驶证怎么办 屏幕没碎黑屏了怎么办 三星s6屏幕不亮怎么办 苹果手机掉水里屏幕黑屏怎么办 三星s6电池不耐用怎么办 台式电脑显示屏坏了怎么办 三星笔记本电脑不显示韩文怎么办 三星s6开不开机怎么办 电脑显示屏显示检测信号线怎么办 三星s8屏幕坏了怎么办 手机挤压漏液了怎么办 三星显示器跳白色线条怎么办动 三星s6屏幕漏液怎么办 三星电视曲面屏碎了怎么办 平板摔了花屏怎么办 新手机开不了机怎么办 三星a9屏幕漏液怎么办 小米note后面玻璃碎了怎么办 换手机微信钱包怎么办 内外屏一体手机屏幕碎了怎么办 苹果手机显示屏坏了怎么办 华为手机屏裂了怎么办 乐视手机屏坏了怎么办 红米手机触屏失灵怎么办 小米4排线断了怎么办 红米3x屏幕难点怎么办 华为p7屏幕不亮怎么办 红米5外屏碎了怎么办