0.前言
    水平居中基本方法——指定块的宽度并设定块的左右外边距为auto,上下外边距可取0,那么该块能够在父元素中水平居中。
样式例如以下:
1:margin:0px auto
2:margin-left:auto; margin-right:auto;
    垂直居中基本方法——设定块的上下内边距相等。
样式例如以下:
    padding-top:20px; padding-bottom:20px;

1.div居中对齐

【HTML】
<!DOCTYPE html>
<html>
<head>
<style>
#all{
margin:0px auto;
width:500px; /* 必须制定宽度 */
height:200px;
background-color:blue;
}
</style>
</head>
<body>
<div id="all">
<div>
</body>
</html>
【效果】
【1】body中有一个ID为all的块,该块的宽度为500px,高度为200px。通过margin:0px auto在body中水平居中。

图1 div居中

2.div中文字居中
【HTML】
<!DOCTYPE html>
<html>
<head>
<style>
#all{
margin:0px auto;
width:500px; /* 必须制定宽度 */
height:200px;
background-color:blue;
}
#string{
margin:0px auto;
width:300px;
height:100px;
background-color:red;
text-align:center; /* 文字居中 */
font-size:32px; /* 文字大小 */
color:white; /* 文字颜色 */
}
</style>
</head>
<body>
<div id="all">
<div id="string">center<div>
<div>
</body>
</html>
【效果】
【1】body中有一个ID为all的块,该块的宽度为500px,高度为200px。在body中水平居中。
【2】在名称为all的块中有一个ID为string的块。通过margin:0px auto使得该块在父元素中水平居中。

text-align:center使得


图2 div 文字水平居中

3 div图片居中
【HTML】
<!DOCTYPE html>
<html>
<head>
<style>
#all{
margin:0px auto;
width:500px; /* 必须制定宽度 */
height:200px;
background-color:blue;
}
#string{
margin:0px auto;
width:300px; /* 必须制定宽度 */
height:100px;
background-color:red;
text-align:center; /* 文字居中 */
font-size:32px; /* 文字大小 */
color:white; /* 文字颜色 */
}
#image{
margin:0px auto;
width:300px; /* 必须制定宽度 */
background-color:white;
text-align:center; /* 图像居中 */
padding-top:20px; /* 图像上填充 */
padding-bottom:20px; /* 图像下填充 */
}
</style>
</head>
<body>
<div id="all">
<div id="string">center</div>
<div id="image"><img src="loader.gif"></div>
</div>
</body>
</html>
【效果】
【1】图片在div中居中的方法和文字同样,父div中设定text-align:center就可以。
【2】假设须要图片垂直居中,那么能够设定父div的上下内边距。比如 padding-top:20px;padding-bottom:20px;

图3 div中图像水平居中

4.表格内容居中
【HTML】——HTML写法
<!DOCTYPE html>
<html>
<head>
<style>
#all{
margin:0px auto;
width:500px; /* 必须制定宽度 */
height:200px;
background-color:blue;
}
</style>
</head>
<body>
<div id="all">
<!-- 表格在父窗口中居中 -->
<table width="80%" align="center" border="1">
<tbody>
<!-- 单元格居中 -->
<tr height="50px"><td align="center">文字居中</td></tr>
<tr height="50px"><td align="center"><img src="loader.gif"></td></tr>
</tbody>
</table>
</div>
</body>
</html>
【效果】
【1】<table align="center" > 使得表格在父div中水平居中。
【2】<td align="center"> 使得单元格中的内容水平居中,请注意单元格中的内容默认垂直居中。

图4 表格内容居中——HTML写法

【HTML】CSS写法
<!DOCTYPE html>
<html>
<head>
<style>
#all{
margin:0px auto;
width:500px; /* 必须制定宽度 */
height:200px;
background-color:blue;
}
/* 设置边框 */
table, th, td{
border: 1px solid black;
}
/* 设置table位置 */
table{
margin:0px auto; /* 效果等同 <tabel align="center">*/
width:80% /* 必须制定宽度 */
}
/* 单元格对齐 */
td{
text-align:center;
}
</style>
</head>
<body>
<div id="all">
<table>
<tbody>
<tr height="50px"><td>文字居中</td></tr>
<tr height="50px"><td><img src="loader.gif"></td></tr>
</tbody>
</table>
</div>
</body>
</html>
【效果】
【1】table{margin:0px auto;},使得表格在父div中水平居中
【2】td{text-align:center;},单元格内容水平居中,请注意td{text-align:center;}和<td align="center">有同样效果。
【3】效果和如图4所看到的。

