惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Troy Hunt's Blog
Schneier on Security
Schneier on Security
N
News | PayPal Newsroom
Hacker News: Ask HN
Hacker News: Ask HN
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Google DeepMind News
Google DeepMind News
www.infosecurity-magazine.com
www.infosecurity-magazine.com
N
News and Events Feed by Topic
V
Vulnerabilities – Threatpost
Cyberwarzone
Cyberwarzone
K
Kaspersky official blog
P
Privacy & Cybersecurity Law Blog
P
Privacy International News Feed
WordPress大学
WordPress大学
U
Unit 42
PCI Perspectives
PCI Perspectives
S
Schneier on Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
Visual Studio Blog
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
I
Intezer
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
Martin Fowler
Martin Fowler
B
Blog
美团技术团队
T
The Blog of Author Tim Ferriss
C
Cisco Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
酷 壳 – CoolShell
酷 壳 – CoolShell
The Last Watchdog
The Last Watchdog
J
Java Code Geeks
博客园_首页
A
About on SuperTechFans
Vercel News
Vercel News
Attack and Defense Labs
Attack and Defense Labs
H
Heimdal Security Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
小众软件
小众软件
H
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
The Exploit Database - CXSecurity.com
Y
Y Combinator Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Webroot Blog
Webroot Blog
T
Tenable Blog

博客园 - 星空竹月

ASP入门基础教程-VBScript概述及基本元素 第一次安装sql2000挂起无法安装的问题 asp.net(c#)成条形码[转] 关于文章列表如何直接显示文章类别名的问题 Page_PreInit在网页传值的应用 利用.net的强大功能发送email - 星空竹月 - 博客园 在页面和Datalist控件中判断是否为今天 - 星空竹月 - 博客园 如何在asp.net中关闭B页面时,自动刷新A页面? - 星空竹月 - 博客园 ASP.net(c#)做返回上一页效果(后退)--代码 前进和后退按钮特效代码 - 星空竹月 - 博客园 文件上传实例(文件上传,自动重命名,自动添加文件夹) ASP.NET中文显示乱码之解决方法 限制显示字数 asp.net中,限制显示字数的问题? 将截断字符串或二进制数据。 IE6下透明PNG图片的显示 IE不支持PNG图片透明效果,怎么办 配置AjaxControlToolkit 怎样换网页中的鼠标图案??换成其他图片
Div中文字竖直居中的问题
星空竹月 · 2011-03-29 · via 博客园 - 星空竹月

有时候很需要使用CSSDiv中的文字在竖直方向和水平方向居中,今天查阅了一些资料,发现其实实现方法也不时很难。

CSS 中Div文字居中有两种情况,一种是Div文字水平居中,另外一种是Div文字竖直居中

一、首先说一下Div文字竖直居中。
1.单行文字
设置div的高度与文字的行高一样就可以了,即 line-height 和 height 的数值是一样的就可以了,最后给div一个 over-flow: hidden ,让超出的部分隐藏.
这种方法支持块级和内联极元素以及所有的浏览器,不过只能显示一行,而且IE中不支持< img >等的居中,之所以要用 overflow: hidden ,是因为当用户放大字体时,字会超出div的范围.
插入代码:

div{ 
height: 20px; 
line-height: 20px; 
overflow: hidden; 
}

2.多行文字
设定一样的 padding-bottom 和 padding-top
插入代码:

div{ 
padding-top: 20px; 
padding-bottom: 20px; 
}

这种方法同时支持块级和内联极元素以及非文本内容,包括图片等等,也支持所有浏览器,不足的就是不能固定高度。

二、再来说一下Div文字水平居中。
水平居中是很基本的:
align=”center”
就可以实现。

好了,以上就是我如何实现CSS Div文字水平居中和竖直居中方法。