文字和图片居中的HTML代码怎么写?

来源:互联网 发布:cms内容管理系统 编辑:程序博客网 时间:2024/05/01 01:10

HTML 代码 ,怎么将文本/ 图片居中?这是在W3Cschool的编程问答中前端♌蕾儿提出的问题。网友施主同西否给出了详细的解答。

html文字居中和html图片居中方法代码,通过在html中实现文字居中图片居中篇在html中实现文字图片内容居中有三种方法,其中两种使用CSS样式实现,一直使用原始的html标签内加入“align="center"”(居中)实现。

一、对body加CSS居中样式


我们直接对body 设置CSS样式:text-align:center

1、完整HTML实例代码:

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml">   <head>     <meta charset="gb2312" />     <title>W3Cschool居中实例</title>     <style>       body{text-align:center}     </style>   </head>   <body>     W3Cschool会被居中   </body> </html>

2、居中实例截图

对body设置居中实现文字或图片居中
对body设置居中实现文字或图片居中截图

二、对文字外层对象加css居中样式


首先我们CSS命名选择器 为“.w3cschool”,对此选择器内加居中样式。我们实例演示2个DIV对象,一个放文字一个放图片。

1、对应CSS代码如下:

.w3cschool{text-align:center}

2、完整HTML+DIV+CSS代码如下:

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml">   <head>     <meta charset="gb2312" />     <title>W3Cschool居中实例</title>     <style>       .w3cschool{text-align:center}     </style>   </head>   <body>     <div class="w3cschool">W3Cschool会被居中</div>     <div class="w3cschool">      ![](http://upload-images.jianshu.io/upload_images/2642238-eba477a8a75edf90.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)    </div>   </body> </html>

3、CSS实现对象内图片和文字居中效果截图

实现html文字居中-html图片居中实例
实现html文字居中-html图片居中实例截图

三、之间对文字外层对象加align="center"


此方法是以前较为常见的方法,直接在html标签内使用align="center"即可实现对象内图片或文字内容实现居中。我们实例演示HTML表格里居中与一般HTML标签内内容居中。

1、完整HTML源代码:

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml">   <head>     <meta charset="gb2312" />     <title>html align居中-W3Cschool</title>   </head>   <body>     <div align="center">W3Cschool会居中的</div>     <table width="100%">       <tr>         <td align="center">表格中居中</td>       </tr>     </table>   </body> </html>

2、实例效果截图

html文字居中实现截图
html文字居中,html table 表格内文字居中实现截图
直接在标签内使用align属性,方便实践普通html标签 和html表格标签 内使用“align="center“”居中代码实现对象内内容居中。

出处:http://www.w3cschool.cn/html/question-10231663.html

0 0
原创粉丝点击