參考资料

HTML学习笔记——各种居中对齐的更多相关文章

  1. Bootstrap学习笔记之文本对齐风格

    文本对齐风格 在排版中离不开文本的对齐方式.在CSS中常常使用text-align来实现文本的对齐风格的设置.其中主要有四种风格: ☑  左对齐,取值left ☑  居中对齐,取值center ☑   ...

  2. 毕业设计 之 二 PHP学习笔记(一)

    毕业设计 之 二 PHP学习笔记(一) 作者:20135216 平台:windows10 软件:XAMPP,DreamWeaver 一.环境搭建 1.XAMPP下载安装 XAMPP是PHP.MySQL ...

  3. css居中学习笔记

    css居中学习笔记 一.水平居中 以下面的代码为例: <body> <div class="parent"> <div class="chi ...

  4. CSS学习笔记

    CSS学习笔记 2016年12月15日整理 CSS基础 Chapter1 在console输入escape("宋体") ENTER 就会出现unicode编码 显示"%u ...

  5. Android学习笔记(七)——常见控件

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! Android 给我们提供了大量的 UI控件,下面我们简单试试几种常用的控件. 一.TextView 在布局文 ...

  6. Java-Eclipse插件开发学习笔记

    Eclipse插件 学习笔记 作者   Rick- Bao 开始日期  2014年8月26日 结束日期  2014年8月27日 一 . CVS(current version system) 版本控制 ...

  7. Web前端学习笔记(001)

    ....编号    ........类别    ............条目  ................明细....................时间 一.Web前端学习笔记         ...

  8. AngularJs学习笔记--html compiler

    原文再续,书接上回...依旧参考http://code.angularjs.org/1.0.2/docs/guide/compiler 一.总括 Angular的HTML compiler允许开发者自 ...

  9. Bootstrap学习笔记(一) 排版

    Bootsrap是一款优秀的前端开发框架,我从慕课网上开始学习Bootstrap,以下我学习过程中的一些笔记及代码. 首先学习排版: 从Bootstrap网站下载Bootstrap3中文文档(V3.3 ...

随机推荐

  1. ubunut在系统恢复模式下无法改动rootpassword的分析和解决

    前些日子本猫的ubuntu 14.10貌似出了点问题,想改动下rootpassword,可是无奈原系统有错正常情况下无法改动啊.这是逼我重装的节奏吗? 在ubuntu开机后马上按住left_shift ...

  2. Struts 2中的constant详解

    通过对这些属性的配置,可以改变Struts 2 框架的一些默认行为,这些配置可以在struts.xml文件中完成,也可以在struts.properties文件中完成. 1.<constant ...

  3. AOP编程,spring实现及JDK,CGLIB实现

    什么是AOP? AOP(Aspect-OrientedProgramming,面向方面编程)和OOP(Object-Oriented Programing,面向对象编程)思想不同,两者并非对立关系,前 ...

  4. 编程算法 - 有序双循环链表的插入 代码(C)

    有序双循环链表的插入 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 有序双循环链表的插入, 须要找到插入位置, 能够採用, 两个指针, 一个在前, 一 ...

  5. CSDN改版问题多多

    刚刚上CSDN,发现改版了,推出C币功能. 然后看了2分钟,发现了一个Bug,于是准备提交到论坛.但是--居然提交Bug的论坛也出现Bug.印象中,每次CSDN更新版本号Bug都非常多,这,作为程序猿 ...

  6. winform基础——实现简易赈灾物资发放登记系统

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  7. [置顶] PMBOOK第四版-ITO与数据流图总结

    具体文档下载地址: 点击打开文档下载地址 :http://download.csdn.net/detail/lyjluandy/6694205 一.过程组与知识领域表(简图) 二.输入 - 工具 - ...

  8. CSipSimple最新版本号

    要使用CSipSimple有两种方法:第一种是不编译jni,另外一种是编译jni. 这里介绍的是第一种:不编译jni. 首先,用SVNclient检出CSipSimple源代码:svn checkou ...

  9. Matlab的parfor并行编程

    Matlab的parfor并行编程 通常消耗最多计算资源的程序往往是循环. 把循环并行化.或者优化循环体中的代码是最经常使用的加快程序执行速度的思路. Matlab提供了parforkeyword,能 ...

  10. Swift - whose view is not in the window hierarchy 问题解决方法

    问题现象:想在页面初始化的时候,使用self.presentViewController方法弹出个告警提示框UIAlertController.但行后报了个如下告警,同时告警框也出不来. 1 2015 ...