首页 技术 正文
技术 2022年11月21日
0 收藏 822 点赞 5,017 浏览 1023 个字

1、div自身居中

使用margin:0 auto
上下为0,左右自适应的css样式。

要让div水平居中,那么除了设置css margin:0 auto外,还不能再设置float,不然将会导致div靠左(设置float:left)和div靠右(设置float:right)。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>div布局居中实例 thinkcss</title>
<style>
#jz{ margin:0 auto; width:320px; height:100px; border:1px solid #F00}
</style>
</head>
<body>
<div id="jz">设置margin:0 auto兼容各大浏览器让div水平居中</div>
</body>
</html>

以上实例正是使用margin:0 auto让div兼容各大浏览器的水平居中。

2、div内的内容居中

内容居中分为div内容水平居中与div内容垂直居中。

而div内容居中比较简单,只需要设置一个内容居中css(text-align:center)、内容垂直居中(line-height)即可。

(1)div内容水平居中CSS:
text-align:center
无论是p还是div都可以对其设置此CSS实现对应对象内的内容水平居中。

(2)div内容垂直居中 行高属性:
line-height
要让div内只有一行的内容垂直居中,通常对div设置的height(高)与line-height(行高)相同,即可实现div内容垂直居中。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>div内容居中实例 thinkcss</title>
<style>
#juzhong{width:320px;height:100px; line-height:100px; text-align:center; border:1px solid #F00}
</style>
</head>
<body>
<div id="juzhong">text-align和line-height设置水平与垂直居中</div>
</body>
</html>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918