2025年6月7日 星期六 乙巳(蛇)年 三月十一 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > Html+Div+Css(前端)

HTML学习笔记——各种居中对齐

时间:07-29来源:作者:点击数:25

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所示。

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